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

Missing Type and Generator for Mutation Payload #42

Closed
arnonate opened this issue Apr 9, 2021 · 10 comments · Fixed by #57
Closed

Missing Type and Generator for Mutation Payload #42

arnonate opened this issue Apr 9, 2021 · 10 comments · Fixed by #57

Comments

@arnonate
Copy link

arnonate commented Apr 9, 2021

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
Copy link
Owner

ardeois commented Apr 13, 2021

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
Copy link
Author

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
Copy link
Owner

ardeois commented Apr 13, 2021

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

@djasnowski
Copy link

djasnowski commented Jul 12, 2021

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
Copy link
Owner

ardeois commented Jul 12, 2021

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

@djasnowski
Copy link

@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
Copy link
Owner

ardeois commented Jul 12, 2021

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

if (typeName === 'Query' || typeName === 'Mutation') {
return null;
}

@djasnowski
Copy link

djasnowski commented Jul 12, 2021

@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
Copy link
Author

arnonate commented Jul 14, 2021

@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 added a commit that referenced this issue Sep 7, 2021
While these types should not be used very often, it can
happen another mock references a `Query` or `Mutation` type.
Which means we'll declare a property value with `aQuery` or
`aMutation`

That's why we need to define those types

Fixes #42
ardeois added a commit that referenced this issue Sep 7, 2021
While these types should not be used very often, it can
happen another mock references a `Query` or `Mutation` type.
Which means we'll declare a property value with `aQuery` or
`aMutation`

That's why we need to define those types

Fixes #42
@ardeois
Copy link
Owner

ardeois commented Sep 7, 2021

@arnonate fixed in release 1.7.0

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

Successfully merging a pull request may close this issue.

3 participants