GraphQL 代码生成器 - 无法从 Nestjs 服务器端点加载我的 GQL 架构

Posted

技术标签:

【中文标题】GraphQL 代码生成器 - 无法从 Nestjs 服务器端点加载我的 GQL 架构【英文标题】:GraphQL Code Generator - Cannot load my GQL Schema from Nestjs server endpoint 【发布时间】:2020-06-18 02:52:48 【问题描述】:

我在 Angular 应用程序上使用 graphQL code generator,我正在尝试从本地 nestjs 应用程序加载我的架构。

这里是 codegen.yml 文件:

schema: http://localhost:3000/graphql
documents: ./src/app/graphql/**/*.graphql
generates:
  ./src/app/graphql/types.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-apollo-angular

当我运行 graphql-codegen 时,我收到以下错误:

 Failed to load schema from http://localhost:3000/graphql:

        invalid json response body at http://localhost:3000/graphql reason: Unexpected token < in JSON at position 0
        FetchError: invalid json response body at http://localhost:3000/graphql reason: Unexpected token < in JSON at position 0
    at C:\ngWishList2\node_modules\node-fetch\lib\index.js:271:32
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async UrlLoader.load (C:\ngWishList2\node_modules\@graphql-toolkit\url-loader\index.cjs.js:87:22)
    at async C:\ngWishList2\node_modules\@graphql-toolkit\core\index.cjs.js:682:25        
    at async Promise.all (index 5)
    at async loadSingleFile (C:\ngWishList2\node_modules\@graphql-toolkit\core\index.cjs.js:678:5)
    at async C:\ngWishList2\node_modules\@graphql-toolkit\core\index.cjs.js:556:38        
    at async Promise.all (index 0)
    at async loadTypedefs (C:\ngWishList2\node_modules\@graphql-toolkit\core\index.cjs.js:620:5)
    at async Object.loadSchema (C:\ngWishList2\node_modules\@graphql-toolkit\core\index.cjs.js:705:21)
    at async loadSchema (C:\ngWishList2\node_modules\@graphql-codegen\cli\bin.js:342:24)  
    at async C:\ngWishList2\node_modules\@graphql-codegen\cli\bin.js:704:55
    at async Task.task (C:\ngWishList2\node_modules\@graphql-codegen\cli\bin.js:567:17)   

        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 (C:\ngWishList2\node_modules\@graphql-codegen\cli\bin.js:353:15)    
        at processTicksAndRejections (internal/process/task_queues.js:94:5)
    Error: Failed to load schema
        at loadSchema (C:\ngWishList2\node_modules\@graphql-codegen\cli\bin.js:353:15)    
        at processTicksAndRejections (internal/process/task_queues.js:94:5)

当我记录响应时,我看到它试图解析以下内容,这解释了错误:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /graphql</pre>
</body>
</html>

如果我在 index.cjs.cs 中挖掘,并记录生成的 POST 正文和响应,我会得到:

身体:

"query":"query IntrospectionQuery \n  __schema \n    queryType \n      name\n    \n  
  mutationType \n      name\n    \n    subscriptionType \n      name\n    \n    types 
\n      ...FullType\n    \n    directives \n      name\n      description\n      locations\n      args \n        ...InputValue\n      \n    \n  \n\n\nfragment FullType on __Type \n  kind\n  name\n  description\n  fields(includeDeprecated: true) \n    name\n   
 description\n    args \n      ...InputValue\n    \n    type \n      ...TypeRef\n    \n    isDeprecated\n    deprecationReason\n  \n  inputFields \n    ...InputValue\n  \n  
interfaces \n    ...TypeRef\n  \n  enumValues(includeDeprecated: true) \n    name\n    
description\n    isDeprecated\n    deprecationReason\n  \n  possibleTypes \n    ...TypeRef\n  \n\n\nfragment InputValue on __InputValue \n  name\n  description\n  type \n    
...TypeRef\n  \n  defaultValue\n\n\nfragment TypeRef on __Type \n  kind\n  name\n  ofType \n    kind\n    name\n    ofType \n      kind\n      name\n      ofType \n        kind\n        name\n        ofType \n          kind\n          name\n          ofType \n  
          kind\n            name\n            ofType \n              kind\n
name\n              ofType \n                kind\n                name\n              \n            \n          \n        \n      \n    \n  \n\n","variables":,"operationName":"IntrospectionQuery"

回应:

Response 
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: 
    body: PassThrough 
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: false,
      allowHalfOpen: true,
      _transformState: [Object]
    ,
    disturbed: false,
    error: null
  ,
  [Symbol(Response internals)]: 
    url: 'http://localhost:3000/graphql',
    status: 404,
    statusText: 'Not Found',
    headers: Headers  [Symbol(map)]: [Object: null prototype] ,
    counter: 0
  

如果我的端点不正确,那将是有道理的,但如果我在邮递员中尝试那个确切的请求,我会得到一个有效的响应:

关于为什么会这样的任何想法?还有什么我可以检查的吗?

谢谢

【问题讨论】:

【参考方案1】:

我在 codegen.yml 文件中设置了错误的 schema: 路径。确保它的路径正确。

【讨论】:

【参考方案2】:

您的 graphql 服务器正在运行吗? 我有同样的问题,我发现在运行命令之前我没有运行我的服务器

yarn gen

【讨论】:

【参考方案3】:

这可能是由不正确的Content-Type 或编码引起的。这个问题在某处有重现吗?是复制它的回购吗?

【讨论】:

以上是关于GraphQL 代码生成器 - 无法从 Nestjs 服务器端点加载我的 GQL 架构的主要内容,如果未能解决你的问题,请参考以下文章

使用 GraphQL 代码生成器从解析器返回不完整的形状

angular 的 Apollo 客户端:从 graphql 文件生成代码

使用graphql代码生成器时如何将自定义标量类型映射到打字稿类型?

如何从 GraphQL 生成 Dart - graphql 到 dart 生成器

GitHub 的 GraphQL API 如何从 Java 中使用?

从 prisma 生成的客户端访问 graphql 服务器中的数据类型