Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hijack and make it work how I would imagine it #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

n1ru4l
Copy link
Owner

@n1ru4l n1ru4l commented Jul 7, 2021

No description provided.

return {
- document: graphql.concatAST(sources.map(source => graphql.parse(source, options))),
+ document,
+ rawSDL: sources.map(source => source.body).join(`\n#-#\n`),
Copy link
Owner Author

@n1ru4l n1ru4l Jul 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rawSDL is used for building the operation string to Type mapping. Since multiple gql calls are merged into a single document \n#-#\n is used for separating them later.

If we don't provide rawSDL at all, something very weird is happening as it is tried to be auto-generated somewhere and we end up with something weird similar to this: ardatan/graphql-tools#3124

For three gql calls within a single document rawSDL equals to the following string

[Object object]\n\n[Object object]\n\n[Object object]

Comment on lines +9 to +16
- ignore: Object.keys(config.generates).map(p => path.join(process.cwd(), p)),
+ ignore: Object.keys(config.generates).map(p => {
+ let ignorePath = path.join(process.cwd(), p)
+ if (ignorePath.endsWith("**/*") === false) {
+ ignorePath += "**/*"
+ }
+ return ignorePath
+ }),
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have an exact idea what is going on here and this patch probably breaks other stuff. I noticed that the following codegen config:

generates:
  ./demo/gql/:
    preset: ./lib/preset.js
    plugins: [./lib/plugin.js]

Would result in the following error:

Error: unable to find loader for glob "/Users/laurinquast/projects/graphql-typescript-integration/demo/gql/"
        at addGlobsToLoaders (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/index.js
:295:19)
        at collectPathsFromGlobs (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/inde
x.js:331:5)
        at collectSources (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/index.js:21
6:23)
        at loadTypedefs (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/index.js:609:
21)
        at loadDocuments (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-codegen/cli/bin.js:515:
31)
        at /Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-codegen/cli/bin.js:916:55
        at Task.task (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-codegen/cli/bin.js:761:17)
    Error: unable to find loader for glob "/Users/laurinquast/projects/graphql-typescript-integration/demo/gql/"
        at addGlobsToLoaders (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/index.js
:295:19)
        at collectPathsFromGlobs (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/inde
x.js:331:5)
        at collectSources (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/index.js:21
6:23)
        at loadTypedefs (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-tools/load/index.js:609:
21)
        at loadDocuments (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-codegen/cli/bin.js:515:
31)
        at /Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-codegen/cli/bin.js:916:55
        at Task.task (/Users/laurinquast/projects/graphql-typescript-integration/node_modules/@graphql-codegen/cli/bin.js:761:17)

I dug into it and it seems that the folder is not properly ignored because the string must be a glob pattern. So I simply added it there.

Comment on lines +15 to +33
const expandSources = (sources: Array<Source>): Array<Source> => {
const expandedSources: Array<Source> = [];
for (const source of sources) {
const document = source.document!;
const rawSDL = source.rawSDL!;
const operationSDL = rawSDL.split(`\n#-#\n`);
operationSDL.forEach((rawSDL, index) => {
expandedSources.push({
document: {
kind: "Document",
definitions: [document.definitions[index]],
},
rawSDL,
location: source.location,
});
});
}
return expandedSources;
};
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we split our operations that got concatenated into a single string via CodeFileLoader into back into single operations. SO we can properly create the operations raw string -> type document node mapping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant