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

Add validation rule that operation types exist #955

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

benjaminjkraft
Copy link

@benjaminjkraft benjaminjkraft commented Jun 1, 2022

Right now the spec says that, for example, if the schema does not define
a mutation root type, then the schema does not support mutations. But
there's no validation rule for it, which means that many parsers
(including graphql-js) treat a mutation as valid against such a schema.
(Indeed, many end up considering any mutation as valid, since they
don't know what type to validate the root selection set against.) This
commit adds a validation rule to make the schema text explicit.

Slated for discussion at the June 2 2022 working group meeting.
Replaces #947. See also graphql/graphql-js#3592.


Update October 2024: this PR has been refreshed including changes proposed in #1098 and Benjie's editorial edits.

Fixes #1097

Right now the spec says that, for example, if the schema does not define
a mutation root type, then the schema does not support mutations.  But
there's no validation rule for it, which means that many parsers
(including graphql-js) treat a mutation as valid against such a schema.
(Indeed, many end up considering *any* mutation as valid, since they
don't know what type to validate the root selection set against.)  This
commit adds a validation rule to make the schema text explicit.

Slated for discussion at the June 2 working group meeting.  See also
graphql/graphql-js#3592.
@netlify
Copy link

netlify bot commented Jun 1, 2022

Deploy Preview for graphql-spec-draft ready!

Name Link
🔨 Latest commit b209bd8
🔍 Latest deploy log https://app.netlify.com/sites/graphql-spec-draft/deploys/6711494acfa54700085d3245
😎 Deploy Preview https://deploy-preview-955--graphql-spec-draft.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@benjie
Copy link
Member

benjie commented Jun 1, 2022

Excellent! I'm not convinced we need the changes to section 3, but the section 5 changes look solid short of some bikeshedding over inserting "All Operation Definitions" versus elevating the "Operation Type Existence" header.

@benjaminjkraft
Copy link
Author

Ok! I don't feel strongly about mentioning it in section 3.

BTW, the other thing to discuss is whether the rule that a schema must have a query and that the root types must be object types should have explicit validation language as well. It seems like the schema side is a lot less consistent about that, so I wasn't sure. We can discuss tomorrow.

@benjie
Copy link
Member

benjie commented Jun 2, 2022

The validation section (section 5) is about validation of requests; the schema has its own validation rules in the relevant places, for example the rules about root operation types can be found here:

https://spec.graphql.org/draft/#sec-Root-Operation-Types

@leebyron leebyron added the 💡 Proposal (RFC 1) RFC Stage 1 (See CONTRIBUTING.md) label Jun 2, 2022
benjaminjkraft added a commit to benjaminjkraft/gqlparser that referenced this pull request Jun 6, 2022
Turns out neither we (nor anyone else) were validating this, because the
spec didn't say explicitly to validate it.  So you could do
`mutation { bogus }` even if the schema has no mutation types, or worse,
any syntactically valid query if the schema is totally empty.

In graphql/graphql-spec#955 I'm adding it to the spec, and in
graphql/graphql-js#3592 someone else is adding it to graphql-js.  So we
should too, once those land!  At that point we should also likely
reimport the relevant tests, instead of the ones I wrote here, but I
figured I'd put the PR up now so folks can review the non-test parts if
you like.

Fixes vektah#221.
StevenACoffman pushed a commit to vektah/gqlparser that referenced this pull request Jun 6, 2022
Turns out neither we (nor anyone else) were validating this, because the
spec didn't say explicitly to validate it.  So you could do
`mutation { bogus }` even if the schema has no mutation types, or worse,
any syntactically valid query if the schema is totally empty.

In graphql/graphql-spec#955 I'm adding it to the spec, and in
graphql/graphql-js#3592 someone else is adding it to graphql-js.  So we
should too, once those land!  At that point we should also likely
reimport the relevant tests, instead of the ones I wrote here, but I
figured I'd put the PR up now so folks can review the non-test parts if
you like.

Fixes #221.
@Shane32
Copy link

Shane32 commented Jun 10, 2022

BTW, the other thing to discuss is whether the rule that a schema must have a query and that the root types must be object types should have explicit validation language as well.

It seems to me that validation of a document is different than validation of a schema. Section 5 validates "if a request is syntactically correct" and " is unambiguous and mistake‐free in the context of a given GraphQL schema". So I don't believe section 5 is intended to cover validation of a schema, and I would not add any schema validation language there.

Rather, section 3 "describes the capabilities of a GraphQL server", and already has language stating that a query root type must exist, etc.

Perhaps it is worth considering rewriting all of section 3 to include schema validation rules more like the "formal specification" syntax used in section 5. However, this may be more challenging in that a schema is not required to be represented by a SDL.

@benjaminjkraft
Copy link
Author

Yeah, to be clear, we would not add schema validation to section 5, if we wanted to add that it would go in section 3 and there's some precedent for validation rules there. As you say making that validation more formal is probably a bigger project, so I left it out for now; as currently written this is all about document validation.

Copy link
Member

@benjie benjie left a comment

Choose a reason for hiding this comment

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

Looking good, couple minor fixes 👍

spec/Section 3 -- Type System.md Outdated Show resolved Hide resolved
spec/Section 5 -- Validation.md Outdated Show resolved Hide resolved
benjaminjkraft and others added 2 commits June 13, 2022 14:50
Co-authored-by: Benjie Gillam <[email protected]>
Co-authored-by: Benjie Gillam <[email protected]>
@benjie
Copy link
Member

benjie commented Oct 17, 2024

I have:

I think it's good to go now! 🤞

Copy link

@Shane32 Shane32 left a comment

Choose a reason for hiding this comment

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

Short and to the point. I like it. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💡 Proposal (RFC 1) RFC Stage 1 (See CONTRIBUTING.md)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No validation rule exists to assert mutation/subscription operations exist in the schema
5 participants