-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
68 lines (54 loc) · 1.56 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package groot
import (
"reflect"
"github.com/graphql-go/graphql"
"github.com/shreyas44/groot/parser"
)
type SchemaConfig struct {
Query *parser.Object
Mutation *parser.Object
Subscription *parser.Object
Types []parser.Type
Extensions []graphql.Extension
}
type SchemaBuilder struct {
graphqlTypes map[parser.Type]graphql.Type
reflectGrootMap map[reflect.Type]graphql.Type
}
func (builder *SchemaBuilder) addType(t parser.Type, graphqlType graphql.Type) {
builder.graphqlTypes[t] = graphqlType
builder.reflectGrootMap[t.ReflectType()] = graphqlType
}
func (builder *SchemaBuilder) getType(t parser.Type) (graphql.Type, bool) {
graphqlType, ok := builder.graphqlTypes[t]
if ok {
return graphqlType, true
}
return nil, false
}
func NewSchemaBuilder() *SchemaBuilder {
return &SchemaBuilder{
graphqlTypes: map[parser.Type]graphql.Type{},
reflectGrootMap: map[reflect.Type]graphql.Type{},
}
}
func NewSchema(config SchemaConfig) (graphql.Schema, error) {
builder := NewSchemaBuilder()
schemaConfig := graphql.SchemaConfig{
Extensions: config.Extensions,
Types: []graphql.Type{},
}
if config.Query != nil {
schemaConfig.Query = NewObject(config.Query, builder)
}
if config.Mutation != nil {
schemaConfig.Mutation = NewObject(config.Mutation, builder)
}
if config.Subscription != nil {
schemaConfig.Subscription = NewObject(config.Subscription, builder)
}
for _, t := range config.Types {
schemaConfig.Types = append(schemaConfig.Types, getOrCreateType(t, builder))
}
return graphql.NewSchema(schemaConfig)
}