5

我在 Angular 应用程序上使用graphQL 代码生成器,我正在尝试从本地 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
  }
}

如果我的端点不正确,那将是有道理的,但是如果我在邮递员中尝试该确切请求,我确实会得到一个有效的响应: 在此处输入图像描述

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

谢谢

4

3 回答 3

1

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

yarn gen
于 2021-05-19T15:10:34.557 回答
0

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

于 2020-03-22T20:56:43.323 回答
0

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

于 2021-12-23T00:53:04.000 回答