Skip to content

Commit

Permalink
Validate that the operation's root type is valid
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benjaminjkraft committed May 18, 2022
1 parent d3d9eb0 commit 832bb4a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions validator/imported/spec/schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,4 @@
}
scalar Any
- ""
35 changes: 35 additions & 0 deletions validator/rules/known_root_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package validator

import (
"fmt"

"github.com/vektah/gqlparser/v2/ast"
. "github.com/vektah/gqlparser/v2/validator"
)

func init() {
AddRule("KnownRootType", func(observers *Events, addError AddErrFunc) {
// A query's root must be a valid type. Surprisingly, this isn't
// checked anywhere else!
observers.OnOperation(func(walker *Walker, operation *ast.OperationDefinition) {
var def *ast.Definition
switch operation.Operation {
case ast.Query, "":
def = walker.Schema.Query
case ast.Mutation:
def = walker.Schema.Mutation
case ast.Subscription:
def = walker.Schema.Subscription
default:
// This shouldn't even parse; if it did we probably need to
// update this switch block to add the new operation type.
panic(fmt.Sprintf(`got unknown operation type "%s"`, operation.Operation))
}
if def == nil {
addError(
Message(`Schema does not support operation type "%s"`, operation.Operation),
At(operation.Position))
}
})
})
}
19 changes: 19 additions & 0 deletions validator/spec/KnownRootTypeRule.spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- name: Known root type
rule: KnownRootType
schema: 0
query: |
query { dog { name } }
- name: Valid root type but not in schema
rule: KnownRootType
schema: 0
query: |
mutation { dog { name } }
errors:
- message: Schema does not support operation type "mutation"
- name: Valid root type but schema is entirely empty
rule: KnownRootType
schema: 20
query: |
{ dog { name } }
errors:
- message: Schema does not support operation type "query"

0 comments on commit 832bb4a

Please sign in to comment.