ardeois / graphql-codegen-typescript-mock-data

[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.
MIT License
132 stars 47 forks source link

Error when using @client field #132

Closed mickey8121 closed 12 months ago

mickey8121 commented 1 year ago

I get an error when I try to generate mocks for a scheme that has a client field:

[FAILED] GraphQL Document Validation failed with 1 errors;
[FAILED]   Error 0: Cannot query field "fieldName" on type "TypeName".

Example of a query that causes an error:

gql`
  query queryName(
    $pagination: Pagination
    $filter: Filter!
    $orderBy: Order
  ) {
    queryName(pagination: $pagination, filter: $filter, orderBy: $orderBy) {
      records {
        fieldName @client
      }
    }
  }
`;

Codegen does this without errors and this field goes into the generated.ts file and if I remove this field from the query, the mocks is generated well

codegen.config.js:

const endpoint = require('./src/defineEndpoint').defineEndpoint();

/** @type {import('graphql-config').IGraphQLConfig } */
module.exports = {
  schema: [endpoint],
  documents: ['src/requests/**/*.ts'],
  extensions: {
    codegen: {
      generates: {
        'src/graphql/generated.ts': {
          schema: 'src/graphql/client_extend.gql', // this @client field is identified here
          plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
          config: {
            withHooks: true,
            maybeValue: 'T',
            inputMaybeValue: 'T | undefined | null',
            defaultScalarType: 'string',
          },
        },
        'src/mocks/generated-mocks.ts': {
          plugins: {
            'typescript-mock-data': {
              typesFile: '../graphql/generated.ts',
              addTypename: true,
              terminateCircularRelationships: true,
              generateLibrary: 'faker',
              scalars: {
                'Datetime': {
                  generator: 'date.recent',
                },
                'IntId': {
                  generator: 'random.numeric'
                },
              },
            },
          },
        },
      },
    },
  },
};
emab commented 1 year ago

To fix this we could try and bump our codegen helper version - this error stems from the plugin trying to parse the GQL - if the first generation can do that, we shouldn't have the issue.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.