Skip to content

Commit

Permalink
added SchemaToJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 28, 2016
1 parent f6b498a commit 2cf7fcc
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
15 changes: 15 additions & 0 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql

import (
"context"
"encoding/json"

"github.com/neelance/graphql-go/errors"
"github.com/neelance/graphql-go/internal/exec"
Expand Down Expand Up @@ -74,3 +75,17 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
Errors: errs,
}
}

func SchemaToJSON(schemaString string) ([]byte, error) {
s, err := schema.Parse(schemaString)
if err != nil {
return nil, err
}

result, err2 := exec.IntrospectSchema(s)
if err2 != nil {
return nil, err
}

return json.Marshal(result)
}
109 changes: 109 additions & 0 deletions internal/exec/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func init() {
}
}

func IntrospectSchema(s *schema.Schema) (interface{}, error) {
r := &request{
schema: s,
doc: introspectionQuery,
}
return introspectSchema(r, introspectionQuery.Operations["IntrospectionQuery"].SelSet), nil
}

func introspectSchema(r *request, selSet *query.SelectionSet) interface{} {
return schemaExec.exec(r, selSet, reflect.ValueOf(&schemaResolver{r.schema}), false)
}
Expand Down Expand Up @@ -452,3 +460,104 @@ func (r *directiveResolver) Locations() []string {
func (r *directiveResolver) Args() []*inputValueResolver {
panic("TODO")
}

var introspectionQuery *query.Document

func init() {
var err *errors.GraphQLError
introspectionQuery, err = query.Parse(introspectionQuerySrc)
if err != nil {
panic(err)
}
}

var introspectionQuerySrc = `
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
`

0 comments on commit 2cf7fcc

Please sign in to comment.