Skip to content

Commit

Permalink
Merge pull request #117 from sogko/sogko/0.4.18
Browse files Browse the repository at this point in the history
Port changes from `graphql-js` v0.4.18 (Oct 2015 spec)
  • Loading branch information
sogko committed May 30, 2016
2 parents 909ca9f + c4394ae commit a5cf5f2
Show file tree
Hide file tree
Showing 62 changed files with 5,126 additions and 1,710 deletions.
392 changes: 202 additions & 190 deletions definition.go

Large diffs are not rendered by default.

62 changes: 60 additions & 2 deletions definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ var blogMutation = graphql.NewObject(graphql.ObjectConfig{
},
})

var blogSubscription = graphql.NewObject(graphql.ObjectConfig{
Name: "Subscription",
Fields: graphql.Fields{
"articleSubscribe": &graphql.Field{
Type: blogArticle,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
},
},
})

var objectType = graphql.NewObject(graphql.ObjectConfig{
Name: "Object",
IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool {
Expand Down Expand Up @@ -204,6 +218,7 @@ func TestTypeSystem_DefinitionExample_DefinesAQueryOnlySchema(t *testing.T) {
t.Fatalf("feedField.Name expected to equal `feed`, got: %v", feedField.Name)
}
}

func TestTypeSystem_DefinitionExample_DefinesAMutationScheme(t *testing.T) {
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
Expand Down Expand Up @@ -233,6 +248,35 @@ func TestTypeSystem_DefinitionExample_DefinesAMutationScheme(t *testing.T) {
}
}

func TestTypeSystem_DefinitionExample_DefinesASubscriptionScheme(t *testing.T) {
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
Subscription: blogSubscription,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}

if blogSchema.SubscriptionType() != blogSubscription {
t.Fatalf("expected blogSchema.SubscriptionType() == blogSubscription")
}

subMutation, _ := blogSubscription.Fields()["articleSubscribe"]
if subMutation == nil {
t.Fatalf("subMutation is nil")
}
subMutationType := subMutation.Type
if subMutationType != blogArticle {
t.Fatalf("subMutationType expected to equal blogArticle, got: %v", subMutationType)
}
if subMutationType.Name() != "Article" {
t.Fatalf("subMutationType.Name expected to equal `Article`, got: %v", subMutationType.Name())
}
if subMutation.Name != "articleSubscribe" {
t.Fatalf("subMutation.Name expected to equal `articleSubscribe`, got: %v", subMutation.Name)
}
}

func TestTypeSystem_DefinitionExample_IncludesNestedInputObjectsInTheMap(t *testing.T) {
nestedInputObject := graphql.NewInputObject(graphql.InputObjectConfig{
Name: "NestedInputObject",
Expand Down Expand Up @@ -263,9 +307,23 @@ func TestTypeSystem_DefinitionExample_IncludesNestedInputObjectsInTheMap(t *test
},
},
})
someSubscription := graphql.NewObject(graphql.ObjectConfig{
Name: "SomeSubscription",
Fields: graphql.Fields{
"subscribeToSomething": &graphql.Field{
Type: blogArticle,
Args: graphql.FieldConfigArgument{
"input": &graphql.ArgumentConfig{
Type: someInputObject,
},
},
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
Mutation: someMutation,
Query: blogQuery,
Mutation: someMutation,
Subscription: someSubscription,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
Expand Down
18 changes: 6 additions & 12 deletions directives.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package graphql

// Directive structs are used by the GraphQL runtime as a way of modifying execution
// behavior. Type system creators will usually not create these directly.
type Directive struct {
Name string `json:"name"`
Description string `json:"description"`
Expand All @@ -9,10 +11,6 @@ type Directive struct {
OnField bool `json:"onField"`
}

/**
* Directives are used by the GraphQL runtime as a way of modifying execution
* behavior. Type system creators will usually not create these directly.
*/
func NewDirective(config *Directive) *Directive {
if config == nil {
config = &Directive{}
Expand All @@ -27,10 +25,8 @@ func NewDirective(config *Directive) *Directive {
}
}

/**
* Used to conditionally include fields or fragments
*/
var IncludeDirective *Directive = NewDirective(&Directive{
// IncludeDirective is used to conditionally include fields or fragments
var IncludeDirective = NewDirective(&Directive{
Name: "include",
Description: "Directs the executor to include this field or fragment only when " +
"the `if` argument is true.",
Expand All @@ -46,10 +42,8 @@ var IncludeDirective *Directive = NewDirective(&Directive{
OnField: true,
})

/**
* Used to conditionally skip (exclude) fields or fragments
*/
var SkipDirective *Directive = NewDirective(&Directive{
// SkipDirective Used to conditionally skip (exclude) fields or fragments
var SkipDirective = NewDirective(&Directive{
Name: "skip",
Description: "Directs the executor to skip this field or fragment when the `if` " +
"argument is true.",
Expand Down
94 changes: 94 additions & 0 deletions directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,100 @@ func TestDirectivesWorksOnInlineFragmentUnlessTrueIncludesInlineFragment(t *test
}
}

func TestDirectivesWorksOnAnonymousInlineFragmentIfFalseOmitsAnonymousInlineFragment(t *testing.T) {
query := `
query Q {
a
... @include(if: false) {
b
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"a": "a",
},
}
result := executeDirectivesTestQuery(t, query)
if len(result.Errors) != 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestDirectivesWorksOnAnonymousInlineFragmentIfTrueIncludesAnonymousInlineFragment(t *testing.T) {
query := `
query Q {
a
... @include(if: true) {
b
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"a": "a",
"b": "b",
},
}
result := executeDirectivesTestQuery(t, query)
if len(result.Errors) != 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestDirectivesWorksOnAnonymousInlineFragmentUnlessFalseIncludesAnonymousInlineFragment(t *testing.T) {
query := `
query Q {
a
... @skip(if: false) {
b
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"a": "a",
"b": "b",
},
}
result := executeDirectivesTestQuery(t, query)
if len(result.Errors) != 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestDirectivesWorksOnAnonymousInlineFragmentUnlessTrueIncludesAnonymousInlineFragment(t *testing.T) {
query := `
query Q {
a
... @skip(if: true) {
b
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"a": "a",
},
}
result := executeDirectivesTestQuery(t, query)
if len(result.Errors) != 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestDirectivesWorksOnFragmentIfFalseOmitsFragment(t *testing.T) {
query := `
query Q {
Expand Down
63 changes: 57 additions & 6 deletions enum_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/location"
"github.com/graphql-go/graphql/testutil"
)

Expand Down Expand Up @@ -93,9 +94,31 @@ var enumTypeTestMutationType = graphql.NewObject(graphql.ObjectConfig{
},
},
})

var enumTypeTestSubscriptionType = graphql.NewObject(graphql.ObjectConfig{
Name: "Subscription",
Fields: graphql.Fields{
"subscribeToEnum": &graphql.Field{
Type: enumTypeTestColorType,
Args: graphql.FieldConfigArgument{
"color": &graphql.ArgumentConfig{
Type: enumTypeTestColorType,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if color, ok := p.Args["color"]; ok {
return color, nil
}
return nil, nil
},
},
},
})

var enumTypeTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: enumTypeTestQueryType,
Mutation: enumTypeTestMutationType,
Query: enumTypeTestQueryType,
Mutation: enumTypeTestMutationType,
Subscription: enumTypeTestSubscriptionType,
})

func executeEnumTypeTest(t *testing.T, query string) *graphql.Result {
Expand Down Expand Up @@ -156,7 +179,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptStringLiterals(t *testing.T) {
Data: nil,
Errors: []gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: `Argument "fromEnum" expected type "Color" but got: "GREEN".`,
Message: "Argument \"fromEnum\" has invalid value \"GREEN\".\nExpected type \"Color\", found \"GREEN\".",
Locations: []location.SourceLocation{
{Line: 1, Column: 23},
},
},
},
}
Expand All @@ -183,7 +209,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptInternalValueInPlaceOfEnumLiteral(t
Data: nil,
Errors: []gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: `Argument "fromEnum" expected type "Color" but got: 1.`,
Message: "Argument \"fromEnum\" has invalid value 1.\nExpected type \"Color\", found 1.",
Locations: []location.SourceLocation{
{Line: 1, Column: 23},
},
},
},
}
Expand All @@ -199,7 +228,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptEnumLiteralInPlaceOfInt(t *testing.T
Data: nil,
Errors: []gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: `Argument "fromInt" expected type "Int" but got: GREEN.`,
Message: "Argument \"fromInt\" has invalid value GREEN.\nExpected type \"Int\", found GREEN.",
Locations: []location.SourceLocation{
{Line: 1, Column: 23},
},
},
},
}
Expand Down Expand Up @@ -240,6 +272,22 @@ func TestTypeSystem_EnumValues_AcceptsEnumLiteralsAsInputArgumentsToMutations(t
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestTypeSystem_EnumValues_AcceptsEnumLiteralsAsInputArgumentsToSubscriptions(t *testing.T) {
query := `subscription x($color: Color!) { subscribeToEnum(color: $color) }`
params := map[string]interface{}{
"color": "GREEN",
}
expected := &graphql.Result{
Data: map[string]interface{}{
"subscribeToEnum": "GREEN",
},
}
result := executeEnumTypeTestWithParams(t, query, params)
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestTypeSystem_EnumValues_DoesNotAcceptInternalValueAsEnumVariable(t *testing.T) {
query := `query test($color: Color!) { colorEnum(fromEnum: $color) }`
params := map[string]interface{}{
Expand All @@ -249,7 +297,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptInternalValueAsEnumVariable(t *testi
Data: nil,
Errors: []gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: `Variable "$color" expected value of type "Color!" but got: 2.`,
Message: "Variable \"$color\" got invalid value 2.\nExpected type \"Color\", found \"2\".",
Locations: []location.SourceLocation{
{Line: 1, Column: 12},
},
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions examples/todo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ var rootMutation = graphql.NewObject(graphql.ObjectConfig{
text, _ := params.Args["text"].(string)

// figure out new id
newId := RandStringRunes(8)
newID := RandStringRunes(8)

// perform mutation operation here
// for e.g. create a Todo and save to DB.
newTodo := Todo{
ID: newId,
ID: newID,
Text: text,
Done: false,
}
Expand Down

0 comments on commit a5cf5f2

Please sign in to comment.