Skip to content

[WIP] Working draft version of validator and a couple of validation rules #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9ce95cf
Working draft version of `validator` and a couple of `validation rules`
sogko Nov 9, 2015
ba98d9b
Fix travis error
sogko Nov 9, 2015
b4a10de
Completed port of tests for `ArgumentsOfCorrectTypeRule`
sogko Nov 9, 2015
e82559d
Implemented `DefaultValuesOfCorrectTypeRule` and tested
sogko Nov 10, 2015
303b6e1
Implemented `FieldsOnCorrectTypeRule ` and tested
sogko Nov 10, 2015
7ce1c94
Implemented `FragmentsOnCompositeTypesRule` & `KnownArgumentNamesRul…
sogko Nov 10, 2015
86bf407
Implemented `KnownDirectivesRule ` & `KnownFragmentNamesRule ` and te…
sogko Nov 10, 2015
940ebe6
Merge pull request #17 from graphql-go/master
sogko Nov 10, 2015
8757891
Merge branch 'master' into sogko/validator
sogko Nov 10, 2015
9e62e2d
Implemented `LoneAnonymousOperationRule` & `NoFragmentCyclesRule` and…
sogko Nov 12, 2015
4c94ee3
Implemented `NoUndefinedVariablesRule`
sogko Nov 12, 2015
d2f6f2c
Implemented `NoUnusedFragmentsRule` and tested
sogko Nov 15, 2015
d0063b3
Implemented `OverlappingFieldsCanBeMergedRule` and tested
sogko Nov 17, 2015
d0824e0
Implemented `NoUnusedVariablesRule` and tested
sogko Nov 17, 2015
385a163
Implemented `PossibleFragmentSpreadsRule` and tested
sogko Nov 17, 2015
923f4db
Implemented `ProvidedNonNullArgumentsRule` and tested
sogko Nov 17, 2015
c35a864
Implemented `ScalarLeafsRule` and tested
sogko Nov 18, 2015
4ad62fc
Implemented `UniqueArgumentNamesRule` and tested
sogko Nov 18, 2015
b32b47d
Implemented `UniqueFragmentNamesRule` and tested
sogko Nov 18, 2015
2e55f1e
Implemented `UniqueOperationNamesRule` and tested
sogko Nov 18, 2015
39e9c6a
Implemented `VariablesAreInputTypesRule` & `VariablesInAllowedPositio…
sogko Nov 18, 2015
ef782a5
Merge pull request #18 from graphql-go/master
sogko Nov 18, 2015
c7d03dd
Merge branch 'sogko/master' into sogko/validator
sogko Nov 18, 2015
f57876e
Moved tests in `rules` folder out to root
sogko Nov 18, 2015
7b9d6e3
Minor `gofmt`
sogko Nov 18, 2015
49b0dab
Added missing rule tests
sogko Nov 18, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (

func main() {
// Schema
fields := graphql.FieldConfigMap{
"hello": &graphql.FieldConfig{
fields := graphql.Fields{
"hello": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.GQLFRParams) interface{} {
return "world"
Expand Down
88 changes: 9 additions & 79 deletions abstract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
)

type testDog struct {
Name string
Woofs bool
Name string `json:"name"`
Woofs bool `json:"woofs"`
}

type testCat struct {
Name string
Meows bool
Name string `json:"name"`
Meows bool `json:"meows"`
}

type testHuman struct {
Name string
Name string `json:"name"`
}

func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
Expand Down Expand Up @@ -168,21 +168,9 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Name
}
return nil
},
},
"woofs": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Woofs
}
return nil
},
},
},
})
Expand All @@ -195,21 +183,9 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Name
}
return nil
},
},
"meows": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Meows
}
return nil
},
},
},
})
Expand All @@ -219,15 +195,6 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {
Types: []*graphql.Object{
dogType, catType,
},
ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
if _, ok := value.(*testCat); ok {
return catType
}
if _, ok := value.(*testDog); ok {
return dogType
}
return nil
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Expand All @@ -251,11 +218,12 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {

query := `{
pets {
name
... on Dog {
name
woofs
}
... on Cat {
name
meows
}
}
Expand All @@ -281,7 +249,6 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {
Schema: schema,
RequestString: query,
})

if len(result.Errors) != 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
Expand Down Expand Up @@ -464,66 +431,28 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
if human, ok := p.Source.(*testHuman); ok {
return human.Name
}
return nil
},
},
},
})
dogType := graphql.NewObject(graphql.ObjectConfig{
Name: "Dog",
IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool {
_, ok := value.(*testDog)
return ok
},
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Name
}
return nil
},
},
"woofs": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Woofs
}
return nil
},
},
},
})
catType := graphql.NewObject(graphql.ObjectConfig{
Name: "Cat",
IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool {
_, ok := value.(*testCat)
return ok
},
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Name
}
return nil
},
},
"meows": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Meows
}
return nil
},
},
},
})
Expand Down Expand Up @@ -568,11 +497,12 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {

query := `{
pets {
name
... on Dog {
name
woofs
}
... on Cat {
name
meows
}
}
Expand Down
62 changes: 52 additions & 10 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,45 @@ var _ Input = (*List)(nil)
var _ Input = (*NonNull)(nil)

func IsInputType(ttype Type) bool {
Named := GetNamed(ttype)
if _, ok := Named.(*Scalar); ok {
named := GetNamed(ttype)
if _, ok := named.(*Scalar); ok {
return true
}
if _, ok := Named.(*Enum); ok {
if _, ok := named.(*Enum); ok {
return true
}
if _, ok := Named.(*InputObject); ok {
if _, ok := named.(*InputObject); ok {
return true
}
return false
}

func IsOutputType(ttype Type) bool {
Named := GetNamed(ttype)
if _, ok := Named.(*Scalar); ok {
name := GetNamed(ttype)
if _, ok := name.(*Scalar); ok {
return true
}
if _, ok := Named.(*Object); ok {
if _, ok := name.(*Object); ok {
return true
}
if _, ok := Named.(*Interface); ok {
if _, ok := name.(*Interface); ok {
return true
}
if _, ok := Named.(*Union); ok {
if _, ok := name.(*Union); ok {
return true
}
if _, ok := Named.(*Enum); ok {
if _, ok := name.(*Enum); ok {
return true
}
return false
}

func IsLeafType(ttype Type) bool {
named := GetNamed(ttype)
if _, ok := named.(*Scalar); ok {
return true
}
if _, ok := named.(*Enum); ok {
return true
}
return false
Expand Down Expand Up @@ -100,6 +111,19 @@ var _ Composite = (*Object)(nil)
var _ Composite = (*Interface)(nil)
var _ Composite = (*Union)(nil)

func IsCompositeType(ttype interface{}) bool {
if _, ok := ttype.(*Object); ok {
return true
}
if _, ok := ttype.(*Interface); ok {
return true
}
if _, ok := ttype.(*Union); ok {
return true
}
return false
}

// These types may describe the parent context of a selection set.
type Abstract interface {
ObjectType(value interface{}, info ResolveInfo) *Object
Expand All @@ -110,6 +134,24 @@ type Abstract interface {
var _ Abstract = (*Interface)(nil)
var _ Abstract = (*Union)(nil)

type Nullable interface {
}

var _ Nullable = (*Scalar)(nil)
var _ Nullable = (*Object)(nil)
var _ Nullable = (*Interface)(nil)
var _ Nullable = (*Union)(nil)
var _ Nullable = (*Enum)(nil)
var _ Nullable = (*InputObject)(nil)
var _ Nullable = (*List)(nil)

func GetNullable(ttype Type) Nullable {
if ttype, ok := ttype.(*NonNull); ok {
return ttype.OfType
}
return ttype
}

// These named types do not include modifiers like List or NonNull.
type Named interface {
String() string
Expand Down
4 changes: 2 additions & 2 deletions examples/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var data map[string]user
/*
Create User object type with fields "id" and "name" by using GraphQLObjectTypeConfig:
- Name: name of object type
- Fields: a map of fields by using GraphQLFieldConfigMap
- Fields: a map of fields by using GraphQLFields
Setup type of field use GraphQLFieldConfig
*/
var userType = graphql.NewObject(
Expand All @@ -39,7 +39,7 @@ var userType = graphql.NewObject(
/*
Create Query object type with fields "user" has type [userType] by using GraphQLObjectTypeConfig:
- Name: name of object type
- Fields: a map of fields by using GraphQLFieldConfigMap
- Fields: a map of fields by using GraphQLFields
Setup type of field use GraphQLFieldConfig to define:
- Type: type of field
- Args: arguments to query with current field
Expand Down
2 changes: 1 addition & 1 deletion graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Do(p Params) *Result {
Errors: gqlerrors.FormatErrors(err),
}
}
validationResult := ValidateDocument(p.Schema, AST)
validationResult := ValidateDocument(&p.Schema, AST, nil)

if !validationResult.IsValid {
return &Result{
Expand Down
4 changes: 2 additions & 2 deletions language/kinds/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const (
Directive = "Directive"
VariableDefinition = "VariableDefinition"
Variable = "Variable"
Named = "Named"
List = "List"
Named = "Named" // previously NamedType
List = "List" // previously ListType
NonNull = "NonNull"
InlineFragment = "InlineFragment"
FragmentSpread = "FragmentSpread"
Expand Down
Loading