Skip to content

Commit

Permalink
Merge pull request #144 from maoueh/fix/default-root-operation-types
Browse files Browse the repository at this point in the history
Fix ast.Schema inferred root types on Mutation & Subscription and `shema` definition
  • Loading branch information
lwc authored Apr 7, 2021
2 parents 9e0bf38 + 9b84c4a commit 824dec2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
21 changes: 13 additions & 8 deletions validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,21 @@ func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, *gqlerror.Error) {
}
}

if schema.Query == nil && schema.Types["Query"] != nil {
schema.Query = schema.Types["Query"]
}
// Inferred root operation type names should be performed only when a `schema` directive is
// **not** provided, when it is, `Mutation` and `Subscription` becomes valid types and are not
// assigned as a root operation on the schema.
if len(ast.Schema) == 0 {
if schema.Query == nil && schema.Types["Query"] != nil {
schema.Query = schema.Types["Query"]
}

if schema.Mutation == nil && schema.Types["Mutation"] != nil {
schema.Mutation = schema.Types["Mutation"]
}
if schema.Mutation == nil && schema.Types["Mutation"] != nil {
schema.Mutation = schema.Types["Mutation"]
}

if schema.Subscription == nil && schema.Types["Subscription"] != nil {
schema.Subscription = schema.Types["Subscription"]
if schema.Subscription == nil && schema.Types["Subscription"] != nil {
schema.Subscription = schema.Types["Subscription"]
}
}

if schema.Query != nil {
Expand Down
13 changes: 13 additions & 0 deletions validator/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ func TestLoadSchema(t *testing.T) {
require.Equal(t, "SearchResult", implements[1].Name) // union
})

t.Run("default root operation type names", func(t *testing.T) {
file, err := ioutil.ReadFile("testdata/default_root_operation_type_names.graphql")
require.Nil(t, err)
s, err := LoadSchema(Prelude, &ast.Source{Input: string(file), Name: "TestLoadSchema"})
require.Nil(t, err)

require.Nil(t, s.Mutation)
require.Nil(t, s.Subscription)

require.Equal(t, "Mutation", s.Types["Mutation"].Name)
require.Equal(t, "Subscription", s.Types["Subscription"].Name)
})

t.Run("type extensions", func(t *testing.T) {
file, err := ioutil.ReadFile("testdata/extensions.graphql")
require.Nil(t, err)
Expand Down
16 changes: 16 additions & 0 deletions validator/testdata/default_root_operation_type_names.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema {
query: Query
}

type Query {
mutation: Mutation!
subscription: Subscription!
}

type Mutation {
name: String!
}

type Subscription {
name: String!
}

0 comments on commit 824dec2

Please sign in to comment.