Skip to content

Commit 838c677

Browse files
authored
Validate subscriptions in Operation function (#5926) (#5983)
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. (cherry picked from commit e33850e)
1 parent fc8104e commit 838c677

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

graphql/e2e/directives/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ type Student implements People {
139139
taughtBy: [Teacher] @hasInverse(field: "teaches")
140140
}
141141

142-
type Message {
142+
type Message @withSubscription {
143143
content: String! @dgraph(pred: "post")
144144
author: String @dgraph(pred: "<职业>")
145145
}

graphql/resolve/resolver_error_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ func TestManyMutationsWithError(t *testing.T) {
399399
}
400400
}
401401

402+
func TestSubscriptionErrorWhenNoneDefined(t *testing.T) {
403+
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
404+
resp := resolveWithClient(gqlSchema, `subscription { foo }`, nil, nil)
405+
test.RequireJSONEq(t, x.GqlErrorList{{Message: "Not resolving subscription because schema" +
406+
" doesn't have any fields defined for subscription operation."}}, resp.Errors)
407+
}
408+
402409
func resolve(gqlSchema schema.Schema, gqlQuery string, dgResponse string) *schema.Response {
403410
return resolveWithClient(gqlSchema, gqlQuery, nil, &executor{resp: dgResponse})
404411
}

graphql/schema/request.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ func (s *schema) Operation(req *Request) (Operation, error) {
5555
return nil, listErr
5656
}
5757

58+
if len(doc.Operations) == 1 && doc.Operations[0].Operation == ast.Subscription &&
59+
s.schema.Subscription == nil {
60+
return nil, errors.Errorf("Not resolving subscription because schema doesn't have any " +
61+
"fields defined for subscription operation.")
62+
}
63+
5864
if len(doc.Operations) > 1 && req.OperationName == "" {
5965
return nil, errors.Errorf("Operation name must by supplied when query has more " +
6066
"than 1 operation.")

graphql/schema/wrappers.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,6 @@ func customMappings(s *ast.Schema) map[string]map[string]*ast.Directive {
568568

569569
// AsSchema wraps a github.com/vektah/gqlparser/ast.Schema.
570570
func AsSchema(s *ast.Schema) (Schema, error) {
571-
572-
// vektah/gqlparser library doesn't validate subscriptions properly if s.Subscription == nil.
573-
//s.Subscription is nil when there is no type with @withSubscription true, so we are handling that case.
574-
if s.Subscription == nil {
575-
s.Subscription = &ast.Definition{Name: "Subscription"}
576-
}
577571
// Auth rules can't be effectively validated as part of the normal rules -
578572
// because they need the fully generated schema to be checked against.
579573
authRules, err := authRules(s)

0 commit comments

Comments
 (0)