Browse Source

Added remove props handler

master
Anthony Hinsinger 5 years ago
parent
commit
41bf5e4b33
  1. 49
      src/apolloclient.js

49
src/apolloclient.js

@ -1,5 +1,3 @@
import { onError } from "@apollo/client/link/error";
import { WebSocketLink } from "@apollo/client/link/ws";
import { import {
ApolloClient, ApolloClient,
InMemoryCache, InMemoryCache,
@ -8,8 +6,9 @@ import {
HttpLink, HttpLink,
split, split,
} from "@apollo/client"; } from "@apollo/client";
import { WebSocketLink } from "@apollo/client/link/ws";
import { onError } from "@apollo/client/link/error";
import { getMainDefinition } from "@apollo/client/utilities"; import { getMainDefinition } from "@apollo/client/utilities";
import { import {
propertyVectorFragment, propertyVectorFragment,
vectorFragment, vectorFragment,
@ -17,12 +16,10 @@ import {
} from "./graphql/fragment"; } from "./graphql/fragment";
import { isConnectedQuery } from "./graphql/query"; import { isConnectedQuery } from "./graphql/query";
// Create an http link:
const httpLink = new HttpLink({ const httpLink = new HttpLink({
uri: "/graphql", uri: "/graphql",
}); });
// Create a WebSocket link:
const wsLink = new WebSocketLink({ const wsLink = new WebSocketLink({
uri: `ws://${window.location.host}/graphql`, uri: `ws://${window.location.host}/graphql`,
options: { options: {
@ -68,15 +65,17 @@ const client = new ApolloClient({
}, },
}, },
}, },
},
}),
resolvers: {
Device: { Device: {
messages() { fields: {
return []; messages: {
read(val = []) {
return val;
},
}, },
}, },
}, },
},
}),
}); });
const CONNECTED = gql` const CONNECTED = gql`
@ -214,6 +213,36 @@ client
}); });
}); });
const REMOVE_PROPERTY = gql`
subscription removeProperty {
removeProperty {
id
device
}
}
`;
client
.subscribe({
query: REMOVE_PROPERTY,
})
.subscribe(({ data }) => {
const res = client.readQuery({
query: GETDEVICE,
variables: { id: data.removeProperty.device },
});
client.writeQuery({
query: GETDEVICE,
variables: { id: res.device.id },
data: {
device: {
...res.device,
properties: res.device.properties.filter(p => p.name !== data.removeProperty.id),
},
},
});
});
const NEW_VALUE = gql` const NEW_VALUE = gql`
subscription newValue { subscription newValue {
newValue { newValue {

Loading…
Cancel
Save