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

Commit c12f709

Browse files
committed
supported schema description and add test for it
1 parent e2f3458 commit c12f709

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

internal/gqlgen/gqlgen.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,22 @@ func NewExecutableSchema(filenames ...string) (*ExecutableSchema, error) {
8383
}
8484

8585
type ExecutableSchema struct {
86-
ParsedSchema *ast.Schema
86+
ParsedSchema *ast.Schema
87+
wantReservedTypes bool
8788
}
8889

89-
func (e *ExecutableSchema) Exec(oc *graphql.OperationContext) *bytes.Buffer {
90+
type ExecOption func(es *ExecutableSchema)
91+
92+
func WithReservedTypes(want bool) ExecOption {
93+
return func(es *ExecutableSchema) {
94+
es.wantReservedTypes = want
95+
}
96+
}
97+
98+
func (e *ExecutableSchema) Exec(oc *graphql.OperationContext, opts ...ExecOption) *bytes.Buffer {
99+
for _, opt := range opts {
100+
opt(e)
101+
}
90102
ec := executionContext{oc, e}
91103
data := ec._Query(context.Background(), oc.Operation.SelectionSet)
92104
var buf bytes.Buffer
@@ -100,7 +112,7 @@ type executionContext struct {
100112
}
101113

102114
func (ec *executionContext) introspectSchema() *wrapper.Schema {
103-
return wrapper.WrapSchema(ec.ParsedSchema)
115+
return wrapper.WrapSchema(ec.ParsedSchema, wrapper.WithReservedTypes(ec.wantReservedTypes))
104116
}
105117

106118
func (ec *executionContext) introspectType(name string) *wrapper.Type {
@@ -559,6 +571,8 @@ func (ec *executionContext) ___Schema(ctx context.Context, sel ast.SelectionSet,
559571
if out.Values[i] == gql.Null {
560572
invalids++
561573
}
574+
case "description":
575+
out.Values[i] = gql.MarshalString(obj.Description())
562576
default:
563577
panic("unknown field " + strconv.Quote(field.Name))
564578
}

internal/gqlgen/gqlgen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestExec(t *testing.T) {
101101
ParsedSchema: AST,
102102
}
103103

104-
resp := es.Exec(oc)
104+
resp := es.Exec(oc, gqlgen.WithReservedTypes(true))
105105

106106
return resp.Bytes(), nil
107107
})

internal/gqlgen/gqlgen_test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ tests:
240240
}
241241
query: |
242242
{
243-
__schema { description }
243+
__schema {
244+
description
245+
}
244246
}
245247
json: |
246248
{"__schema":{"description":"This is a simple schema"}}

internal/wrapper/schema.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ import (
77
"github.com/Code-Hex/gqlparser/v2/ast"
88
)
99

10+
type WrapSchemaOption func(s *Schema)
11+
12+
func WithReservedTypes(want bool) WrapSchemaOption {
13+
return func(s *Schema) {
14+
s.wantReservedTypes = want
15+
}
16+
}
17+
1018
type Schema struct {
11-
schema *ast.Schema
19+
schema *ast.Schema
20+
wantReservedTypes bool
1221
}
1322

1423
func (s *Schema) Types() []Type {
1524
types := make([]Type, 0, len(s.schema.Types))
1625
for _, typ := range s.schema.Types {
26+
if !s.wantReservedTypes && strings.HasPrefix(typ.Name, "__") {
27+
continue
28+
}
1729
types = append(types, *WrapTypeFromDef(s.schema, typ))
1830
}
1931
sort.Slice(types, func(i, j int) bool {

internal/wrapper/wrapper.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ type (
3232
}
3333
)
3434

35-
func WrapSchema(schema *ast.Schema) *Schema {
36-
return &Schema{schema: schema}
35+
func WrapSchema(schema *ast.Schema, opts ...WrapSchemaOption) *Schema {
36+
s := &Schema{schema: schema}
37+
for _, opt := range opts {
38+
opt(s)
39+
}
40+
return s
3741
}
3842

3943
func (f *EnumValue) IsDeprecated() bool {

0 commit comments

Comments
 (0)