Skip to content

Commit

Permalink
Make sure there's a Query node before trying to add a field to it.
Browse files Browse the repository at this point in the history
Federation adds some queries to the schema.  There already existed
code to insert a Query node if none existed previously.  But that code
was only put on addEntityToSchema(), and not the other place we update
the query, addServiceToSchema().

Almost always the old code was good enough, since we call
addEntityToSchema() before addServiceToSchema().  But there's on
exception: when the schema has no `@key`.  In that case we only call
addServiceToSchema(), so we need to do the query-existence check there
too.
  • Loading branch information
csilvers committed Feb 5, 2020
1 parent b941b97 commit 9f2a624
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ func (f *federation) InjectSources(cfg *config.Config) {
cfg.AdditionalSources = append(cfg.AdditionalSources, &ast.Source{Name: "entity.graphql", Input: s, BuiltIn: true})
}

// ensureQuery ensures that a "Query" node exists on the schema.
func ensureQuery(s *ast.Schema) {
if s.Query == nil {
s.Query = &ast.Definition{
Kind: ast.Object,
Name: "Query",
}
s.Types["Query"] = s.Query
}
}

// addEntityToSchema adds the _Entity Union and _entities query to schema.
// This is part of MutateSchema.
func (f *federation) addEntityToSchema(s *ast.Schema) {
Expand Down Expand Up @@ -125,13 +136,7 @@ func (f *federation) addEntityToSchema(s *ast.Schema) {
},
},
}
if s.Query == nil {
s.Query = &ast.Definition{
Kind: ast.Object,
Name: "Query",
}
s.Types["Query"] = s.Query
}
ensureQuery(s)
s.Query.Fields = append(s.Query.Fields, fieldDef)
}

Expand All @@ -155,6 +160,7 @@ func (f *federation) addServiceToSchema(s *ast.Schema) {
Name: "_service",
Type: ast.NonNullNamedType("_Service", nil),
}
ensureQuery(s)
s.Query.Fields = append(s.Query.Fields, _serviceDef)
}

Expand Down

0 comments on commit 9f2a624

Please sign in to comment.