Skip to content

Commit

Permalink
introspection: __type
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 19, 2016
1 parent 99a3752 commit 0c8c943
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
20 changes: 20 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,26 @@ var tests = []struct {
}
`,
},

{
name: "StarWarsIntrospection3",
schema: starwars.Schema,
resolver: &starwars.Resolver{},
query: `
{
__type(name: "Droid") {
name
}
}
`,
result: `
{
"__type": {
"name": "Droid"
}
}
`,
},
}

func TestAll(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ func (e *objectExec) execSelectionSet(r *request, selSet *query.SelectionSet, re
}

case "__schema":
addResult(f.Alias, introspect(r, f.SelSet))
addResult(f.Alias, introspectSchema(r, f.SelSet))

case "__type":
addResult(f.Alias, introspectType(r, execValue(r, f.Arguments["name"]).(string), f.SelSet))

default:
fe, ok := e.fields[f.Name]
Expand Down
30 changes: 21 additions & 9 deletions internal/exec/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,41 @@ import (
"github.com/neelance/graphql-go/internal/schema"
)

var metaExec *Exec
var metaSchema *schema.Schema
var schemaExec iExec
var typeExec iExec

func init() {
metaSchema, err := schema.Parse(metaSchemaSrc, "")
var err error
metaSchema, err = schema.Parse(metaSchemaSrc, "")
if err != nil {
panic(err)
}

metaExec, err = Make(metaSchema, &schemaResolver{})
schemaExec, err = makeExec(metaSchema, metaSchema.Types["__Schema"], reflect.TypeOf(&schemaResolver{}), make(map[typeRefMapKey]*typeRefExec))
if err != nil {
panic(err)
}

typeExec, err = makeExec(metaSchema, metaSchema.Types["__Type"], reflect.TypeOf(&typeResolver{}), make(map[typeRefMapKey]*typeRefExec))
if err != nil {
panic(err)
}
}

func introspect(r *request, selSet *query.SelectionSet) interface{} {
return metaExec.exec(r, selSet, reflect.ValueOf(&schemaResolver{r.Schema}))
func introspectSchema(r *request, selSet *query.SelectionSet) interface{} {
return schemaExec.exec(r, selSet, reflect.ValueOf(&schemaResolver{r.Schema}))
}

var metaSchemaSrc = `
schema {
query: __Schema
func introspectType(r *request, name string, selSet *query.SelectionSet) interface{} {
t, ok := r.Schema.Types[name]
if !ok {
return nil
}
return typeExec.exec(r, selSet, reflect.ValueOf(&typeResolver{typ: t}))
}

var metaSchemaSrc = `
type __Schema {
types: [__Type!]!
queryType: __Type!
Expand Down Expand Up @@ -130,7 +142,7 @@ func (r *schemaResolver) Types() []*typeResolver {
}
}
addTypes(r.schema)
addTypes(metaExec.schema)
addTypes(metaSchema)
for _, name := range scalarTypeNames {
l = append(l, &typeResolver{scalar: name})
}
Expand Down

0 comments on commit 0c8c943

Please sign in to comment.