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
133 stars 47 forks source link

Missing Type and Generator for Mutation Payload #42

Closed arnonate closed 2 years ago

arnonate commented 3 years ago

Hello! Thanks for the script. I am seeing one issue with my schema. Mutation payloads have an optional query field that is not getting generated in the output mocks file. See screenshot below:

Screen Shot 2021-04-09 at 5 30 11 PM

Screen Shot 2021-04-09 at 5 30 30 PM

If you can point me in the right direction I will happily contribute a solution. I am just not sure if I have my codegen options set correctly or if this will require a pull request.

schema:
  - 'schema.grapql':
      headers:
        Authorization: 'Bearer ${AUTH_TOKEN}'
  - 'src/graphql/schema.local.graphql'
documents:
  - ./src/**/*.tsx
  - ./src/**/*.ts
  - ./src/**/*.graphql
overwrite: true
watch: true
generates:
  './src/generated/graphql.tsx':
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-apollo
    config:
      skipTypename: false
      withHooks: true
      withHOC: false
      withComponent: false
      withRefetchFn: true
      dedupeOperationSuffix: true
      futureProofEnums: true
      immutableTypes: true
      reactApolloVersion: 3
      # maybeValue: T | null | undefined
  'src/generated/mocks.ts':
    plugins:
      - typescript-mock-data:
          typesFile: './graphql.tsx'
          enumValues: pascal-case#pascalCase
          terminateCircularRelationships: true
          addTypename: true
          typenames: keep
          scalars:
            Date: date
            Datetime: date
ardeois commented 3 years ago

Hi, Thanks for the report

I'm not sure to understand your issue, but that's probably a bug on our side.

It's probably related to this if that skips the generation of mock data for Query and Mutation types

arnonate commented 3 years ago

Ha! That looks like the culprit for sure! For now I just wrote a small codegen plugin that writes the missing import and type that I need. Is there a reason to skip Query and Mutation types?

ardeois commented 3 years ago

Yeah because it didn't make sense to create a mock aQuery or aMutation For me, you need to create a mock for a specific query, you don't want to mock the whole Query object

So maybe it's the generation of aQuery() that don't make sense? not sure

naknode commented 3 years ago

Ha! That looks like the culprit for sure! For now I just wrote a small codegen plugin that writes the missing import and type that I need. Is there a reason to skip Query and Mutation types?

Care to share what you've learned? I, too, am getting this issue and i understand why it's being omitted as what @ardeois stated. How do we go about fixing this?

ardeois commented 3 years ago

@naknode Can you share your use case as of why you would need aQuery ?

naknode commented 3 years ago

@naknode Can you share your use case as of why you would need aQuery ?

@ardeois Sorry, I miswrote. I do not need aQuery. As you states, it is not needed for practical purposes. How should I proceed with eliminating a bunch of Cannot find name aQuery errors.

Great library, btw!

ardeois commented 3 years ago

Ah sorry I understand your issue now. This should be fixed in this plugin. We would probably need to property import Query when it's used as a type, but don't generate aQuery

Or the easiest way is to keep generating aQuery and aMutation as it indeed can be used as an output type (as @arnonate shows in his example)

You can do a PR to update this logic https://github.com/ardeois/graphql-codegen-typescript-mock-data/blob/3d2e6791b9ab762540f48037d611b53cf2930320/src/index.ts#L391-L393

naknode commented 3 years ago

@ardeois I am on the latest 1.5.7. so I should already have the PR you are requesting. I see it in my local node_modules, too. So this should already be fixed, no?

Can you show me where @arnonate is using it as a output type? I'm not an expert-level GraphQLer but I understand the fundamentals. :-) Thank you

Also, it keeps generating imports of UUID and JSON filters as UUIDFilter and JSONFilter, is there anyway to correct the import through this plugin's config?

Thanks again!

arnonate commented 3 years ago

@naknode I just wrote a small plugin that appends this to the output:

// codedgen-plugin.js

module.exports = {
  plugin: (schema, documents, config) => {
    return `// Custom codegen-plugin output
import { Query } from './graphql';
const aQuery = (overrides: Partial<Query>, omit: Set<string>): Query | null | undefined => overrides ? overrides as Query : undefined;`;
  },
};

It's not a perfect solution, but I am not actually mocking aQuery anywhere, just needed to get rid of the errors. Here is the plugin in my .graphqlrc:

  'src/generated/mocks.ts':
    plugins:
      - typescript-mock-data:
          typesFile: './graphql.tsx'
          enumValues: pascal-case#pascalCase
          terminateCircularRelationships: true
          addTypename: true
          typenames: keep
          scalars:
            Date: date
            Datetime:
              generator: date
              arguments: 'YYYY-MM-DDTHH:mm:ss.SSS\Z'
      - codegen-plugin.js
ardeois commented 2 years ago

@arnonate fixed in release 1.7.0