Skip to content

Commit

Permalink
added some method documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 11, 2017
1 parent 91bd7f8 commit f451652
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const OpenTracingTagTrivial = "graphql.trivial"
const OpenTracingTagArgsPrefix = "graphql.args."
const OpenTracingTagError = "graphql.error"

// ID represents GraphQL's "ID" type. A custom type may be used instead.
type ID string

func (_ ID) ImplementsGraphQLType(name string) bool {
Expand All @@ -41,6 +42,9 @@ 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) {
s := &Schema{
schema: schema.New(),
Expand All @@ -61,6 +65,7 @@ func ParseSchema(schemaString string, resolver interface{}) (*Schema, error) {
return s, nil
}

// MustParseSchema calls ParseSchema and panics on error.
func MustParseSchema(schemaString string, resolver interface{}) *Schema {
s, err := ParseSchema(schemaString, resolver)
if err != nil {
Expand All @@ -69,6 +74,7 @@ func MustParseSchema(schemaString string, resolver interface{}) *Schema {
return s
}

// Schema represents a GraphQL schema with an optional resolver.
type Schema struct {
schema *schema.Schema
exec *exec.Exec
Expand All @@ -77,12 +83,17 @@ type Schema struct {
MaxParallelism int
}

// Response represents a typical response of a GraphQL server. It may be encoded to JSON directly or
// it may be further processed to a custom response type, for example to include custom error data.
type Response struct {
Data interface{} `json:"data,omitempty"`
Errors []*errors.QueryError `json:"errors,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

// Exec executes the given query with the schema's resolver. It panics if the schema was created
// without a resolver. If the context get cancelled, no further resolvers will be called and a
// the context error will be returned as soon as possible (not immediately).
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")
Expand Down Expand Up @@ -116,10 +127,12 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
}
}

// Inspect allows inspection of the given schema.
func (s *Schema) Inspect() *introspection.Schema {
return introspection.WrapSchema(s.schema)
}

// ToJSON encodes the schema in a JSON format used by tools like Relay.
func (s *Schema) ToJSON() ([]byte, error) {
result, err := exec.IntrospectSchema(s.schema)
if err != nil {
Expand Down

0 comments on commit f451652

Please sign in to comment.