GraphQL Code Generator - Cannot load my GQL Schema from Nestjs server endpoint
Asked Answered
R

5

6

I'm using graphQL code generator on an angular app and I'm trying to load my schema from a local nestjs app.

Here is the codegen.yml file:

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

When I run graphql-codegen, I'm getting the following error:

 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)

When I log the response, I see that its trying to parse the following, which explains the error:

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

If I dig in the index.cjs.cs, and log the generated POST body and response, I'm getting:

BODY:

{"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:

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
  }
}

That would make sense if my endpoint was incorrect, but if I try that exact request in postman, I do get a valid response: enter image description here

Any ideas on why is that? Anything else I could check?

Thanks

Response answered 5/3, 2020 at 12:58 Comment(0)
T
2

is your graphql server running ? i had the same issue and i found out i didnt run my server before running the command

yarn gen
Toner answered 19/5, 2021 at 15:10 Comment(0)
O
1

I had the wrong path for my schema: set in my codegen.yml file. Make sure it's pathed correctly.

Omor answered 23/12, 2021 at 0:53 Comment(0)
D
0

This might caused by an incorrect Content-Type or encoding. Is there a reproduction for this issue somewhere? are repo that reproduces it?

Doggoned answered 22/3, 2020 at 20:56 Comment(0)
S
0

Just wanted to share that if your project is ran within a container, that it may be a docker-related network issue. I am currently running my project locally within a node container + vite.

My GraphQL server is ran in a separate container from a backend project.

Had to update my codegen.yaml file to read

schema: http://host.docker.internal:8000/graphql
Serviceberry answered 25/7, 2022 at 17:35 Comment(0)
E
0

Had the same issue. Root cause was that localhost server was listening to ipv6 interface only, so i had to explicitly declare in codegen config - 'http://::1:8000/grapql'.

Enchantress answered 23/11, 2022 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.