Skip to content

Commit

Permalink
Validate subscriptions in Operation function (#5926)
Browse files Browse the repository at this point in the history
We realised that as a result of b40c632, introspection queries started failing in Insomnia because subscription didn't exist in the types. Instead of adding more hacks there, we add validation for this inside Operation. We already do something similar for Queries/Mutations.
  • Loading branch information
pawanrawal committed Jul 10, 2020
1 parent b40c632 commit e33850e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ type Student implements People {
taughtBy: [Teacher] @hasInverse(field: "teaches")
}

type Message {
type Message @withSubscription {
content: String! @dgraph(pred: "post")
author: String @dgraph(pred: "<职业>")
}
7 changes: 7 additions & 0 deletions graphql/resolve/resolver_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ func TestManyMutationsWithError(t *testing.T) {
}
}

func TestSubscriptionErrorWhenNoneDefined(t *testing.T) {
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
resp := resolveWithClient(gqlSchema, `subscription { foo }`, nil, nil)
test.RequireJSONEq(t, x.GqlErrorList{{Message: "Not resolving subscription because schema" +
" doesn't have any fields defined for subscription operation."}}, resp.Errors)
}

func resolve(gqlSchema schema.Schema, gqlQuery string, dgResponse string) *schema.Response {
return resolveWithClient(gqlSchema, gqlQuery, nil, &executor{resp: dgResponse})
}
Expand Down
6 changes: 6 additions & 0 deletions graphql/schema/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func (s *schema) Operation(req *Request) (Operation, error) {
return nil, listErr
}

if len(doc.Operations) == 1 && doc.Operations[0].Operation == ast.Subscription &&
s.schema.Subscription == nil {
return nil, errors.Errorf("Not resolving subscription because schema doesn't have any " +
"fields defined for subscription operation.")
}

if len(doc.Operations) > 1 && req.OperationName == "" {
return nil, errors.Errorf("Operation name must by supplied when query has more " +
"than 1 operation.")
Expand Down
6 changes: 0 additions & 6 deletions graphql/schema/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,6 @@ func customMappings(s *ast.Schema) map[string]map[string]*ast.Directive {

// AsSchema wraps a github.com/vektah/gqlparser/ast.Schema.
func AsSchema(s *ast.Schema) (Schema, error) {

// vektah/gqlparser library doesn't validate subscriptions properly if s.Subscription == nil.
//s.Subscription is nil when there is no type with @withSubscription true, so we are handling that case.
if s.Subscription == nil {
s.Subscription = &ast.Definition{Name: "Subscription"}
}
// Auth rules can't be effectively validated as part of the normal rules -
// because they need the fully generated schema to be checked against.
authRules, err := authRules(s)
Expand Down

0 comments on commit e33850e

Please sign in to comment.