I think I got all up in my head about direct queries for this one, because of a previous implementation I had in mind, and I forgot that I could just query species and color from the cache by reference without breaking out of the API provided to the cache function! I also learned in here that I _can_ look up things from the root by doing `readField("allSpecies", {__ref: "ROOT_QUERY"})`, which I struggled to figure out my previous time. I couldn't figure out how to read an uncached field with arguments (I couldn't quite figure out how to build a proper FieldNode, and passing the string form seemed to provide `null` to the `species` cache field reader), but it's probably doable!
127 lines
4 KiB
JavaScript
127 lines
4 KiB
JavaScript
import { ApolloClient, createHttpLink, InMemoryCache } from "@apollo/client";
|
|
import { setContext } from "@apollo/client/link/context";
|
|
import { createPersistedQueryLink } from "apollo-link-persisted-queries";
|
|
|
|
import cachedZones from "./cached-data/zones.json";
|
|
|
|
// Teach Apollo to load certain fields from the cache, to avoid extra network
|
|
// requests. This happens a lot - e.g. reusing data from item search on the
|
|
// outfit immediately!
|
|
const typePolicies = {
|
|
Query: {
|
|
fields: {
|
|
allZones: (_, { toReference }) => {
|
|
return cachedZones.map((z) =>
|
|
toReference({ __typename: "Zone", id: z.id }, true)
|
|
);
|
|
},
|
|
items: (_, { args, toReference }) => {
|
|
return args.ids.map((id) =>
|
|
toReference({ __typename: "Item", id }, true)
|
|
);
|
|
},
|
|
item: (_, { args, toReference }) => {
|
|
return toReference({ __typename: "Item", id: args.id }, true);
|
|
},
|
|
petAppearanceById: (_, { args, toReference }) => {
|
|
return toReference({ __typename: "PetAppearance", id: args.id }, true);
|
|
},
|
|
species: (_, { args, toReference }) => {
|
|
return toReference({ __typename: "Species", id: args.id }, true);
|
|
},
|
|
color: (_, { args, toReference }) => {
|
|
return toReference({ __typename: "Color", id: args.id }, true);
|
|
},
|
|
},
|
|
},
|
|
|
|
Item: {
|
|
fields: {
|
|
appearanceOn: (appearance, { args, readField, toReference }) => {
|
|
// If we already have this exact appearance in the cache, serve it!
|
|
if (appearance) {
|
|
return appearance;
|
|
}
|
|
|
|
// Otherwise, we're going to see if this is a standard color, in which
|
|
// case we can reuse the standard color appearance if we already have
|
|
// it! This helps for fast loading when switching between standard
|
|
// colors.
|
|
const { speciesId, colorId } = args;
|
|
const speciesStandardBodyId = readField(
|
|
"standardBodyId",
|
|
toReference({ __typename: "Species", id: speciesId })
|
|
);
|
|
const colorIsStandard = readField(
|
|
"isStandard",
|
|
toReference({ __typename: "Color", id: colorId })
|
|
);
|
|
if (speciesStandardBodyId == null || colorIsStandard == null) {
|
|
// This is an expected case while the page is loading.
|
|
return null;
|
|
}
|
|
|
|
if (colorIsStandard) {
|
|
const itemId = readField("id");
|
|
return toReference({
|
|
__typename: "ItemAppearance",
|
|
id: `item-${itemId}-body-${speciesStandardBodyId}`,
|
|
});
|
|
} else {
|
|
return undefined;
|
|
}
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
// The PersistedQueryLink in front of the HttpLink helps us send cacheable GET
|
|
// requests.
|
|
const persistedQueryLink = createPersistedQueryLink({
|
|
useGETForHashedQueries: true,
|
|
});
|
|
const httpLink = createHttpLink({ uri: "/api/graphql" });
|
|
const buildAuthLink = (getAuth0) =>
|
|
setContext(async (_, { headers }) => {
|
|
// Wait for auth0 to stop loading, so we can maybe get a token! We'll do
|
|
// this hackily by checking every 100ms until it's true.
|
|
await new Promise((resolve) => {
|
|
function check() {
|
|
if (getAuth0().isLoading) {
|
|
setTimeout(check, 100);
|
|
} else {
|
|
resolve();
|
|
}
|
|
}
|
|
check();
|
|
});
|
|
|
|
const { isAuthenticated, getAccessTokenSilently } = getAuth0();
|
|
if (isAuthenticated) {
|
|
const token = await getAccessTokenSilently();
|
|
return {
|
|
headers: {
|
|
...headers,
|
|
authorization: token ? `Bearer ${token}` : "",
|
|
},
|
|
};
|
|
}
|
|
});
|
|
|
|
const initialCache = {};
|
|
for (const zone of cachedZones) {
|
|
initialCache[`Zone:${zone.id}`] = { __typename: "Zone", ...zone };
|
|
}
|
|
|
|
/**
|
|
* apolloClient is the global Apollo Client instance we use for GraphQL
|
|
* queries. This is how we communicate with the server!
|
|
*/
|
|
const buildClient = (getAuth0) =>
|
|
new ApolloClient({
|
|
link: buildAuthLink(getAuth0).concat(persistedQueryLink).concat(httpLink),
|
|
cache: new InMemoryCache({ typePolicies }).restore(initialCache),
|
|
connectToDevTools: true,
|
|
});
|
|
|
|
export default buildClient;
|