Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codegen add __typename recipe #26

Closed
n1ru4l opened this issue Jul 24, 2023 · 3 comments
Closed

Codegen add __typename recipe #26

n1ru4l opened this issue Jul 24, 2023 · 3 comments

Comments

@n1ru4l
Copy link

n1ru4l commented Jul 24, 2023

Regarding the following:

A lot of GraphQL clients use `__typename` for their caches, the issue here being that we have to add `__typename` to each
`SelectionSet` we do in a document (or we add an extra plugin to `graphql-codegen` to do this for us.), otherwise our
client-side cache could start missbehaving, in Relay this is done for us by the optimising compiler.

It would be nice to add the following tod document it 😇

import { Kind, visit } from 'graphql';
import { Types } from '@graphql-codegen/plugin-helpers';

export const addTypenameDocumentTransform: Types.DocumentTransformObject = {
  transform({ documents }) {
    return documents.map(document => ({
      ...document,
      document: document.document
        ? visit(document.document, {
            SelectionSet(node) {
              if (
                !node.selections.find(
                  selection => selection.kind === 'Field' && selection.name.value === '__typename',
                )
              ) {
                return {
                  ...node,
                  selections: [
                    {
                      kind: Kind.FIELD,
                      name: {
                        kind: Kind.NAME,
                        value: '__typename',
                      },
                    },
                    ...node.selections,
                  ],
                };
              }
            },
          })
        : undefined,
    }));
  },
};
import { addTypenameDocumentTransform } from './configs/graphql-code-generator/add-typename-document-transform.mjs';
import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: "YOUR_GRAPHQL_ENDPOINT",
  documents: ["./**/*.{ts,tsx}"],
  ignoreNoDocuments: true,
  generates: {
    "./gql/": {
      preset: "client",
      plugins: [],
      presetConfig: {
        persistedDocuments: true,
      },
      documentTransforms: [addTypenameDocumentTransform],
    },
  },
};

export default config;
@JoviDeCroock
Copy link
Owner

Oh awesome, thanks! I will add it in a gist and link to it from that paragraph

@n1ru4l
Copy link
Author

n1ru4l commented Jul 24, 2023

We will also add this document transform to the client preset and document it on the codegen website.

@n1ru4l
Copy link
Author

n1ru4l commented Jul 24, 2023

@n1ru4l n1ru4l closed this as completed Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants