Skip to content

Commit 7717e95

Browse files
Remove the internal schema as a package level var to fix race issues (#148)
This triggers the Go race detector when using the gateway in tests. There is very little advantage to having the package level var.
1 parent 6a08012 commit 7717e95

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

gateway.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,20 @@ func (g *Gateway) Execute(ctx *RequestContext, plans QueryPlanList) (map[string]
106106
return result, nil
107107
}
108108

109-
func (g *Gateway) internalSchema() *ast.Schema {
109+
func (g *Gateway) internalSchema() (*ast.Schema, error) {
110110
// we start off with the internal schema
111-
schema := internalSchema
111+
schema, err := graphql.LoadSchema(`
112+
interface Node {
113+
id: ID!
114+
}
115+
116+
type Query {
117+
node(id: ID!): Node
118+
}
119+
`)
120+
if schema == nil {
121+
return nil, fmt.Errorf("Syntax error in schema string: %s", err.Error())
122+
}
112123

113124
// then we have to add any query fields we have
114125
for _, field := range g.queryFields {
@@ -120,7 +131,7 @@ func (g *Gateway) internalSchema() *ast.Schema {
120131
}
121132

122133
// we're done
123-
return schema
134+
return schema, nil
124135
}
125136

126137
// New instantiates a new schema with the required stuffs.
@@ -161,7 +172,10 @@ func New(sources []*graphql.RemoteSchema, configs ...Option) (*Gateway, error) {
161172
}
162173
}
163174

164-
internal := gateway.internalSchema()
175+
internal, err := gateway.internalSchema()
176+
if err != nil {
177+
return nil, err
178+
}
165179
// find the field URLs before we merge schemas. We need to make sure to include
166180
// the fields defined by the gateway's internal schema
167181
urls := fieldURLs(sources, true).Concat(

internal.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gateway
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/99designs/gqlgen/graphql/introspection"
87
"github.com/mitchellh/mapstructure"
@@ -11,10 +10,6 @@ import (
1110
"github.com/nautilus/graphql"
1211
)
1312

14-
// internalSchema is a graphql schema that exists at the gateway level and is merged with the
15-
// other schemas that the gateway wraps.
16-
var internalSchema *ast.Schema
17-
1813
// internalSchemaLocation is the location that functions should take to identify a remote schema
1914
// that points to the gateway's internal schema.
2015
const internalSchemaLocation = "🎉"
@@ -310,21 +305,3 @@ func (g *Gateway) introspectDirectiveSlice(directives []introspection.Directive,
310305

311306
return result
312307
}
313-
314-
func init() {
315-
// load the internal
316-
schema, err := graphql.LoadSchema(`
317-
interface Node {
318-
id: ID!
319-
}
320-
321-
type Query {
322-
node(id: ID!): Node
323-
}
324-
`)
325-
if schema == nil {
326-
panic(fmt.Sprintf("Syntax error in schema string: %s", err.Error()))
327-
}
328-
329-
internalSchema = schema
330-
}

0 commit comments

Comments
 (0)