Describe the bug
When specifying !-exclude rules per the documentation, schema generation fails with a message suggesting that it tried to load the exclude rule as if it were a file:
Failed to load schema from !packages/server/src/schema/index.ts:
Unable to find any GraphQL type definitions for the following pointers: !packages/server/src/schema/index.ts
Error: Unable to find any GraphQL type definitions for the following pointers: !packages/server/src/schema/index.ts
at Object.loadTypedefs (/Users/seankelley/code/repo/node_modules/graphql-toolkit/dist/commonjs/loaders/load-typedefs.js:127:11)
at async Object.exports.loadSchema (/Users/seankelley/code/repo/node_modules/@graphql-codegen/cli/dist/commonjs/load.js:79:19)
at async Promise.all (index 1)
at async /Users/seankelley/code/repo/node_modules/@graphql-codegen/cli/dist/commonjs/codegen.js:175:66
at async Task.task (/Users/seankelley/code/repo/node_modules/@graphql-codegen/cli/dist/commonjs/codegen.js:18:9)
To Reproduce
Steps to reproduce the behavior:
https://codesandbox.io/s/graphql-codegen-issue-template-i3fpy
(These are pretty large -- will provide a stripped-down subset if it turns out to be necessary.)
(These are pretty large -- will provide a stripped-down subset if it turns out to be necessary.)
codegen.yml config file:schema:
- "packages/server/src/schema/**/*.ts"
- "!packages/server/src/schema/index.ts"
overwrite: true
generates:
packages/frontend-common/src/graphql/__generated__/fragmentTypes.ts:
plugins:
- fragment-matcher
packages/server/src/types/__generated__/types.ts:
plugins:
- "typescript"
- "typescript-resolvers"
hooks:
afterOneFileWrite:
- prettier --write
That index.ts file just collects all the other schema files that match .../**/*.ts and doesn't define any of its own GraphQL, which is why it's excluded.
If I put dummy GraphQL in index.ts with a gql tag, it works just fine, further suggesting it's trying to load the file erroneously.
Expected behavior
The codegen succeeds, ignoring the index.ts file.
Environment:
@graphql-codegen/cli: ^1.7.0@graphql-codegen/fragment-matcher: ^1.7.0@graphql-codegen/typescript: 1.7.0@graphql-codegen/typescript-resolvers: 1.7.0@graphql-codegen/typescript-document-nodes: 1.7.0Additional context
This is the similar, or the same as, #2410, but that issue got derailed and was closed a while ago.
I did a little spelunking in the code, and I think the issue starts here:
It loads each entry in schema separately rather than all of them together, which is what the docs state:
All provided glob expressions are being evaludated together - the usage is similar to .gitingore file.
...and also what would be necessary for globbing exclusion to work properly as described in the docs.
@seansfkelley I think in negative glob patterns, you need to wrap the negated part with braces:
schema:
- "packages/server/src/schema/**/*.ts"
- "!(packages/server/src/schema/index.ts)"
Can you please try it?
No dice:
Failed to load schema from !(packages/server/src/schema/index.ts):
Unable to find any GraphQL type definitions for the following pointers: !(packages/server/src/schema/index.ts)
Error: Unable to find any GraphQL type definitions for the following pointers: !(packages/server/src/schema/index.ts)
at Object.loadTypedefs (/Users/seankelley/code/repo/node_modules/graphql-toolkit/dist/commonjs/loaders/load-typedefs.js:78:15)
at async Object.exports.loadSchema (/Users/seankelley/code/repo/node_modules/@graphql-codegen/cli/dist/commonjs/load.js:64:23)
at async Promise.all (index 1)
at async /Users/seankelley/code/repo/node_modules/@graphql-codegen/cli/dist/commonjs/codegen.js:148:82
at async Task.task (/Users/seankelley/code/repo/node_modules/@graphql-codegen/cli/dist/commonjs/codegen.js:18:17)
It still looks like it's trying to load the negation rule as a file. My theory in "additional context" above explains that partially, though it doesn't explain why, if it's only getting a negated glob with no positive globs, it doesn't just match literally everything else (maybe it refuses to handle a set of globs with no positive glob?).
Okay, I got a working sandbox for the issue: https://codesandbox.io/s/graphql-codegen-issue-template-i3fpy
Thanks @seansfkelley ! I'll take a look
Hello! Do you have news about this? thank you! :D
Could you try with the following canary version?
1.8.4-alpha-75df213b.53+75df213b
I put the following: - '! (README.md) '
With the Canary version.
And I got:
脳 ./src/app/utils/graphql-gen/apollo.ts
Failed to load schema from !(README.md):
Unable to find any GraphQL type definitions for the following pointers: !(README.md)
Error: Unable to find any GraphQL type definitions for the following pointers: !(README.md) ...
@Dragons0458 Could you share your codegen.yml and package.json file?
FWIW it now works with my sandbox linked above, which I've updated to use the alpha version you mentioned. It also works on my original test case for which I filed this issue, so as far as I'm concerned, it works!
@Dragons0458 Could you share your codegen.yml and package.json file?
yes!.
Codegen.yml
schema:
- ./src/app/graphql/schemas/**/*.ts
- !(./src/app/graphql/schemas/**/index.ts)
package.json without canary version.
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.12",
"@angular/cli": "~8.3.12",
"@angular/compiler-cli": "~8.2.11",
"@angular/language-service": "~8.2.11",
"@graphql-codegen/cli": "^1.8.3",
"@graphql-codegen/typescript": "^1.8.3",
"@graphql-codegen/typescript-apollo-angular": "^1.8.3",
"@graphql-codegen/typescript-operations": "^1.8.3",
"@graphql-codegen/typescript-resolvers": "^1.8.3",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tsconfig-paths": "^3.9.0",
"tslint": "~5.15.0",
"typescript": "~3.5.3"
}
@Dragons0458 In here I couldn't see !(README.md) in schema fields in codegen.yml and 1.8.4-alpha-75df213b.53+75df213b as codegen's version in package.json. :)
So could you try with the following canary version?
1.8.4-alpha-75df213b.53+75df213b
@Dragons0458 In here I couldn't see
!(README.md)inschemafields in codegen.yml and1.8.4-alpha-75df213b.53+75df213bas codegen's version in package.json. :)
So could you try with the following canary version?
1.8.4-alpha-75df213b.53+75df213b
The README.MD file was just an example, my apologies for not specifying well.
These are my updated files:
codegen.yml
schema:
- ./src/app/graphql/schemas/**/*.ts
- "!(./src/app/graphql/schemas/**/index.ts)"
generates:
./src/app/utils/graphql-gen/apollo.ts:
schema: http://localhost:4000/api
documents: ./src/app/graphql/documents/**/*.graphql
plugins:
- typescript
- typescript-operations
- typescript-apollo-angular
./src/app/utils/graphql-gen/resolvers.ts:
schema: 'type Mutation { _: Boolean } type Query { _: Boolean }'
plugins:
- typescript
- typescript-resolvers
require:
- ts-node/register
package.json
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.12",
"@angular/cli": "~8.3.12",
"@angular/compiler-cli": "~8.2.11",
"@angular/language-service": "~8.2.11",
"@graphql-codegen/cli": "^1.8.4-alpha-75df213b.53+75df213b",
"@graphql-codegen/typescript": "^1.8.3",
"@graphql-codegen/typescript-apollo-angular": "^1.8.3",
"@graphql-codegen/typescript-operations": "^1.8.3",
"@graphql-codegen/typescript-resolvers": "^1.8.3",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tsconfig-paths": "^3.9.0",
"tslint": "~5.15.0",
"typescript": "~3.5.3"
}
The mistake is:
Found 2 errors
脳 ./src/app/utils/graphql-gen/apollo.ts
Failed to load schema from !(./src/app/graphql/schemas/**/index.ts):
Unable to find any GraphQL type definitions for the following pointers: !(./src/app/graphql/schemas/**/index.ts)
Error: Unable to find any GraphQL type definitions for the following pointers: !(./src/app/graphql/schemas/**/index.ts)
Thanks for the help!
@ardatan is this fixed now?
@Dragons0458
"@graphql-codegen/cli": "1.8.4-alpha-75df213b.53+75df213b",
If you're using canary versions, you shouldn't use ^ and it should be exact version like above.
Could you fix it and try again?
@dotansimha It supposed to be fixed in that canary. Let's wait @Dragons0458 's response
Hello! I just changed the package and the previous error does not happen, now I get the following:
Failed to load schema from ./src/app/graphql/schemas/**/*.ts,!(./src/app/graphql/schemas/**/index.ts),http://localhost:4000/api:
Unable to load from file "PATH\src\app\graphql\schemas\index.ts": Unexpected token export

