Skip to content

Commit

Permalink
removed SchemaBuilder
Browse files Browse the repository at this point in the history
It is not necessary any more. Simpler API wins.
  • Loading branch information
neelance committed Dec 31, 2016
1 parent 518a5fe commit cdef856
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ func (id *ID) UnmarshalGraphQL(input interface{}) error {
}

func ParseSchema(schemaString string, resolver interface{}) (*Schema, error) {
b := New()
if err := b.Parse(schemaString); err != nil {
s := &Schema{
schema: schema.New(),
}
if err := s.schema.Parse(schemaString); err != nil {
return nil, err
}
return b.ApplyResolver(resolver)

if resolver != nil {
e, err := exec.Make(s.schema, resolver)
if err != nil {
return nil, err
}
s.exec = e
}

return s, nil
}

func MustParseSchema(schemaString string, resolver interface{}) *Schema {
Expand All @@ -56,39 +67,6 @@ func MustParseSchema(schemaString string, resolver interface{}) *Schema {
return s
}

type SchemaBuilder struct {
schema *schema.Schema
}

func New() *SchemaBuilder {
return &SchemaBuilder{
schema: schema.New(),
}
}

func (b *SchemaBuilder) Parse(schemaString string) error {
return b.schema.Parse(schemaString)
}

func (b *SchemaBuilder) ApplyResolver(resolver interface{}) (*Schema, error) {
e, err2 := exec.Make(b.schema, resolver)
if err2 != nil {
return nil, err2
}
return &Schema{
schema: b.schema,
exec: e,
}, nil
}

func (b *SchemaBuilder) ToJSON() ([]byte, error) {
result, err := exec.IntrospectSchema(b.schema)
if err != nil {
return nil, err
}
return json.MarshalIndent(result, "", "\t")
}

type Schema struct {
schema *schema.Schema
exec *exec.Exec
Expand All @@ -101,6 +79,10 @@ type Response struct {
}

func (s *Schema) Exec(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response {
if s.exec == nil {
panic("schema created without resolver, can not exec")
}

document, err := query.Parse(queryString, s.schema.Resolve)
if err != nil {
return &Response{
Expand Down Expand Up @@ -128,3 +110,11 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
Errors: errs,
}
}

func (s *Schema) ToJSON() ([]byte, error) {
result, err := exec.IntrospectSchema(s.schema)
if err != nil {
return nil, err
}
return json.MarshalIndent(result, "", "\t")
}

0 comments on commit cdef856

Please sign in to comment.