Graphql-code-generator: Custom scalars inside a fragment are turned into `any` when `preResolveTypes: true`

Created on 5 Oct 2020  路  7Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
Custom scalars inside a fragment are turned into any when preResolveTypes: true

To Reproduce
Check live: https://codesandbox.io/s/elegant-haibt-30ppy

Steps to reproduce the behavior:

  1. My GraphQL schema:
scalar Date

type Query {
    user(id: ID!): User!
}

type User {
    id: ID!
    username: String!
    email: String!
    createdAt: Date!
}
  1. My GraphQL operations:
query user {
    user(id: 1) {
        ...UserFragment
    }
}

fragment UserFragment on User {
    username
    email
    createdAt
}
  1. My codegen.yml config file:
schema: schema.graphql
documents: document.graphql
generates:
  types.ts:
    plugins:
      - typescript:
          scalars:
            Date: string
      - typescript-operations:
          preResolveTypes: true

Expected behavior
UserFragmentFragment['createdAt'] should be Scalars['Date'], but it is any

Environment:

  • OS: macOS 10.15.6
  • @graphql-codegen/...:

    "@graphql-codegen/cli": "^1.17.10",
    "@graphql-codegen/typescript": "^1.17.10",
    "@graphql-codegen/typescript-operations": "^1.17.8",

  • NodeJS: 12.16.1

bug plugins

Most helpful comment

@cevr it did work for me, the issue was solved following his suggestion.
@dotansimha thank you very much!

All 7 comments

@CarlosGines you are using the per-plugin configuration, so scalars config is only available for typescript plugin and not for the typescript-operations.

Please use:

schema: schema.graphql
documents: document.graphql
generates:
  types.ts:
    config:
      scalars:
        Date: string
    plugins:
      - typescript
      - typescript-operations:
          preResolveTypes: true

This happens even when the config is outside the plugin

@cevr just noticed it, re-opening. Thanks

@cevr it did work for me, the issue was solved following his suggestion.
@dotansimha thank you very much!

@CarlosGines yeah you are right, rerunning worked, it guess it was something with the sandbox. Closing :)
@cevr if you are still having issues with that, can you please report a new issue, with a reproduction? Thanks!

Ah okay I see the problem,

My use case was that in a monorepo, I had one repo that held the schema types, and one repo that held the operations and apollo hooks.

I only declared the scalars in the schema type repo, but hadn't declared them in the other.

Declaring the scalars on both configs fixed the issue.

@dotansimha if the import-types preset is used, should it attempt to import before defaulting to any if no scalars are present in the config?

EDIT: here's a sandbox simulating the issue: https://codesandbox.io/s/suspicious-sound-9v0on?file=/codegen.yml

scalars should be under config. Maybe that's the problem;
See;
https://codesandbox.io/s/purple-wave-v08j9?file=/codegen.yml

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leebenson picture leebenson  路  3Comments

steebchen picture steebchen  路  3Comments

edorivai picture edorivai  路  3Comments

iamdanthedev picture iamdanthedev  路  3Comments

mdaouas picture mdaouas  路  3Comments