Skip to content

Commit

Permalink
Fix panic when mutations or subscriptions is not configured
Browse files Browse the repository at this point in the history
  • Loading branch information
linniksa committed Jan 3, 2019
1 parent 21fe71d commit 90fefa5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ func executeOperation(p executeOperationParams) *Result {
// Extracts the root type of the operation from the schema.
func getOperationRootType(schema Schema, operation ast.Definition) (*Object, error) {
if operation == nil {
return nil, errors.New("Can only execute queries and mutations")
return nil, errors.New("Can only execute queries, mutations and subscription")
}

switch operation.GetOperation() {
case ast.OperationTypeQuery:
return schema.QueryType(), nil
case ast.OperationTypeMutation:
mutationType := schema.MutationType()
if mutationType.PrivateName == "" {
if mutationType == nil || mutationType.PrivateName == "" {
return nil, gqlerrors.NewError(
"Schema is not configured for mutations",
[]ast.Node{operation},
Expand All @@ -196,7 +196,7 @@ func getOperationRootType(schema Schema, operation ast.Definition) (*Object, err
return mutationType, nil
case ast.OperationTypeSubscription:
subscriptionType := schema.SubscriptionType()
if subscriptionType.PrivateName == "" {
if subscriptionType == nil || subscriptionType.PrivateName == "" {
return nil, gqlerrors.NewError(
"Schema is not configured for subscriptions",
[]ast.Node{operation},
Expand Down
51 changes: 51 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,57 @@ func TestThrowsIfUnknownOperationNameIsProvided(t *testing.T) {
t.Fatalf("unexpected result, Diff: %v", testutil.Diff(expectedErrors, result.Errors))
}
}

func TestThrowsIfOperationTypeIsUnsupported(t *testing.T) {
query := `mutation Mut { a } subscription Sub { a }`
operations := []string{"Mut", "Sub"}

expectedErrors := [][]gqlerrors.FormattedError{
{{
Message: `Schema is not configured for mutations`,
Locations: []location.SourceLocation{{Line: 1, Column: 1}},
}},
{{
Message: `Schema is not configured for subscriptions`,
Locations: []location.SourceLocation{{Line: 1, Column: 20}},
}},
}

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}

// parse query
ast := testutil.TestParse(t, query)

for opIndex, operation := range operations {
expectedErrors := expectedErrors[opIndex]

// execute
ep := graphql.ExecuteParams{
Schema: schema,
AST: ast,
OperationName: operation,
}
result := testutil.TestExecute(t, ep)
if result.Data != nil {
t.Fatalf("wrong result, expected nil result.Data, got %v", result.Data)
}
if !testutil.EqualFormattedErrors(expectedErrors, result.Errors) {
t.Fatalf("unexpected result, Diff: %v", testutil.Diff(expectedErrors, result.Errors))
}
}
}
func TestUsesTheQuerySchemaForQueries(t *testing.T) {

doc := `query Q { a } mutation M { c } subscription S { a }`
Expand Down

0 comments on commit 90fefa5

Please sign in to comment.