Do you know why it happens? thanks!
@Dragons0458 If you export your type definitions from a single file, you can use schema/index.ts with ts-node. So, codegen would take all schema from single import.
Or you can let codegen pluck your type definitions from schema files by respecting gql tags in your schema files by using glob (you don't need ! in that case, because it would skip if there is no gql tag in the file).
So let's remove require and !index.ts glob from your configuration file and try again to see what happens.
If you want to use your exported schema from index.ts, just use schema/index.ts without any glob expressions to respect how you import/export stuff.
Hello! sorry for the delay, it's a very good idea, but I have a question ... if what returns index.ts is an array of gql, how do I get graphql-code-generator to interpret it? thanks!
This is the index.ts file:
import directives from './directives';
import inputs from './inputs';
import mutations from './mutations';
import queries from './queries';
import types from './types';
export default directives.concat(mutations).concat(types).concat(inputs).concat(queries);
When creating the client, I import this array so that I can visualize my local scheme with the Apollo DevTools plugin.
At the moment I have been working with the following file:
schema:
- ./src/app/graphql/schemas/directives/client.ts
- ./src/app/graphql/schemas/inputs/company.ts
- ./src/app/graphql/schemas/mutations/company.ts
- ./src/app/graphql/schemas/queries/company.ts
- ./src/app/graphql/schemas/types/company.ts
generates:
./src/app/utils/graphql-gen/apollo.ts:
schema: http://localhost:4000/api
documents: ./src/app/graphql/documents/**/*.graphql
plugins:
- typescript
- typescript-operations
- typescript-apollo-angular
./src/app/utils/graphql-gen/resolvers.ts:
schema: 'type Mutation { _: Boolean } type Query { _: Boolean }'
plugins:
- typescript
- typescript-resolvers
./src/app/utils/graphql-gen/fragment-matcher.ts:
schema: http://localhost:4000/api
plugins:
- fragment-matcher
require:
- ts-node/register
But as you can see, it is quite tedious to have to put schemas that way :(
Schema loader can understand gql tags so you don't need require: ts-node/register at all, it would be better to remove it. Also;
What if ./src/app/graphql/schemas/**/*.ts? Schema loader would ignore index.ts in that case.
schema: ./src/app/graphql/schemas/**/*.ts
If not, you can pass noRequire: true with no require: ts-node/register to force schema loader obtain SDL using graphql-tag-pluck without using import/export like below;
schema:
./src/app/graphql/schemas/**/*.ts:
noRequire: true
Thank you very much! In the end the file looked like this:
schema:
./src/app/graphql/schemas/**/*.ts:
noRequire: true
generates:
./src/app/utils/graphql-gen/apollo.ts:
schema: http://localhost:4000/api
documents: ./src/app/graphql/documents/**/*.graphql
plugins:
- typescript
- typescript-operations
- typescript-apollo-angular
./src/app/utils/graphql-gen/resolvers.ts:
schema: 'type Mutation { _: Boolean } type Query { _: Boolean }'
plugins:
- typescript
- typescript-resolvers
./src/app/utils/graphql-gen/fragment-matcher.ts:
schema: http://localhost:4000/api
plugins:
- fragment-matcher