Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

add introspection tests #17

Merged
merged 5 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
supported schema description and add test for it
  • Loading branch information
Code-Hex committed Apr 6, 2021
commit c12f709d344c8a6ac833f644a02dd1b8784853cb
20 changes: 17 additions & 3 deletions internal/gqlgen/gqlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,22 @@ func NewExecutableSchema(filenames ...string) (*ExecutableSchema, error) {
}

type ExecutableSchema struct {
ParsedSchema *ast.Schema
ParsedSchema *ast.Schema
wantReservedTypes bool
}

func (e *ExecutableSchema) Exec(oc *graphql.OperationContext) *bytes.Buffer {
type ExecOption func(es *ExecutableSchema)

func WithReservedTypes(want bool) ExecOption {
return func(es *ExecutableSchema) {
es.wantReservedTypes = want
}
}

func (e *ExecutableSchema) Exec(oc *graphql.OperationContext, opts ...ExecOption) *bytes.Buffer {
for _, opt := range opts {
opt(e)
}
ec := executionContext{oc, e}
data := ec._Query(context.Background(), oc.Operation.SelectionSet)
var buf bytes.Buffer
Expand All @@ -100,7 +112,7 @@ type executionContext struct {
}

func (ec *executionContext) introspectSchema() *wrapper.Schema {
return wrapper.WrapSchema(ec.ParsedSchema)
return wrapper.WrapSchema(ec.ParsedSchema, wrapper.WithReservedTypes(ec.wantReservedTypes))
}

func (ec *executionContext) introspectType(name string) *wrapper.Type {
Expand Down Expand Up @@ -559,6 +571,8 @@ func (ec *executionContext) ___Schema(ctx context.Context, sel ast.SelectionSet,
if out.Values[i] == gql.Null {
invalids++
}
case "description":
out.Values[i] = gql.MarshalString(obj.Description())
default:
panic("unknown field " + strconv.Quote(field.Name))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/gqlgen/gqlgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestExec(t *testing.T) {
ParsedSchema: AST,
}

resp := es.Exec(oc)
resp := es.Exec(oc, gqlgen.WithReservedTypes(true))

return resp.Bytes(), nil
})
Expand Down
4 changes: 3 additions & 1 deletion internal/gqlgen/gqlgen_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ tests:
}
query: |
{
__schema { description }
__schema {
description
}
}
json: |
{"__schema":{"description":"This is a simple schema"}}
14 changes: 13 additions & 1 deletion internal/wrapper/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ import (
"github.com/Code-Hex/gqlparser/v2/ast"
)

type WrapSchemaOption func(s *Schema)

func WithReservedTypes(want bool) WrapSchemaOption {
return func(s *Schema) {
s.wantReservedTypes = want
}
}

type Schema struct {
schema *ast.Schema
schema *ast.Schema
wantReservedTypes bool
}

func (s *Schema) Types() []Type {
types := make([]Type, 0, len(s.schema.Types))
for _, typ := range s.schema.Types {
if !s.wantReservedTypes && strings.HasPrefix(typ.Name, "__") {
continue
}
types = append(types, *WrapTypeFromDef(s.schema, typ))
}
sort.Slice(types, func(i, j int) bool {
Expand Down
8 changes: 6 additions & 2 deletions internal/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ type (
}
)

func WrapSchema(schema *ast.Schema) *Schema {
return &Schema{schema: schema}
func WrapSchema(schema *ast.Schema, opts ...WrapSchemaOption) *Schema {
s := &Schema{schema: schema}
for _, opt := range opts {
opt(s)
}
return s
}

func (f *EnumValue) IsDeprecated() bool {
Expand Down