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

"This document does not define any operations" when working with GraphiQL #17

Open
SleepySquash opened this issue Jul 7, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@SleepySquash
Copy link

When working with GraphiQL, usually a number of queries are written down in the same document for convenience:

query todos {
  todos {
    id
    text
  }
}

mutation add {
  createTodo(data: { text: "dqwdwq" }) {
    id
  }
}

However, when trying to execute any of the queries, the GraphiQL sends the whole document to the server containing every operation out there, the one to run is sent via operationName parameter:

operationName: "add"
query: "query todos {\n  todos {\n    id\n    text\n  }\n}\n\nmutation add {\n  createTodo(data: {text: \"dqwdwq\"}) {\n    id\n  }\n}\n"
variables: {}

graphql_server2 doesn't handle such scenarios and prints the This document does not define any operations error due to this code:

OperationDefinitionContext getOperation(
    DocumentContext document, String? operationName) {
  var ops = document.definitions.whereType<OperationDefinitionContext>();
  if (operationName == null) {
    return ops.length == 1
        ? ops.first
        : throw GraphQLException.fromMessage(
            'This document does not define any operations.');
  } else {
    return ops.firstWhere((d) => d.name == operationName,
        orElse: (() => throw GraphQLException.fromMessage(
            'Missing required operation "$operationName".')));
  }
}

The appropriate solution would be to respect the operationName and find the operation with the specified name in the ops list, not throw an error if there's more than 1 (or less than 1) operation - error should be thrown if there's 0 operations in the document of the specified operationName doesn't correspond to any provided.

@dukefirehawk dukefirehawk added the bug Something isn't working label Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants