Skip to content

Commit

Permalink
return error rather than panic when provided schema refers to missing…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
benweint committed May 30, 2024
1 parent 322537a commit 4c3ff7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 8 additions & 3 deletions pkg/model/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ func maybeTypeName(in *ast.Definition) string {
return in.Name
}

func resolveTypeKinds(typesByName map[string]*Definition, t *Type) {
func resolveTypeKinds(typesByName map[string]*Definition, t *Type) error {
if t.OfType != nil {
resolveTypeKinds(typesByName, t.OfType)
return resolveTypeKinds(typesByName, t.OfType)
} else {
t.Kind = TypeKind(typesByName[t.Name].Kind)
referencedType, ok := typesByName[t.Name]
if !ok {
return fmt.Errorf("could not resolve type named '%s'", t.Name)
}
t.Kind = TypeKind(referencedType.Kind)
}
return nil
}
19 changes: 14 additions & 5 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *Schema) ResolveNames(names []string) ([]*NameReference, error) {
var roots []*NameReference
var badNames []string
for _, rootName := range names {
root := s.ResolveName(rootName)
root := s.resolveName(rootName)
if root == nil {
badNames = append(badNames, rootName)
} else {
Expand All @@ -45,7 +45,7 @@ func (s *Schema) ResolveNames(names []string) ([]*NameReference, error) {
return roots, nil
}

func (s *Schema) ResolveName(name string) *NameReference {
func (s *Schema) resolveName(name string) *NameReference {
parts := strings.SplitN(name, ".", 2)
typePart := parts[0]
for _, def := range s.Types {
Expand Down Expand Up @@ -73,6 +73,9 @@ func (s *Schema) ResolveName(name string) *NameReference {
return nil
}

// MakeSchema constructs and returns a Schema from the given ast.Schema.
// The provided ast.Schema must be 'complete' in the sense that it must contain type definitions
// for all types used in the schema, including built-in types like String, Int, etc.
func MakeSchema(in *ast.Schema) (*Schema, error) {
var types DefinitionList
typesByName := map[string]*Definition{}
Expand Down Expand Up @@ -112,16 +115,22 @@ func MakeSchema(in *ast.Schema) (*Schema, error) {
// Resolve type kinds for named types by looking them up in typesByName
for _, t := range types {
for _, f := range t.Fields {
resolveTypeKinds(typesByName, f.Type)
if err := resolveTypeKinds(typesByName, f.Type); err != nil {
return nil, err
}
for _, a := range f.Arguments {
resolveTypeKinds(typesByName, a.Type)
if err := resolveTypeKinds(typesByName, a.Type); err != nil {
return nil, err
}
}
}
}

for _, d := range directives {
for _, arg := range d.Arguments {
resolveTypeKinds(typesByName, arg.Type)
if err := resolveTypeKinds(typesByName, arg.Type); err != nil {
return nil, err
}
}
}

Expand Down

0 comments on commit 4c3ff7c

Please sign in to comment.