forked from TykTechnologies/graphql-go-tools
/
datasource_schema.go
66 lines (55 loc) · 1.62 KB
/
datasource_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
package datasource
import (
"context"
"encoding/json"
"io"
"github.com/pvormste/graphql-go-tools/pkg/introspection"
"github.com/pvormste/graphql-go-tools/pkg/operationreport"
)
type SchemaDataSourcePlannerConfig struct {
}
type SchemaDataSourcePlannerFactoryFactory struct {
}
func (s SchemaDataSourcePlannerFactoryFactory) Initialize(base BasePlanner, configReader io.Reader) (PlannerFactory, error) {
factory := &SchemaDataSourcePlannerFactory{
base: base,
}
err := json.NewDecoder(configReader).Decode(&factory.config)
if err != nil {
return factory, err
}
gen := introspection.NewGenerator()
var data introspection.Data
var report operationreport.Report
gen.Generate(base.Definition, &report, &data)
factory.schemaBytes, err = json.Marshal(data)
return factory, err
}
type SchemaDataSourcePlannerFactory struct {
base BasePlanner
config SchemaDataSourcePlannerConfig
schemaBytes []byte
}
func (s SchemaDataSourcePlannerFactory) DataSourcePlanner() Planner {
return SimpleDataSourcePlanner(&SchemaDataSourcePlanner{
BasePlanner: s.base,
dataSourceConfig: s.config,
schemaBytes: s.schemaBytes,
})
}
type SchemaDataSourcePlanner struct {
BasePlanner
dataSourceConfig SchemaDataSourcePlannerConfig
schemaBytes []byte
}
func (s *SchemaDataSourcePlanner) Plan(args []Argument) (DataSource, []Argument) {
return &SchemaDataSource{
SchemaBytes: s.schemaBytes,
}, append(s.Args, args...)
}
type SchemaDataSource struct {
SchemaBytes []byte
}
func (s *SchemaDataSource) Resolve(ctx context.Context, args ResolverArgs, out io.Writer) (n int, err error) {
return out.Write(s.SchemaBytes)
}