首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在typescript/javascript导出中使用字符串模板处理graphql-codegen模式

如何在typescript/javascript导出中使用字符串模板处理graphql-codegen模式
EN

Stack Overflow用户
提问于 2020-02-28 05:37:39
回答 1查看 1.9K关注 0票数 2

我使用graphql-codegen生成给定模式的类型文件。

除了在导出的方案中有字符串模板之外,所有这些都是好的,它会抱怨sytax并似乎不会编译它。有关详细信息,请查看下列文件。

types.js如下:

代码语言:javascript
复制
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
#   }
`;

而config yml文件是:

代码语言:javascript
复制
schema: 
  - types.js

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

当运行yarn graphql:codegen时,它会抱怨如下:

代码语言:javascript
复制
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)

看起来graphql-codegen不喜欢模板字符串,比如:${Colors.join('\n')}

另外,请查看包含上述所有文件的存储库

谁能帮上忙?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-03-22 21:00:34

它没有处理它,主要是因为加载代码文件和插值代码文件的复杂性。但是,解决办法是:

  • 创建一个schema.js文件。
  • 从源文件导入带有字符串内插的所有类型,并使用buildSchema (来自graphql)或makeExecutableSchema (来自graphql-tools)构建GraphQLSchema对象实例。
  • GraphQLSchema导出为默认导出,或作为名为schema的标识符导出。
  • 将该文件提供给codegen (通过执行schema: ./schema.js --使用单个代码文件会使编码元查找ast代码,然后尝试对其执行require )。

如果您使用的是TypeScript,您还应该在编码元中添加require扩展(参见https://graphql-code-generator.com/docs/getting-started/require-field#typescript-support)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60445841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档