Skip to content

Commit

Permalink
Move GraphQL types to package ast (graph-gophers#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelnikolov authored and KNiepok committed Feb 28, 2023
1 parent 945d196 commit 7ddb8a6
Show file tree
Hide file tree
Showing 41 changed files with 670 additions and 518 deletions.
2 changes: 1 addition & 1 deletion types/argument.go → ast/argument.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

// Argument is a representation of the GraphQL Argument.
//
Expand Down
2 changes: 1 addition & 1 deletion types/directive.go → ast/directive.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
9 changes: 9 additions & 0 deletions ast/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Package ast represents all types from the [GraphQL specification] in code.
The names of the Go types, whenever possible, match 1:1 with the names from
the specification.
[GraphQL specification]: https://spec.graphql.org
*/
package ast
2 changes: 1 addition & 1 deletion types/enum.go → ast/enum.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/extension.go → ast/extension.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/field.go → ast/field.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/fragment.go → ast/fragment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/input.go → ast/input.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/interface.go → ast/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/object.go → ast/object.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/query.go → ast/query.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/scalar.go → ast/scalar.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
41 changes: 41 additions & 0 deletions ast/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ast

// Schema represents a GraphQL service's collective type system capabilities.
// A schema is defined in terms of the types and directives it supports as well as the root
// operation types for each kind of operation: `query`, `mutation`, and `subscription`.
//
// For a more formal definition, read the relevant section in the specification:
//
// http://spec.graphql.org/draft/#sec-Schema
type Schema struct {
// RootOperationTypes determines the place in the type system where `query`, `mutation`, and
// `subscription` operations begin.
//
// http://spec.graphql.org/draft/#sec-Root-Operation-Types
//
RootOperationTypes map[string]NamedType

// Types are the fundamental unit of any GraphQL schema.
// There are six kinds of named type definitions in GraphQL, and two wrapping types.
//
// http://spec.graphql.org/draft/#sec-Types
Types map[string]NamedType

// Directives are used to annotate various parts of a GraphQL document as an indicator that they
// should be evaluated differently by a validator, executor, or client tool such as a code
// generator.
//
// http://spec.graphql.org/#sec-Type-System.Directives
Directives map[string]*DirectiveDefinition

EntryPointNames map[string]string
Objects []*ObjectTypeDefinition
Unions []*Union
Enums []*EnumTypeDefinition
Extensions []*Extension
SchemaString string
}

func (s *Schema) Resolve(name string) Type {
return s.Types[name]
}
2 changes: 1 addition & 1 deletion types/types.go → ast/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import (
"github.com/graph-gophers/graphql-go/errors"
Expand Down
2 changes: 1 addition & 1 deletion types/union.go → ast/union.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
2 changes: 1 addition & 1 deletion types/value.go → ast/value.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import (
"strconv"
Expand Down
2 changes: 1 addition & 1 deletion types/variable.go → ast/variable.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ast

import "github.com/graph-gophers/graphql-go/errors"

Expand Down
10 changes: 5 additions & 5 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ func ExampleRestrictIntrospection() {
// }
}

func ExampleSchema_ASTSchema() {
func ExampleSchema_AST() {
schema := graphql.MustParseSchema(starwars.Schema, nil)
ast := schema.ASTSchema()
ast := schema.AST()

for _, e := range ast.Enums {
fmt.Printf("Enum %q has the following options:\n", e.Name)
Expand All @@ -250,7 +250,7 @@ func ExampleSchema_ASTSchema() {
// - FOOT
}

func ExampleSchema_ASTSchema_generateEnum() {
func ExampleSchema_AST_generateEnum() {
s := `
schema {
query: Query
Expand Down Expand Up @@ -309,7 +309,7 @@ func (s Season) String() string {
graphql.UseStringDescriptions(),
}
schema := graphql.MustParseSchema(s, nil, opts...)
ast := schema.ASTSchema()
ast := schema.AST()
seasons := ast.Enums[0]

err = tpl.Execute(os.Stdout, seasons)
Expand Down Expand Up @@ -376,7 +376,7 @@ func ExampleUseStringDescriptions() {
graphql.UseStringDescriptions(),
}
schema := graphql.MustParseSchema(s, nil, opts...)
ast := schema.ASTSchema()
ast := schema.AST()

post := ast.Objects[1]
fmt.Printf("Field descriptions of the %q type:\n", post.TypeName())
Expand Down
21 changes: 14 additions & 7 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"time"

"github.com/graph-gophers/graphql-go/ast"
"github.com/graph-gophers/graphql-go/directives"
"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/internal/common"
Expand All @@ -22,12 +23,11 @@ import (
noopratelimit "github.com/graph-gophers/graphql-go/ratelimit/noop"
"github.com/graph-gophers/graphql-go/trace/noop"
"github.com/graph-gophers/graphql-go/trace/tracer"
"github.com/graph-gophers/graphql-go/types"
)

// ParseSchema parses a GraphQL schema and attaches the given root resolver. It returns an error if
// the Go type signature of the resolvers does not match the schema. If nil is passed as the
// resolver, then the schema can not be executed, but it may be inspected (e.g. with [Schema.ToJSON] or [Schema.ASTSchema]).
// resolver, then the schema can not be executed, but it may be inspected (e.g. with [Schema.ToJSON] or [Schema.AST]).
func ParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) (*Schema, error) {
s := &Schema{
schema: schema.New(),
Expand Down Expand Up @@ -77,7 +77,7 @@ func MustParseSchema(schemaString string, resolver interface{}, opts ...SchemaOp

// Schema represents a GraphQL schema with an optional resolver.
type Schema struct {
schema *types.Schema
schema *ast.Schema
res *resolvable.Schema

allowIntrospection func(ctx context.Context) bool
Expand All @@ -97,9 +97,16 @@ type Schema struct {
middlewares []Middleware
}

// ASTSchema returns the abstract syntax tree of the GraphQL schema definition.
// AST returns the abstract syntax tree of the GraphQL schema definition.
// It in turn can be used by other tools such as validators or generators.
func (s *Schema) ASTSchema() *types.Schema {
func (s *Schema) AST() *ast.Schema {
return s.schema
}

// ASTSchema returns the abstract syntax tree of the GraphQL schema definition.
//
// Deprecated: use [Schema.AST] instead.
func (s *Schema) ASTSchema() *ast.Schema {
return s.schema
}

Expand Down Expand Up @@ -395,7 +402,7 @@ func (t *validationBridgingTracer) TraceValidation(context.Context) func([]*erro
return t.tracer.TraceValidation()
}

func validateRootOp(s *types.Schema, name string, mandatory bool) error {
func validateRootOp(s *ast.Schema, name string, mandatory bool) error {
t, ok := s.RootOperationTypes[name]
if !ok {
if mandatory {
Expand All @@ -409,7 +416,7 @@ func validateRootOp(s *types.Schema, name string, mandatory bool) error {
return nil
}

func getOperation(document *types.ExecutableDefinition, operationName string) (*types.OperationDefinition, error) {
func getOperation(document *ast.ExecutableDefinition, operationName string) (*ast.OperationDefinition, error) {
if len(document.Operations) == 0 {
return nil, fmt.Errorf("no operations in query document")
}
Expand Down
8 changes: 4 additions & 4 deletions internal/common/directive.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package common

import "github.com/graph-gophers/graphql-go/types"
import "github.com/graph-gophers/graphql-go/ast"

func ParseDirectives(l *Lexer) types.DirectiveList {
var directives types.DirectiveList
func ParseDirectives(l *Lexer) ast.DirectiveList {
var directives ast.DirectiveList
for l.Peek() == '@' {
l.ConsumeToken('@')
d := &types.Directive{}
d := &ast.Directive{}
d.Name = l.ConsumeIdentWithLoc()
d.Name.Loc.Column--
if l.Peek() == '(' {
Expand Down
10 changes: 5 additions & 5 deletions internal/common/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"text/scanner"

"github.com/graph-gophers/graphql-go/ast"
"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/types"
)

type syntaxError string
Expand Down Expand Up @@ -119,11 +119,11 @@ func (l *Lexer) ConsumeIdent() string {
return name
}

func (l *Lexer) ConsumeIdentWithLoc() types.Ident {
func (l *Lexer) ConsumeIdentWithLoc() ast.Ident {
loc := l.Location()
name := l.sc.TokenText()
l.ConsumeToken(scanner.Ident)
return types.Ident{Name: name, Loc: loc}
return ast.Ident{Name: name, Loc: loc}
}

func (l *Lexer) ConsumeKeyword(keyword string) {
Expand All @@ -133,8 +133,8 @@ func (l *Lexer) ConsumeKeyword(keyword string) {
l.ConsumeWhitespace()
}

func (l *Lexer) ConsumeLiteral() *types.PrimitiveValue {
lit := &types.PrimitiveValue{Type: l.next, Text: l.sc.TokenText()}
func (l *Lexer) ConsumeLiteral() *ast.PrimitiveValue {
lit := &ast.PrimitiveValue{Type: l.next, Text: l.sc.TokenText()}
l.ConsumeWhitespace()
return lit
}
Expand Down
18 changes: 9 additions & 9 deletions internal/common/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package common
import (
"text/scanner"

"github.com/graph-gophers/graphql-go/types"
"github.com/graph-gophers/graphql-go/ast"
)

func ParseLiteral(l *Lexer, constOnly bool) types.Value {
func ParseLiteral(l *Lexer, constOnly bool) ast.Value {
loc := l.Location()
switch l.Peek() {
case '$':
Expand All @@ -15,12 +15,12 @@ func ParseLiteral(l *Lexer, constOnly bool) types.Value {
panic("unreachable")
}
l.ConsumeToken('$')
return &types.Variable{Name: l.ConsumeIdent(), Loc: loc}
return &ast.Variable{Name: l.ConsumeIdent(), Loc: loc}

case scanner.Int, scanner.Float, scanner.String, scanner.Ident:
lit := l.ConsumeLiteral()
if lit.Type == scanner.Ident && lit.Text == "null" {
return &types.NullValue{Loc: loc}
return &ast.NullValue{Loc: loc}
}
lit.Loc = loc
return lit
Expand All @@ -32,24 +32,24 @@ func ParseLiteral(l *Lexer, constOnly bool) types.Value {
return lit
case '[':
l.ConsumeToken('[')
var list []types.Value
var list []ast.Value
for l.Peek() != ']' {
list = append(list, ParseLiteral(l, constOnly))
}
l.ConsumeToken(']')
return &types.ListValue{Values: list, Loc: loc}
return &ast.ListValue{Values: list, Loc: loc}

case '{':
l.ConsumeToken('{')
var fields []*types.ObjectField
var fields []*ast.ObjectField
for l.Peek() != '}' {
name := l.ConsumeIdentWithLoc()
l.ConsumeToken(':')
value := ParseLiteral(l, constOnly)
fields = append(fields, &types.ObjectField{Name: name, Value: value})
fields = append(fields, &ast.ObjectField{Name: name, Value: value})
}
l.ConsumeToken('}')
return &types.ObjectValue{Fields: fields, Loc: loc}
return &ast.ObjectValue{Fields: fields, Loc: loc}

default:
l.SyntaxError("invalid value")
Expand Down
Loading

0 comments on commit 7ddb8a6

Please sign in to comment.