diff --git a/graphql.go b/graphql.go index 2bc1639f..427fa9c4 100644 --- a/graphql.go +++ b/graphql.go @@ -39,7 +39,7 @@ func ParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) ( } if resolver != nil { - r, err := resolvable.ApplyResolver(s.schema, resolver, s.useFieldResolvers) + r, err := resolvable.ApplyResolver(s.schema, resolver) if err != nil { return nil, err } @@ -63,10 +63,9 @@ type Schema struct { schema *schema.Schema res *resolvable.Schema - maxParallelism int - tracer trace.Tracer - logger log.Logger - useFieldResolvers bool + maxParallelism int + tracer trace.Tracer + logger log.Logger } // SchemaOpt is an option to pass to ParseSchema or MustParseSchema. @@ -96,7 +95,7 @@ func Logger(logger log.Logger) SchemaOpt { // Specifies whether to use struct field resolvers func UseFieldResolvers() SchemaOpt { return func(s *Schema) { - s.useFieldResolvers = true + s.schema.UseFieldResolvers = true } } diff --git a/graphql_test.go b/graphql_test.go index 385b8e21..1c1bd082 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -61,7 +61,7 @@ func (r *timeResolver) AddHour(args struct{ Time graphql.Time }) graphql.Time { return graphql.Time{Time: args.Time.Add(time.Hour)} } -var starwarsSchema = graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{}, nil) +var starwarsSchema = graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{}) func TestHelloWorld(t *testing.T) { gqltesting.RunTests(t, []*gqltesting.Test{ diff --git a/internal/exec/resolvable/resolvable.go b/internal/exec/resolvable/resolvable.go index 165e099f..920a0ea7 100644 --- a/internal/exec/resolvable/resolvable.go +++ b/internal/exec/resolvable/resolvable.go @@ -56,14 +56,9 @@ func (*Object) isResolvable() {} func (*List) isResolvable() {} func (*Scalar) isResolvable() {} -// TODO figure out a better way to handle passed config -// this approach avoids updating signature of many functions -var useFieldResolvers bool - -func ApplyResolver(s *schema.Schema, resolver interface{}, useFieldRes bool) (*Schema, error) { +func ApplyResolver(s *schema.Schema, resolver interface{}) (*Schema, error) { b := newBuilder(s) - useFieldResolvers = useFieldRes var query, mutation Resolvable @@ -232,7 +227,7 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p * 2) Otherwise use resolver type's field */ if isResolverSchemaOrType(rt) == true || len(f.Args) > 0 || - useFieldResolvers == false || rt.Kind() == reflect.Interface { + b.schema.UseFieldResolvers == false || rt.Kind() == reflect.Interface { methodIndex = findMethod(resolverType, f.Name) } else { fieldIndex = findField(rt, f.Name) @@ -266,7 +261,7 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p * 2) Or it is configured to use method */ typeAssertions := make(map[string]*TypeAssertion) - if isResolverSchemaOrType(rt) == true || useFieldResolvers == false { + if isResolverSchemaOrType(rt) == true || b.schema.UseFieldResolvers == false { for _, impl := range possibleTypes { methodIndex := findMethod(resolverType, "To"+impl.Name) if methodIndex == -1 { diff --git a/internal/schema/schema.go b/internal/schema/schema.go index 62d6de12..03b0e061 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -45,6 +45,8 @@ type Schema struct { objects []*Object unions []*Union enums []*Enum + + UseFieldResolvers bool } // Resolve a named type in the schema by its name.