graphql-code-generator: Unable to create custom document loader for glob pattern source
I am trying to write my own document loader to facilitate the usage of the @export directive from Apollo Client. I found several issues (#3939 & #2771) (even a PR #3886) that contain bits and pieces for the solution, but I figured I’d require a custom document loader because I need to filter out some variable definition requirements to prevent syntax errors.
I am now running into the issue that the custom document loader functionality provided by GraphQL Codegen (and if I followed the code trail correctly, it’s actually the custom loader functionality of @graphql-tools/load) is expecting a single Source, string or GraphQLSchema return value. This is a bit hard when I am trying to facilitate a loader for a glob pattern. This will inherently return a Source[] or string[].
Also, I literally tried to use the loadDocuments function from @graphql-tools/load (I noticed the arguments given to the loader are exactly the same as the ones loadDocuments expects.) This’ll return an Array<Source>, and even just returning a single Source I keep getting the following error:
Error:
Unable to find any GraphQL type definitions for the following pointers:
- **/*.graphql
at prepareResult (/workspace/node_modules/@graphql-tools/load/index.js:507:15)
at loadTypedefs (/workspace/node_modules/@graphql-tools/load/index.js:473:12)
at async loadDocuments (/workspace/node_modules/@graphql-codegen/cli/bin.js:540:31)
at async /workspace/node_modules/@graphql-codegen/cli/bin.js:944:55
at async Task.task (/workspace/node_modules/@graphql-codegen/cli/bin.js:789:17)
I hope I am doing it wrong, so I’d love a working example of a custom document loader for a glob pattern, but I am afraid it’s not supported by @graphql-tools/load (atleast not for a CustomLoader). If this is the case, what’s the expected path to take from here?
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 7
- Comments: 18 (1 by maintainers)
It seems to me like custom loaders just got skipped over in https://github.com/ardatan/graphql-tools/pull/3157. Would it be sufficient to just change the lines here to add a check if the custom loader returned an array of sources, and if so, add all the sources to the list? Then custom loaders could function the same as the rest of the loaders (fallback loaders, if I’m reading the code correctly).
Oh. My. God! Option 2 does actually work! 🎉 🎉 🎉 I wish I could’ve got back the time I’ve spent on the workaround.
So to recap, if you’re working with globs and custom loaders:
Note: In my case, I was dealing with client-side queries, so I’ve joined the string definitions first with
.join('\n'), and thengraphql.parse’d the merged string.@wesselvdv and @abrenneke, would this approach solve your issues too?
And @dotansimha, @ardatan, and @B2o5T, could we get this info into the docs, to help out other poor souls?
Hi @B2o5T I’ll create it this weekend
Could we add info from @JuroOravec’s comment to the documentation? For example to the “Custom Document Loader” section?
@dotansimha You have any pointers for me?