Skip to content

Commit

Permalink
added SchemaOpt
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 27, 2017
1 parent 1dcc575 commit d09dd54
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ func (id *ID) UnmarshalGraphQL(input interface{}) error {
// ParseSchema parses a GraphQL schema and attaches the given root resolver. It returns an error if
// the Go type signature of the resolvers does not match the schema. If nil is passed as the
// resolver, then the schema can not be executed, but it may be inspected (e.g. with ToJSON).
func ParseSchema(schemaString string, resolver interface{}) (*Schema, error) {
func ParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) (*Schema, error) {
s := &Schema{
schema: schema.New(),
MaxParallelism: 10,
Tracer: trace.OpenTracingTracer{},
maxParallelism: 10,
tracer: trace.OpenTracingTracer{},
}
for _, opt := range opts {
opt(s)
}

if err := s.schema.Parse(schemaString); err != nil {
return nil, err
}
Expand All @@ -57,8 +61,8 @@ func ParseSchema(schemaString string, resolver interface{}) (*Schema, error) {
}

// MustParseSchema calls ParseSchema and panics on error.
func MustParseSchema(schemaString string, resolver interface{}) *Schema {
s, err := ParseSchema(schemaString, resolver)
func MustParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) *Schema {
s, err := ParseSchema(schemaString, resolver, opts...)
if err != nil {
panic(err)
}
Expand All @@ -70,11 +74,25 @@ type Schema struct {
schema *schema.Schema
exec *exec.Exec

// MaxParallelism specifies the maximum number of resolvers per request allowed to run in parallel. The default is 10.
MaxParallelism int
maxParallelism int
tracer trace.Tracer
}

// SchemaOpt is an option to pass to ParseSchema or MustParseSchema.
type SchemaOpt func(*Schema)

// Tracer is used to trace queries and fields. It defaults to trace.OpenTracingTracer.
Tracer trace.Tracer
// MaxParallelism specifies the maximum number of resolvers per request allowed to run in parallel. The default is 10.
func MaxParallelism(n int) SchemaOpt {
return func(s *Schema) {
s.maxParallelism = n
}
}

// Tracer is used to trace queries and fields. It defaults to trace.OpenTracingTracer.
func Tracer(tracer trace.Tracer) SchemaOpt {
return func(s *Schema) {
s.tracer = tracer
}
}

// Response represents a typical response of a GraphQL server. It may be encoded to JSON directly or
Expand Down Expand Up @@ -112,8 +130,8 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
Doc: doc,
Vars: variables,
Schema: s.schema,
Limiter: make(chan struct{}, s.MaxParallelism),
Tracer: s.Tracer,
Limiter: make(chan struct{}, s.maxParallelism),
Tracer: s.tracer,
}
varTypes := make(map[string]*introspection.Type)
for _, v := range op.Vars {
Expand All @@ -123,7 +141,7 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
}
varTypes[v.Name.Name] = introspection.WrapType(t)
}
traceCtx, finish := s.Tracer.TraceQuery(ctx, queryString, operationName, variables, varTypes)
traceCtx, finish := s.tracer.TraceQuery(ctx, queryString, operationName, variables, varTypes)
data, errs := r.Execute(traceCtx, s.exec, op)
finish(errs)

Expand Down

0 comments on commit d09dd54

Please sign in to comment.