Search code examples
graphql-codegen

GraphQL Codegen duplicates RegisterDocument with typescript-urql


The codegen.ts config below results in duplicating the RegisterDocument entries.

codegen.ts:

const config: CodegenConfig = {
  overwrite: true,
  schema: "http://localhost:4000/graphql",
  documents: "src/graphql/**/*.graphql",
  generates: {
    "src/generated/graphql": {
      preset: "client",
      plugins: [
        "typescript-urql"
      ],
      config: {
        documentVariableSuffix: 'test2'
      }
    }
  }
};

the output:

export const RegisterDocument = {"kind":"Document", ...}

export const RegisterDocument = gql`
    mutation Register($username: String!, $password: String!) {
  register(options: {username: $username, password: $password}) {
    errors {
      field
      message
    }
    user {
      id
      username
      createdAt
    }
  }
}
    `;

export function useRegisterMutation() {
  return Urql.useMutation<RegisterMutation, RegisterMutationVariables>(RegisterDocument);
};

Seemingly either the documentVariableSuffix param didn't affect the output const naming or it was a wrong param. The use of the typescript-operations or/and typescript packages only led to more duplicates.

What is the way to have typescript-urql register the mutation differently?

UP. The register mutation I need typings for:

const registerMutationDocument = graphql(`
  mutation Register($username: String!, $password: String!) {
    register(options: { username: $username, password: $password }) {
      errors {
        field
        message
      }
      user {
        id
        username
        createdAt
      }
    }
  }
`)

Solution

  • After a few attempts with various code pieces it seems I've got it to work. Thank you mr. Poly for the hints.

    Here's the take. First, the present iteration of graphql-codegen watches for .ts/.tsx documents not *.graphql ones. Second the only plugins needed are the ones listed in the docs.

    const config: CodegenConfig = {
      overwrite: true,
      schema: "http://localhost:4000/graphql",
      documents: "src/graphql/mutations/*.ts",
      generates: {
        "src/generated/graphql/": {
          preset: "client",
          plugins: []
        }
      }
    };
    

    Third the way to put the mutations etc. to a dedicated folder that I used was to create one at src/graphql/mutations. The register.ts holding the mutation had the following code:

    import { graphql } from '../../generated/graphql';
    
    export const registerMutationDocument = graphql(`
      mutation Register($username: String!, $password: String!) {
        register(options: { username: $username, password: $password }) {
          errors {
            field
            message
          }
          user {
            id
            username
            createdAt
          }
        }
      }
    `);
    

    The only problem for me was if I tried to add a field to it that didn't exist on the model the editor froze. The usage within the component:

    import { registerMutationDocument } from '../graphql/mutations/register';
    ...
    const [, register] = useMutation(registerMutationDocument);