question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mutation validates when schema does not support mutations

See original GitHub issue

graphql-js currently considers the following bogus operation valid against the given schema:

# schema:
type Query { f: String }

# operation:
mutation { bogus }

In particular, if the schema does not define a root type for one of the GraphQL operation types (here mutation), graphql-js does not against such operations. In fact, this can even happen with queries:

# schema:
type T { f: String }

# operation:
query { bogus }

(edit: validate does complain here, although buildSchema does not.)

Of course, any server that tries to resolve such a query will crash, or at best return null. The spec does say that schemas must have a query root operation type (so the second schema is invalid) and that schemas with no mutation root operation type do not support mutations (so the first operation is invalid).

In practice, this has come up for me primarily in testing graphql libraries – while most real-world schemas support both queries and mutations, and users will typically look before assuming a server supports subscriptions, it’s easy in writing test schemas to forget that. But I’m sure there do exist servers which don’t have any mutations, too; I think my previous job had some such internal services, although since they were behind an Apollo federated gateway users would never see this.

I may have some time to write a PR for this. One question is whether it’s actually safe to start validating against schemas with no query root operation type; it wouldn’t surprise me if there are plenty of cases where users validate an extension schema (which may legitimately have no query root operation type) on its own, and currently it will validate. So it may be better to avoid the potentially-breaking change and validate both cases at the query stage. But I’d love to hear from maintainers as to their thoughts on that point. (edit: irrelevant if we keep the validation in validate rather than buildSchema.)

BTW, my suspicion is many other GraphQL libraries have this issue; I originally discovered it in vektah/gqlparser#221.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yaacovCRcommented, May 18, 2022

Got it, thanks for clarifying. This is probable because of that check above, but perhaps simply removing it is not the answer. The strict view of FieldsOnCorrectType rule assumes the existence of the parent type because it’s only checking fields. We could automatically fail if the parent type does not exist or we could stay strict and introduce a new rule KnownOperationType

0reactions
benjaminjkraftcommented, May 20, 2022

That looks great, thanks for making the PRs! I should be able to attend, I’ll add to the agenda.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation and User Errors in GraphQL Mutations
In this blog post I would like to propose the pattern which you can use to handle user input validation and user errors...
Read more >
Evidence of bug in query and validate that warrants further ...
Schema: No mutations defined. Request: mutation. Method: ParseAndValidate.validate. Returns it as valid . If I add mutation with dummy ...
Read more >
Writing mutation resolvers | Full-Stack Quickstart
How a GraphQL mutation modifies data. We've written all of the resolvers we need for schema fields that apply to query operations. Now,...
Read more >
Graphql-Mutation : Schema Exception - Stack Overflow
You can try this: public void ConfigureServices(IServiceCollection services) { services.AddGraphQLServer() .ConfigureSchema(sb => sb.
Read more >
Mutations and Input Types - GraphQL
You don't need anything more than this to implement mutations. But in many cases, you will find a number of different mutations that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found