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

gqlparser accepts a schema with no query type and validates any query against it #221

Closed
benjaminjkraft opened this issue May 15, 2022 · 5 comments · Fixed by #225
Closed
Labels
help wanted Extra attention is needed

Comments

@benjaminjkraft
Copy link
Contributor

Consider the following schema:

type T { f: String }

Note the lack of a Query type (or a schema declaration pointing to an alternative entrypoint); you can't make any queries against it. However:

  1. gqlparser considers this schema valid; this is sort of debatable but the spec says "The query root operation type must be provided and must be an Object type." and here the query root operation type defaults to Query which is not a valid object type.
  2. Worse, gqlparser will validate any (otherwise syntactically correct) query against such a schema, e.g. the query { f { g } } will validate; this is clearly wrong.
    Indeed the same thing happens with an entirely empty schema.

Here's a test case for your convenience (I just shoved it in a random file since I wasn't sure how the tests are organized):

diff --git validator/imported/spec/schemas.yml validator/imported/spec/schemas.yml
index e7db1f3..5eeafed 100644
--- validator/imported/spec/schemas.yml
+++ validator/imported/spec/schemas.yml
@@ -487,3 +487,4 @@
   }
 
   scalar Any
+- ""
diff --git validator/spec/FragmentsOnCompositeTypes.spec.yml validator/spec/FragmentsOnCompositeTypes.spec.yml
index ec8ad0b..012c154 100644
--- validator/spec/FragmentsOnCompositeTypes.spec.yml
+++ validator/spec/FragmentsOnCompositeTypes.spec.yml
@@ -7,3 +7,10 @@
   errors:
     - message: Fragment "c" cannot condition on non composite type "Float".
     - message: Cannot spread fragment "c" within itself.
+
+- name: Empty schema
+  schema: 20
+  query: |
+    { f { g } }
+  errors:
+    - message: Something should fail!
@benjaminjkraft
Copy link
Contributor Author

benjaminjkraft commented May 15, 2022

A related issue: gqlparser will validate any mutation against a (legal) schema with no mutation type:

# schema
type Query { f: String }

# operation
mutation { g { h } }

@StevenACoffman
Copy link
Collaborator

Yeah, this is an area for improvement. PRs welcome.

@benjaminjkraft
Copy link
Contributor Author

Ok, maybe I can figure out how to fix this. I'll take a look.

@benjaminjkraft
Copy link
Contributor Author

Ok, I have a draft change for this, but on further investigation this also affects graphql-js, so I'm gonna submit it there first to make sure we're fixing it in a consistent way, especially w/r/t whether a schema with no query type is valid.

@StevenACoffman
Copy link
Collaborator

Yeah, as you probably saw, we import the graphql-js tests https://github.com/vektah/gqlparser/tree/master/validator/imported so fixing it upstream would help a wider community beyond even the Go ecosystem.

benjaminjkraft added a commit to benjaminjkraft/gqlparser that referenced this issue Jun 6, 2022
Somehow we were not validating this!  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.  Ideally
we'd prohibit schemas without a query type entirely (spec says they need
one) but that caused some problems in tests and it wouldn't surprise me
if it causes problems in real life too (since an extension schema,
validated on its own, may look the same).  So we just prevent it at
query time.

Fixes vektah#221.
benjaminjkraft added a commit to benjaminjkraft/gqlparser that referenced this issue 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 that referenced this issue 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants