| giolekva | c76b21b | 2020-04-18 19:28:43 +0400 | [diff] [blame^] | 1 | package schema |
| 2 | |
| 3 | import ( |
| 4 | "github.com/vektah/gqlparser/ast" |
| 5 | "github.com/vektah/gqlparser/parser" |
| 6 | ) |
| 7 | |
| 8 | type SchemaStore interface { |
| 9 | Schema() *ast.SchemaDocument |
| 10 | SetSchema(gqlSchema string) error |
| 11 | AddSchema(gqlSchema string) error |
| 12 | } |
| 13 | |
| 14 | type InMemorySchemaStore struct { |
| 15 | gqlSchema string |
| 16 | schema *ast.SchemaDocument |
| 17 | } |
| 18 | |
| 19 | func NewInMemorySchemaStore() SchemaStore { |
| 20 | return &InMemorySchemaStore{gqlSchema: ""} |
| 21 | } |
| 22 | |
| 23 | func (s *InMemorySchemaStore) Schema() *ast.SchemaDocument { |
| 24 | return s.schema |
| 25 | } |
| 26 | |
| 27 | func (s *InMemorySchemaStore) AddSchema(gqlSchema string) error { |
| 28 | return s.SetSchema(s.gqlSchema + gqlSchema) |
| 29 | } |
| 30 | |
| 31 | func (s *InMemorySchemaStore) SetSchema(gqlSchema string) error { |
| 32 | schema, err := parser.ParseSchema(&ast.Source{Input: gqlSchema}) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | s.schema = schema |
| 37 | return nil |
| 38 | } |