2020-07-31 23:10:34 -07:00
|
|
|
import { ApolloClient, createHttpLink, InMemoryCache } from "@apollo/client";
|
2020-05-14 15:51:08 -07:00
|
|
|
import { createPersistedQueryLink } from "apollo-link-persisted-queries";
|
|
|
|
|
2020-08-19 17:50:05 -07:00
|
|
|
const cachedZones = require("./cached-data/zones.json");
|
2020-08-19 18:01:20 -07:00
|
|
|
const cachedZonesById = new Map(cachedZones.map((z) => [z.id, z]));
|
2020-08-19 17:50:05 -07:00
|
|
|
|
2020-07-31 23:10:34 -07:00
|
|
|
const typePolicies = {
|
2020-05-14 15:51:08 -07:00
|
|
|
Query: {
|
2020-07-31 23:10:34 -07:00
|
|
|
fields: {
|
|
|
|
// Teach Apollo how to serve `items` queries from the cache. That way,
|
|
|
|
// when you remove an item from your outfit, or add an item from search,
|
|
|
|
// Apollo knows it already has the data it needs and doesn't need to ask
|
|
|
|
// the server again!
|
2020-07-31 23:21:09 -07:00
|
|
|
items: (_, { args, toReference }) => {
|
|
|
|
return args.ids.map((id) =>
|
|
|
|
toReference({ __typename: "Item", id }, true)
|
|
|
|
);
|
|
|
|
},
|
2020-05-14 15:51:08 -07:00
|
|
|
|
2020-07-31 23:10:34 -07:00
|
|
|
// Teach Apollo how to serve `petAppearance` queries from the cache. That
|
|
|
|
// way, when you switch pet poses, Apollo knows it already has the
|
|
|
|
// appearance data and doesn't need to ask the server again!
|
|
|
|
petAppearance: (_, { args, toReference }) => {
|
|
|
|
const { speciesId, colorId, pose } = args;
|
|
|
|
const id = `${speciesId}-${colorId}-${pose}`;
|
2020-07-31 23:21:09 -07:00
|
|
|
return toReference({ __typename: "PetAppearance", id }, true);
|
2020-07-31 23:10:34 -07:00
|
|
|
},
|
2020-05-14 15:51:08 -07:00
|
|
|
},
|
|
|
|
},
|
2020-08-19 17:50:05 -07:00
|
|
|
|
|
|
|
Zone: {
|
|
|
|
fields: {
|
|
|
|
depth: (depth, { readField }) => {
|
2020-08-19 18:01:20 -07:00
|
|
|
const id = readField("id");
|
|
|
|
return depth || cachedZonesById.get(id)?.depth || 0;
|
2020-08-19 17:50:05 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
label: (label, { readField }) => {
|
2020-08-19 18:01:20 -07:00
|
|
|
const id = readField("id");
|
|
|
|
return label || cachedZonesById.get(id)?.label || `Zone #${id}`;
|
2020-08-19 17:50:05 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-14 15:51:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// The PersistedQueryLink in front of the HttpLink helps us send cacheable GET
|
|
|
|
// requests.
|
|
|
|
const persistedQueryLink = createPersistedQueryLink({
|
|
|
|
useGETForHashedQueries: true,
|
|
|
|
});
|
|
|
|
const httpLink = createHttpLink({ uri: "/api/graphql" });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* apolloClient is the global Apollo Client instance we use for GraphQL
|
|
|
|
* queries. This is how we communicate with the server!
|
|
|
|
*/
|
|
|
|
export default new ApolloClient({
|
|
|
|
link: persistedQueryLink.concat(httpLink),
|
2020-07-31 23:10:34 -07:00
|
|
|
cache: new InMemoryCache({ typePolicies }),
|
2020-05-14 15:51:08 -07:00
|
|
|
});
|