A compatibility wrapper that provides the react-relay API on top of Apollo Client.
-
Configure Apollo Client's cache to automatically add __typename
field selections, which concrete types implement the Node
interface, and the type-policies needed to read the watch query data from the store:
import { InMemoryCache } from "@apollo/client";
import { typePolicies } from "@graphitation/apollo-react-relay-duct-tape";
const cache = new InMemoryCache({
addTypename: true,
possibleTypes: {
Node: ["Todo"],
},
typePolicies: {
Query: {
fields: {
__fragments: {
read: typePolicies.Query.fields.__fragments.read,
},
node: {
read: typePolicies.Query.fields.node.read,
},
},
},
Node: {
fields: {
__fragments: {
read: typePolicies.Node.fields.__fragments.read,
},
},
},
},
});
-
Configure webpack to transform your code by replacing inline GraphQL documents with their compiled artefacts:
const {
createImportDocumentsTransform,
} = require("@graphitation/apollo-react-relay-duct-tape/lib/storeObservation/createImportDocumentsTransform");
const config: webpack.Configuration = {
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/,
options: {
getCustomTransformers: () => ({
before: [createImportDocumentsTransform()],
}),
},
},
],
},
};
-
TODO: Have a Node
interface definition
-
TODO: Add node
root field
-
Optionally, if you rely on Apollo Client's @client
directive, be sure to explicitly add it to your local copy of your schema, otherwise the compiler will not accept its use.
directive @client(always: Boolean) on FIELD
In a shell, start the compiler and point it to your schema and source. Depending on the size of the code-base a first run may take a while, but subsequent builds should cached. For developer-expedience, it is advised to run the compiler using the watch mode -- provided you have installed the watchman
tool.