Graphql-code-generator: How to handle schema with string templates in typescript/javascript exports

Created on 1 Mar 2020  Â·  5Comments  Â·  Source: dotansimha/graphql-code-generator

Describe the bug
if there is string template in the exported scheme, it will complain the syntax and seems it will not compile it. Check the following files for more details.

To Reproduce
Please check the repo for all the files and reproduce steps.

  1. My GraphQL schema:
// types.js
const gql = require("graphql-tag");

const Colors = ["Red", "Yellow"];
export default gql`
  type Person {
    name: String
  }
  enum Color {
      ${Colors.join("\n")}
  }
# if we change the above enum to be the below, it will be good
#   enum Color {
#       Red
#       Yellow
#   }
`;
  1. My codegen.yml config file:
schema: 
  - types.js

generates:
  generated.ts:
    plugins:
        - typescript
        - typescript-resolvers

Expected behavior
It should run successfully, but it complains the following:

Found 1 error

  ✖ generated.ts
    Failed to load schema from types.js:

        Syntax Error: Expected Name, found }
        GraphQLError: Syntax Error: Expected Name, found }
    at syntaxError (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/error/syntaxError.js:15:10)
    at Parser.expectToken (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:1404:40)
    at Parser.parseName (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:94:22)
    at Parser.parseEnumValueDefinition (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:1014:21)
    at Parser.optionalMany (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:1497:28)
    at Parser.parseEnumValuesDefinition (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:1002:17)
    at Parser.parseEnumTypeDefinition (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:986:23)
    at Parser.parseTypeSystemDefinition (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:705:23)
    at Parser.parseDefinition (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:146:23)
    at Parser.many (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:1518:26)
    at Parser.parseDocument (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:111:25)
    at Object.parse (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/graphql/language/parser.js:36:17)
    at Object.parseGraphQLSDL (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/@graphql-toolkit/common/index.cjs.js:572:28)
    at CodeFileLoader.load (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/@graphql-toolkit/code-file-loader/index.cjs.js:120:31)
    at async /Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/@graphql-toolkit/core/index.cjs.js:682:25
    at async Promise.all (index 0)

        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export "schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
          - String in config file

        Try to use one of above options and run codegen again.
    Error: Failed to load schema
        at loadSchema (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/@graphql-codegen/cli/bin.js:353:15)
    Error: Failed to load schema
        at loadSchema (/Users/rliu/study/reproduce-graphql-codegen-with-string-template-in-js-export/node_modules/@graphql-codegen/cli/bin.js:353:15)

Environment:

  • OS: mac 10.15.2
  • @graphql-codegen/cli: 1.12.2
  • @graphql-codegen/typescript: 1.12.2
  • @graphql-codegen/typescript-resolvers: 1.12.2
  • NodeJS: 13.9

Additional context

bug dependencies

All 5 comments

It's an issue with graphql-toolkit, I think it should try to require the file when code ast lookup fails, @ardatan what do you think?

@ardatan I tried the latest codegen version, seems the issue still there. Could you have a look further, thank you.

My bad. You need to add noPluck: true configuration option.

schema: 
  - types.js:
        noPluck: true

generates:
  generated.ts:
    plugins:
        - typescript
        - typescript-resolvers

@ron-liu I'm closing. noPluck solves that :) Let me know if it's still causes issues for you.

Was this page helpful?
0 / 5 - 0 ratings