Describe the bug
Can't specify schema files to exclude
To Reproduce
Steps to reproduce the behavior:
√ Parse configuration
> Generate outputs
> Generate src/generated/graphql.generated.ts
× Load GraphQL schemas
→ Failed to load schema
Load GraphQL documents
Generate
Found 1 error
× src/generated/graphql.generated.ts
Failed to load schema from !**/*.generated.ts:
Unable to find any GraphQL type defintions for the following pointers: !
**/*.generated.ts
Error: Unable to find any GraphQL type defintions for the following poin
ters: !**/*.generated.ts
at Object.loadTypedefs
(C:\Users\Canh\Documents\Project\Mandevices\mandevices\server\services\post\node
_modules\graphql-toolkit\dist\commonjs\loaders\load-typedefs.js:78:15)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
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.
type Query {
posts: [Post!]!
}
type Post @key(fields: "id") {
id: ID!
title: String
}
Query: {
posts: () => {
return [
{
id: "abc",
title: "Sach"
}
];
}
},
Post: {
// @ts-ignore
__resolveReference(post, { fetchPostById }) {
return fetchPostById(post.id);
}
}
codegen.yml config file:overwrite: true
schema:
- './src/**/*.ts'
- '!**/*.generated.ts'
documents: null
generates:
src/generated/graphql.generated.ts:
plugins:
- "typescript"
- "typescript-resolvers"
config:
federation: true
noSchemaStitching: true
Expected behavior
Files should be generated
Environment:
@graphql-codegen/...: 1..6.1Weird, the generated files should be automatically ignored.
@CANHBK please note the following: it's not a good idea to specify all **/*.ts files - it will make the codegen work very hard. Try to be more specific.
Also, can you please try to use:
schema:
- './src/**/*.ts'
- '!(src/**/*.generated.ts)'
?
@dotansimha, Thanks for your answer
I'm not opening another issue because I have a similar problem.
This is my codegen.yml:
overwrite: true
schema: "http://localhost:8080/graphql"
documents:
- "src/web/schemas/public/*.graphql"
- "src/web/schemas/private/*.graphql"
generates:
src/generated/graphql.ts:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-resolvers"
- "typescript-react-apollo"
./graphql.schema.json:
plugins:
- "introspection"
And this is the output:
$ graphql-codegen --config codegen.yml
√ Parse configuration
> Generate outputs
> Generate src/generated/graphql.ts
√ Load GraphQL schemas
× Load GraphQL documents
→ Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/*.graphql, src/web/schemas/private/*.graphql
Generate
> Generate ./graphql.schema.json
√ Load GraphQL schemas
× Load GraphQL documents
→ Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/*.graphql, src/web/schemas/private/*.graphql
Generate
But when I do ls src/web/schemas/private/*.graphql, I see all the files there.
Note: I have multiple .graphql files which I'm merging with mergeSchemas, and I've sorted them into folders based on permission level, but this shouldn't interefere with finding the files in the first place.
OS: Windows 10 (using Cygwin to do the ls, but the file finder should be able to figure things out, right?)
Any clue, @dotansimha? Thanks!
Hi @dandrei !
I'm not sure what causing this. Is it something you can share so I can reproduce it locally?
Hi @dotansimha, I've stripped down the code to the bare minimum and uploaded it here: https://www.dropbox.com/s/89lhisjmdmuwc32/2019-09-09-gcg.zip?dl=0
Also tested on Linux to make sure this isn't OS specific.
After extracting the archive, all you need to do is:
npm inpm run startAt this point, http://127.0.0.1:8080/graphql should be serving a GraphQL endpoint (if you visit it in the browser, you'll get the Apollo GraphQL Playground interactive query IDE).
The only query that works in this stripped down version is:
query {
readData(name: "testing 123")
}
Running graphql-codegen --config codegen.yml (or npm run gen -- it's aliased in package.json) should reproduce the issue.
Even specifying the path explicitly (without wildcards) in codegen.yml fails:
documents:
- "src/web/schemas/public/public.graphql"
That errors out as:
Error: Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/public.graphql
But the file is clearly there.
+1 experiencing the exact same behaviour as @dandrei
+1
Maybe it's related to the directory you are running it from? (the cwd)
I'll try locally your reproduction and update soon.
@dandrei
I don't understand your codegen.yml file.
Under schema, you specify a url for GraphQL endpoint which should provide your schema.
You also have local files for your schema, and you use it with documents field (which is for GraphQL operations documents).
You should first figure out where you schema is coming from. If you are writing the GraphQL server, you probably can load your schema files using local files (and then there is no need for url endpoint). If you are writing a client side, you can either load the schema from local file, or from endpoint.
But your documents field should have value only of you are using client-side plugins in your client-side application, and it should point to GraphQL operations (query/mutation/fragment/subscription).
Also, the typescript plugin is a common plugin, but typescript-operations/typescript-react-apollo is for client-side only, and typescript-resolvers is for server-side only - they shouldn't get mixed.
@brabacpetr @PhillipWinder I'm not sure what's it your use-case and what causes it, but please add more info and a reproduction.
Thanks @dotansimha.
I am interested in generating files for _both_ the client side (Apollo React hooks and TypeScript types) and server side (TypeScript types and resolver signatures -- the last one mostly to check that my resolvers have the correct input and output types, useful especially when refactoring them).
I specified both a URL (under the schema field) and .graphql files (under the documents field) because that's how I saw it done in the configuration example here: https://graphql-code-generator.com/docs/getting-started/#using-in-runtime
Are you saying that I have to generate the server-side and client-side code in separate steps (so using two different codegen.yml configuration files)?
I did the following and it seems to work without throwing errors:
codegenc.yml (for client)
overwrite: true
schema: "http://localhost:8080/graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
./graphql.schema.json:
plugins:
- "introspection"
codegens.yml (for server)
overwrite: true
schema: "http://localhost:8080/graphql"
generates:
src/generated/graphql.ts:
schema:
- "src/web/schemas/public/public.graphql"
plugins:
- "typescript"
- "typescript-resolvers"
./graphql.schema.json:
plugins:
- "introspection"
Haven't yet tested if the two generated files (graphql.ts, graphql.tsx) work the way I need them to, but at least I managed to generate them, which was the original problem.
@dandrei As I see, you don't have documents in codegenc.yml file so you don't have any generated components. But, you should have documents with your operations defined to be used in the client app.
You're right, I just can't seem to get it to not throw errors when I specify a documents field.
If the configuration file is this:
overwrite: true
documents:
- "src/web/schemas/public/public.graphql"
schema: "http://localhost:8080/graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
./graphql.schema.json:
plugins:
- "introspection"
I get this:
graphql-codegen --config codegenc.yml
√ Parse configuration
> Generate outputs
> Generate src/generated/graphql.tsx
√ Load GraphQL schemas
× Load GraphQL documents
→ Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/public.graphql
Generate
@CANHBK Does that src/web/schemas/public/public.graphql file have operations like below;
query SomeQuery {
foo {
bar
}
}
If yes, please open a new issue with a reproduction using this sandbox https://codesandbox.io/s/github/dotansimha/graphql-code-generator-issue-sandbox-template
If no, I suggest you to check GraphQL docs to learn more about the difference between schema and documents.
the spec is really confusing https://graphql.github.io/graphql-spec/June2018/#sec-Language.Document
Here is a link to explain the differences https://stackoverflow.com/questions/59417171/whats-difference-between-schema-and-documents-in-graphql
Most helpful comment
Even specifying the path explicitly (without wildcards) in
codegen.ymlfails:That errors out as:
But the file is clearly there.