Skip to content

Commit a5c80ac

Browse files
authored
Merge pull request #431 from linniksa/fix-unsupported-operation-error
Fix panic when mutations or subscriptions is not configured
2 parents 21fe71d + 90fefa5 commit a5c80ac

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

executor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ func executeOperation(p executeOperationParams) *Result {
175175
// Extracts the root type of the operation from the schema.
176176
func getOperationRootType(schema Schema, operation ast.Definition) (*Object, error) {
177177
if operation == nil {
178-
return nil, errors.New("Can only execute queries and mutations")
178+
return nil, errors.New("Can only execute queries, mutations and subscription")
179179
}
180180

181181
switch operation.GetOperation() {
182182
case ast.OperationTypeQuery:
183183
return schema.QueryType(), nil
184184
case ast.OperationTypeMutation:
185185
mutationType := schema.MutationType()
186-
if mutationType.PrivateName == "" {
186+
if mutationType == nil || mutationType.PrivateName == "" {
187187
return nil, gqlerrors.NewError(
188188
"Schema is not configured for mutations",
189189
[]ast.Node{operation},
@@ -196,7 +196,7 @@ func getOperationRootType(schema Schema, operation ast.Definition) (*Object, err
196196
return mutationType, nil
197197
case ast.OperationTypeSubscription:
198198
subscriptionType := schema.SubscriptionType()
199-
if subscriptionType.PrivateName == "" {
199+
if subscriptionType == nil || subscriptionType.PrivateName == "" {
200200
return nil, gqlerrors.NewError(
201201
"Schema is not configured for subscriptions",
202202
[]ast.Node{operation},

executor_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,57 @@ func TestThrowsIfUnknownOperationNameIsProvided(t *testing.T) {
843843
t.Fatalf("unexpected result, Diff: %v", testutil.Diff(expectedErrors, result.Errors))
844844
}
845845
}
846+
847+
func TestThrowsIfOperationTypeIsUnsupported(t *testing.T) {
848+
query := `mutation Mut { a } subscription Sub { a }`
849+
operations := []string{"Mut", "Sub"}
850+
851+
expectedErrors := [][]gqlerrors.FormattedError{
852+
{{
853+
Message: `Schema is not configured for mutations`,
854+
Locations: []location.SourceLocation{{Line: 1, Column: 1}},
855+
}},
856+
{{
857+
Message: `Schema is not configured for subscriptions`,
858+
Locations: []location.SourceLocation{{Line: 1, Column: 20}},
859+
}},
860+
}
861+
862+
schema, err := graphql.NewSchema(graphql.SchemaConfig{
863+
Query: graphql.NewObject(graphql.ObjectConfig{
864+
Name: "Query",
865+
Fields: graphql.Fields{
866+
"a": &graphql.Field{
867+
Type: graphql.String,
868+
},
869+
},
870+
}),
871+
})
872+
if err != nil {
873+
t.Fatalf("Error in schema %v", err.Error())
874+
}
875+
876+
// parse query
877+
ast := testutil.TestParse(t, query)
878+
879+
for opIndex, operation := range operations {
880+
expectedErrors := expectedErrors[opIndex]
881+
882+
// execute
883+
ep := graphql.ExecuteParams{
884+
Schema: schema,
885+
AST: ast,
886+
OperationName: operation,
887+
}
888+
result := testutil.TestExecute(t, ep)
889+
if result.Data != nil {
890+
t.Fatalf("wrong result, expected nil result.Data, got %v", result.Data)
891+
}
892+
if !testutil.EqualFormattedErrors(expectedErrors, result.Errors) {
893+
t.Fatalf("unexpected result, Diff: %v", testutil.Diff(expectedErrors, result.Errors))
894+
}
895+
}
896+
}
846897
func TestUsesTheQuerySchemaForQueries(t *testing.T) {
847898

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

0 commit comments

Comments
 (0)