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:
Any ideas on why is that? Anything else I could check?
Thanks
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
I had the wrong path for my schema:
set in my codegen.yml file. Make sure it's pathed correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With