diff --git a/graphql/e2e/directives/schema.graphql b/graphql/e2e/directives/schema.graphql index 1670926d759..05580be0307 100644 --- a/graphql/e2e/directives/schema.graphql +++ b/graphql/e2e/directives/schema.graphql @@ -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: "<职业>") } diff --git a/graphql/resolve/resolver_error_test.go b/graphql/resolve/resolver_error_test.go index 6ceffbd68c1..3857f54a873 100644 --- a/graphql/resolve/resolver_error_test.go +++ b/graphql/resolve/resolver_error_test.go @@ -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}) } diff --git a/graphql/schema/request.go b/graphql/schema/request.go index fb2f0ce4b25..808dbc40241 100644 --- a/graphql/schema/request.go +++ b/graphql/schema/request.go @@ -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.") diff --git a/graphql/schema/wrappers.go b/graphql/schema/wrappers.go index 75762cf498f..d93295f7a95 100644 --- a/graphql/schema/wrappers.go +++ b/graphql/schema/wrappers.go @@ -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)