urql-exhaustive-additional-typenames-exchange
add all list fields in an operation to additionalTypenames.
This is useful if your project is more about avoiding the risk of bugs from cache operations than cache efficiency.
When working with the document cache, you need to consider which types to add to additionalTypenames. As mentioned in Document Cache Gotchas, in situations where the response data for a list field is empty, that type should be added. However, attempting to do this rigorously can make it difficult to enumerate the type correctly, as it is often only apparent at runtime. Therefore, the basic policy for this custom exchange is to "add all list fields in the operation to additionalTypenames". This approach may reduce cache efficiency, but we do not see this as a problem if the priority is to minimize the risk of bugs.
related: urql-graphql/urql#3440
pnpm add urql-exhaustive-additional-typenames-exchange
import { Client, cacheExchange, fetchExchange } from "urql";
import { exhaustiveAdditionalTypenamesExchange } from "urql-exhaustive-additional-typenames-exchange";
import schema from "./generated/minified.json";
const client = new Client({
url: "http://localhost:3000/graphql",
exchanges: [
cacheExchange,
exhaustiveAdditionalTypenamesExchange({ schema }),
fetchExchange,
],
});
You may have noticed that schema
is passed. This exchange requires a schema definition to identify the types included in the operation at runtime.
It is similar to that used in Schema Awareness in GraphCache.
Here is how to get a minified schema using GraphQL Code Generator and the @urql/introspection package provided by urql.
pnpm add -D @graphql-codegen/cli @graphql-codegen/introspection @urql/introspection
//lib/minifyIntrospection.mjs
#!/usr/bin/env node
import { minifyIntrospectionQuery } from '@urql/introspection'
import * as fs from 'fs'
const main = () => {
const json = fs.readFileSync('./generated/introspection.json', 'utf8')
const minified = minifyIntrospectionQuery(JSON.parse(json))
fs.writeFileSync('./generated/minified.json', JSON.stringify(minified))
}
main()
// codegen.ts
import type { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: `http://localhost:3000/graphql`,
generates: {
["/generated/introspection.json"]: {
plugins: ["introspection"],
config: {
minify: true,
},
hooks: {
afterOneFileWrite: ["node lib/minifyIntrospection.mjs"],
},
},
},
};
export default config;
Input | Description |
---|---|
schema | A serialized GraphQL schema that is used by detect list fields. |
debug | If true, the detected list fields will be logged to the console. |
If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.
MIT