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

Issue with multiple queries/mutations in single .graphql file using loader #168

Closed
ryanjsmyth opened this issue Apr 11, 2018 · 5 comments
Closed
Labels

Comments

@ryanjsmyth
Copy link

ryanjsmyth commented Apr 11, 2018

The following code is causing some issues with exporting multiple queries/mutations from a single .graphql file.

module.exports = doc;

for (const op of doc.definitions) {
  if (op.kind === "OperationDefinition") {
    if (!op.name) {
      if (operationCount > 1) {
        throw "Query/mutation names are required for a document with multiple definitions";
      } else {
        continue;
      }
    }

    const opName = op.name.value;
    outputCode += `
    module.exports["${opName}"] = oneQuery(doc, "${opName}");        
  }
}

When exporting multiple queries/mutations there are a number of unnecessary properties hanging off of each document.

This is because the root doc is assigned to module.exports
module.exports = doc;
and then is incrementally added to through calls to oneQuery
module.exports["${opName}"] = oneQuery(doc, "${opName}");
which essentially does:
doc["${opName}"] = oneQuery(doc, "${opName}");

The call to oneQuery copies the doc and only overwrites the definitions property. This means that all preceding queries/mutations exist on the new doc created by oneQuery.

Here is an example of what the exported queries may look like:

  • FirstQuery
    • definitions
  • SecondQuery
    • definitions
    • FirstQuery
      • definitions
  • ThirdQuery
    • definitions
    • FirstQuery
      • definitions
    • SecondQuery
      • definitions
      • FirstQuery
        • definitions

The problem is this structure becomes deeply nested and if you deepclone it can cause out of memory exceptions. I didn't encounter this problem until one .graphql file grew in size and we began using Apollo Link State, which does a deep clone on your mutation, removing the @client directive, before executing it.

I don't know if this side-effect exists with purpose but if not I think there should be a shallow clone of doc before assigning it to module.exports.

/label bug

@dcflow
Copy link

dcflow commented Apr 16, 2018

@ryanjsmyth I'm having the same issue .. after some line in the gql doc the queries/mutations just stop working .. can you label this /label bug

@dcflow
Copy link

dcflow commented Apr 16, 2018

Hey @jnwng I think there is a problem with the webpack loader when the gql file gets too big

@dcflow
Copy link

dcflow commented Apr 16, 2018

This issue also causes this apollographql/react-apollo#1928

@JonaMc
Copy link

JonaMc commented Apr 16, 2018

I've encountered the same issue. When running any of the mutations below a certain point in my .graphql file, my browser tab completely freezes up and the "out of memory" error can be seen if my console doesn't die first.

@Amareis
Copy link

Amareis commented Apr 19, 2018

Heeeey, just see at this beautiful graphic from my dev tools! It really blocks all the shit, can you fix it right now?!
image

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

No branches or pull requests

4 participants