Skip to content

Commit

Permalink
Enable merging of unions in merger
Browse files Browse the repository at this point in the history
A union's possibleTypes includes all members of the union and not
itself.
See issue nautilus#74: nautilus#74
  • Loading branch information
edwinmo-splunk committed Aug 12, 2019
1 parent 4874e3a commit d2a7d70
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
20 changes: 18 additions & 2 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ func mergeSchemas(sources []*ast.Schema) (*ast.Schema, error) {
}
}

possibleTypesSet := map[string]Set{}

// merge each definition of each type into one
for name, definitions := range types {
if _, exists := possibleTypesSet[name]; !exists {
possibleTypesSet[name] = Set{}
}
for _, definition := range definitions {
// look up if the type is already registered in the aggregate
previousDefinition, exists := result.Types[name]
Expand All @@ -92,8 +97,19 @@ func mergeSchemas(sources []*ast.Schema) (*ast.Schema, error) {
// use the declaration that we got from the new schema
result.Types[name] = definition

// register the type as an implementer of itself
result.AddPossibleType(name, definition)
if definition.Kind == ast.Union {
for _, possibleType := range definition.Types {
for _, typedef := range types[possibleType] {
if !possibleTypesSet[name].Has(typedef.Name) {
possibleTypesSet[name].Add(typedef.Name)
result.AddPossibleType(name, typedef)
}
}
}
} else {
// register the type as an implementer of itself
result.AddPossibleType(name, definition)
}

// each interface that this type implements needs to be registered
for _, iface := range definition.Interfaces {
Expand Down
65 changes: 65 additions & 0 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,71 @@ func TestMergeSchema_union(t *testing.T) {
})
}

func TestMergeSchema_unions(t *testing.T) {
t.Run("Matching", func(t *testing.T) {
originalSchema, err := graphql.LoadSchema(`
type Foo {
name: String!
}
type Bar {
lastName: String!
}
union Foobar = Foo | Bar
`)
if !assert.Nil(t, err, "original schema didn't parse") {
return
}

// merge the schema with a compatible schema
schema, err := testMergeSchemas(t, originalSchema, `
type Baz {
name: String!
}
type Qux {
middleName: String!
}
union Bazqux = Baz | Qux
`)
if err != nil {
t.Error(err.Error())
return
}

possibleTypes := schema.GetPossibleTypes(schema.Types["Foobar"])
if len(possibleTypes) != 2 {
t.Errorf("Union has incorrect number of types. Expected 2, found %v", len(schema.GetPossibleTypes(schema.Types["Foobar"])))
return
}

// keep the unique set of the types we visisted
visited := Set{}
for _, possibleType := range possibleTypes {
visited.Add(possibleType.Name)
}

assert.True(t, visited["Foo"], "did not have Bar in possible type")
assert.True(t, visited["Bar"], "did not have Baz in possible type")

possibleTypes = schema.GetPossibleTypes(schema.Types["Bazqux"])
if len(possibleTypes) != 2 {
t.Errorf("Union has incorrect number of types. Expected 2, found %v", len(schema.GetPossibleTypes(schema.Types["Bazqux"])))
return
}

visited = Set{}
for _, possibleType := range possibleTypes {
visited.Add(possibleType.Name)
}

assert.True(t, visited["Baz"], "did not have Bar in possible type")
assert.True(t, visited["Qux"], "did not have Baz in possible type")
})
}

func TestMergeSchema_interfaces(t *testing.T) {
t.Run("Matching", func(t *testing.T) {
// the directive that we are always comparing to
Expand Down

0 comments on commit d2a7d70

Please sign in to comment.