graphql-code-generator
graphql-code-generator copied to clipboard
Failed to load schema
Describe the bug
Repo URL is https://github.com/ag-reo/graphql-testing
Trying to get codegen to work on a new project BUT it keeps giving me the following error
(node:153353) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time (Usenode --trace-warnings ...` to show where the warning was created)
✔ Parse Configuration
⚠ Generate outputs
❯ Generate to app/server/graphql/sdk.ts
✖
Failed to load schema from http://localhost:8000/gql:
terminated
TypeError: terminated
at Fetch.onAborted (/alloc/Work/actual/remix/graphql-testing/node_modules/undici/lib/fetch/index.js:1890:49)
at Fetch.emit (node:events:390:28)
at Fetch.emit (node:domain:475:12)
at Fetch.terminate (/alloc/Work/actual/remix/graphql-testing/node_modules/undici/lib/fetch/index.js:83:10)
at Object.onError (/alloc/Work/actual/remix/graphql-testing/node_modules/undici/lib/fetch/index.js:2027:34)
at Request.onError (/alloc/Work/actual/remix/graphql-testing/node_modules/undici/lib/core/request.js:265:27)
at errorRequest (/alloc/Work/actual/remix/graphql-testing/node_modules/undici/lib/client.js:1722:13)
at Socket.onSocketClose (/alloc/Work/actual/remix/graphql-testing/node_modules/undici/lib/client.js:985:5)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12)
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.
◼ Load GraphQL documents
◼ Generate
Process finished with exit code 1 ` Now the strange thing is that in my previous project it works BUT all that is different is the versions of the required packages. Here are the older versions of the package that I am using.
` "@apollo/client": "^3.3.20", "graphql": "^15.5.1", "graphql-request": "^3.4.0", "graphql-tag": "^2.12.6",
Dev
"@graphql-codegen/cli": "1.21.5", "@graphql-codegen/import-types-preset": "^1.18.4", "@graphql-codegen/plugin-helpers": "^1.18.7", "@graphql-codegen/typescript": "1.22.3", "@graphql-codegen/typescript-graphql-request": "^3.2.3", "@graphql-codegen/typescript-operations": "1.18.2", "@graphql-codegen/typescript-resolvers": "^1.19.4", `
If I use the versions that I have just put however I have to also change my react and react-dom version otherwise it tells me of a version conflict.
Nothing else has changed, other than the versions. Any help is greatly appreciated.
Regards Anthoni
Your Example Website or App
https://github.com/ag-reo/graphql-testing
Steps to Reproduce the Bug or Issue
This is difficult to put because my graphql server is not live yet. Only in local.
Expected behavior
To generate the required sdk.ts that I have put in my codegen.yml file
Screenshots or Videos
No response
Platform
- OS: [e.g. macOS, Windows, Linux]
- NodeJS: [e.g. 18.5.0]
graphqlversion: [e.g. 16.3.0]@graphql-codegen/*version(s): [e.g. 2.6.2]
Codegen Config File
overwrite: true schema: "http://localhost:8000/gql" documents: "app/server/graphql/schema/**/*.graphql" generates: app/server/graphql/sdk.ts: plugins: - "typescript" - "typescript-operations" - "typescript-graphql-request" ./graphql.schema.json: plugins: - "introspection" config: avoidOptionals: true immutableTypes: false flattenGeneratedTypes: true preResolveTypes: true skipTypeNameForRoot: true skipTypename: true
Additional context
No response
Hi @AnthoniG,
It seems like codegen is failing to load your schema because of a network interruption. Are you sure that the URL is http://localhost:8000/gql and not http://localhost:8000/graphql?
Also, does your GraphQL Server has introspection enabled?
@charlypoly Yes it is definitely http://localhost:8000/gql, I am using a Laravel back-end.
As for introspection, I guess it is active because if I lower the versions of codegen, etc, it works perfectly.
@AnthoniG, does it work properly if you downgrade to @graphql-codegen/[email protected]?
@AnthoniG, I just discussed this with someone who had a similar issue because of HTTP redirections.
Could you perform a curl on your local endpoint and send me the detailed result here (without "follow redirects", with response body and headers).
Thank you,
@charlypoly I am having problems with my health at moment. Will check back when I can.
@AnthoniG, there is no hurry, stay safe. Good recover.
Hi @charlypoly , I have a similar issue This is my config:
overwrite: true
schema: localhost:8000
documents: "graphql/**/*.graphql"
generates:
generated/index.tsx:
config:
withHooks: true
plugins:
- add:
content: '// THIS IS A GENERATED FILE, use `yarn codegen` to regenerate'
- add:
content: '/* tslint:disable */'
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
And when I run the following curl, the server returns the introspection schema:
curl 'http://localhost:8000/' -X POST -H 'content-type: application/json' --data-raw '{"query":" query IntrospectionQuery { __schema { queryType { name } mutationType { name } subscriptionType { name } types { ...FullType } directives { name description locations args { ...InputValue } } } } fragment FullType on __Type { kind name description fields(includeDeprecated: true) { name description args { ...InputValue } type { ...TypeRef } isDeprecated deprecationReason } inputFields { ...InputValue } interfaces { ...TypeRef } enumValues(includeDeprecated: true) { name description isDeprecated deprecationReason } possibleTypes { ...TypeRef } } fragment InputValue on __InputValue { name description type { ...TypeRef } defaultValue } fragment TypeRef on __Type { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name } } } } } } } }","operationName":"IntrospectionQuery"}'
And for completeness, here is the error and the dependencies I'm using:
$ graphql-codegen --config codegen.yml
✔ Parse Configuration
❯ Generate outputs
✔ Parse Configuration
⚠ Generate outputs
❯ Generate to generated/index.tsx
✖
Failed to load schema from localhost:8000:
fetch failed to localhost:8000
TypeError: fetch failed
at Object.processResponse (node:internal/deps/undici/undici:6277:34)
at node:internal/deps/undici/undici:6602:42
at node:internal/process/task_queues:140:7
at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
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.
◼ Load GraphQL documents
◼ Generate
"dependencies": {
"@apollo/client": "^3.7.0",
"graphql": "^16.6.0",
"next": "12.3.1",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@graphql-codegen/add": "^3.2.1",
"@graphql-codegen/cli": "2.13.7",
"@graphql-codegen/client-preset": "1.1.0",
"@graphql-codegen/introspection": "2.2.1",
"@graphql-codegen/typescript": "^2.7.5",
"@graphql-codegen/typescript-operations": "^2.5.5",
"@graphql-codegen/typescript-react-apollo": "^3.3.5",
"@types/node": "18.8.3",
"@types/react": "18.0.21",
"@types/react-dom": "18.0.6",
"eslint": "8.24.0",
"eslint-config-next": "12.3.1",
"typescript": "4.8.4"
}
UPDATE: Using 127.0.0.1 instead of localhost solved the problem
Hi @lazandrei19,
Can you try replacing
schema: localhost:8000
with
schema: http://localhost:8000
Thank you,
I tried that too initially, with no luck. Further problems I encountered with different programs during development seem to indicate that my laptop treats localhost as IPv6, but my graphql server was listening only on IPv4.
I also just ran a quick chek inside node that confirms my suspicions:
Welcome to Node.js v18.7.0.
Type ".help" for more information.
> const dns = require('node:dns');
> dns.lookup('localhost', {}, (err, addr) => console.log(err, addr))
null ::1
Then could you try 127.0.0.1 and 0.0.0.0 instead of localhost?
UPDATE: Using 127.0.0.1 instead of localhost solved the problem
I did try it and it worked. I just don't know if the solution is the same for the problem @AnthoniG was having
Had a similar issue and using 127.0.0.1 or 0.0.0.0 would indeed fix the problem.
Sorry guys but I have been really poorly. Only now just been able to get on computer.
I can say that my bug has been fixed by adding in 127.0.0.1 as @lazandrei19 tried. Not sure why it works via that and not localhost, but don't really care :grinning: .
I just wanted it working
I am now using the new Config version written in Typescript. Here is my config now :-)
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
schema: 'http://127.0.0.1:8000/gql',
documents: ["app/server/graphql/schema/**/*.graphql"],
generates: {
'app/server/graphql/sdk.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-graphql-request'],
config: {
withHooks: true,
rawRequest: true,
pureMagicComment: true,
skipTypename: true,
useTypeImports: true
},
},
},
}
export default config
I can mark mine as solved, so I am holding it up then go ahead and close.