Skip to content

Commit

Permalink
Separate parsing out of validation
Browse files Browse the repository at this point in the history
This separates out the parsing step with ValidateSchemaDocument and
offers a public method to validate a SchemaDocument into a valid
Schema.
  • Loading branch information
Mathew Byrne committed Sep 18, 2018
1 parent 14e83ae commit 1fc2a70
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 12 additions & 0 deletions parser/schema.go
Expand Up @@ -13,6 +13,18 @@ func ParseSchema(source *Source) (*SchemaDocument, *gqlerror.Error) {
return p.parseSchemaDocument(), p.err
}

func ParseSchemas(inputs ...*Source) (*SchemaDocument, *gqlerror.Error) {
ast := &SchemaDocument{}
for _, input := range inputs {
inputAst, err := ParseSchema(input)
if err != nil {
return nil, err
}
ast.Merge(inputAst)
}
return ast, nil
}

func (p *parser) parseSchemaDocument() *SchemaDocument {
var doc SchemaDocument
doc.Position = p.peekPos()
Expand Down
14 changes: 6 additions & 8 deletions validator/schema.go
Expand Up @@ -11,16 +11,14 @@ import (
)

func LoadSchema(inputs ...*Source) (*Schema, *gqlerror.Error) {
ast := &SchemaDocument{}
for _, input := range inputs {
inputAst, err := parser.ParseSchema(input)
if err != nil {
return nil, err
}

ast.Merge(inputAst)
ast, err := parser.ParseSchemas(inputs...)
if err != nil {
return nil, err
}
return ValidateSchemaDocument(ast)
}

func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, *gqlerror.Error) {
schema := Schema{
Types: map[string]*Definition{},
Directives: map[string]*DirectiveDefinition{},
Expand Down

0 comments on commit 1fc2a70

Please sign in to comment.