Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Salman Ahmad committed Apr 5, 2018
1 parent b463cbc commit b0effb8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
11 changes: 5 additions & 6 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
11 changes: 3 additions & 8 deletions internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b0effb8

Please sign in to comment.