Skip to content

Commit a5cf5f2

Browse files
committed
Merge pull request #117 from sogko/sogko/0.4.18
Port changes from `graphql-js` v0.4.18 (Oct 2015 spec)
2 parents 909ca9f + c4394ae commit a5cf5f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+5126
-1710
lines changed

definition.go

Lines changed: 202 additions & 190 deletions
Large diffs are not rendered by default.

definition_test.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ var blogMutation = graphql.NewObject(graphql.ObjectConfig{
9898
},
9999
})
100100

101+
var blogSubscription = graphql.NewObject(graphql.ObjectConfig{
102+
Name: "Subscription",
103+
Fields: graphql.Fields{
104+
"articleSubscribe": &graphql.Field{
105+
Type: blogArticle,
106+
Args: graphql.FieldConfigArgument{
107+
"id": &graphql.ArgumentConfig{
108+
Type: graphql.String,
109+
},
110+
},
111+
},
112+
},
113+
})
114+
101115
var objectType = graphql.NewObject(graphql.ObjectConfig{
102116
Name: "Object",
103117
IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool {
@@ -204,6 +218,7 @@ func TestTypeSystem_DefinitionExample_DefinesAQueryOnlySchema(t *testing.T) {
204218
t.Fatalf("feedField.Name expected to equal `feed`, got: %v", feedField.Name)
205219
}
206220
}
221+
207222
func TestTypeSystem_DefinitionExample_DefinesAMutationScheme(t *testing.T) {
208223
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
209224
Query: blogQuery,
@@ -233,6 +248,35 @@ func TestTypeSystem_DefinitionExample_DefinesAMutationScheme(t *testing.T) {
233248
}
234249
}
235250

251+
func TestTypeSystem_DefinitionExample_DefinesASubscriptionScheme(t *testing.T) {
252+
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
253+
Query: blogQuery,
254+
Subscription: blogSubscription,
255+
})
256+
if err != nil {
257+
t.Fatalf("unexpected error, got: %v", err)
258+
}
259+
260+
if blogSchema.SubscriptionType() != blogSubscription {
261+
t.Fatalf("expected blogSchema.SubscriptionType() == blogSubscription")
262+
}
263+
264+
subMutation, _ := blogSubscription.Fields()["articleSubscribe"]
265+
if subMutation == nil {
266+
t.Fatalf("subMutation is nil")
267+
}
268+
subMutationType := subMutation.Type
269+
if subMutationType != blogArticle {
270+
t.Fatalf("subMutationType expected to equal blogArticle, got: %v", subMutationType)
271+
}
272+
if subMutationType.Name() != "Article" {
273+
t.Fatalf("subMutationType.Name expected to equal `Article`, got: %v", subMutationType.Name())
274+
}
275+
if subMutation.Name != "articleSubscribe" {
276+
t.Fatalf("subMutation.Name expected to equal `articleSubscribe`, got: %v", subMutation.Name)
277+
}
278+
}
279+
236280
func TestTypeSystem_DefinitionExample_IncludesNestedInputObjectsInTheMap(t *testing.T) {
237281
nestedInputObject := graphql.NewInputObject(graphql.InputObjectConfig{
238282
Name: "NestedInputObject",
@@ -263,9 +307,23 @@ func TestTypeSystem_DefinitionExample_IncludesNestedInputObjectsInTheMap(t *test
263307
},
264308
},
265309
})
310+
someSubscription := graphql.NewObject(graphql.ObjectConfig{
311+
Name: "SomeSubscription",
312+
Fields: graphql.Fields{
313+
"subscribeToSomething": &graphql.Field{
314+
Type: blogArticle,
315+
Args: graphql.FieldConfigArgument{
316+
"input": &graphql.ArgumentConfig{
317+
Type: someInputObject,
318+
},
319+
},
320+
},
321+
},
322+
})
266323
schema, err := graphql.NewSchema(graphql.SchemaConfig{
267-
Query: blogQuery,
268-
Mutation: someMutation,
324+
Query: blogQuery,
325+
Mutation: someMutation,
326+
Subscription: someSubscription,
269327
})
270328
if err != nil {
271329
t.Fatalf("unexpected error, got: %v", err)

directives.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package graphql
22

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

12-
/**
13-
* Directives are used by the GraphQL runtime as a way of modifying execution
14-
* behavior. Type system creators will usually not create these directly.
15-
*/
1614
func NewDirective(config *Directive) *Directive {
1715
if config == nil {
1816
config = &Directive{}
@@ -27,10 +25,8 @@ func NewDirective(config *Directive) *Directive {
2725
}
2826
}
2927

30-
/**
31-
* Used to conditionally include fields or fragments
32-
*/
33-
var IncludeDirective *Directive = NewDirective(&Directive{
28+
// IncludeDirective is used to conditionally include fields or fragments
29+
var IncludeDirective = NewDirective(&Directive{
3430
Name: "include",
3531
Description: "Directs the executor to include this field or fragment only when " +
3632
"the `if` argument is true.",
@@ -46,10 +42,8 @@ var IncludeDirective *Directive = NewDirective(&Directive{
4642
OnField: true,
4743
})
4844

49-
/**
50-
* Used to conditionally skip (exclude) fields or fragments
51-
*/
52-
var SkipDirective *Directive = NewDirective(&Directive{
45+
// SkipDirective Used to conditionally skip (exclude) fields or fragments
46+
var SkipDirective = NewDirective(&Directive{
5347
Name: "skip",
5448
Description: "Directs the executor to skip this field or fragment when the `if` " +
5549
"argument is true.",

directives_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,100 @@ func TestDirectivesWorksOnInlineFragmentUnlessTrueIncludesInlineFragment(t *test
324324
}
325325
}
326326

327+
func TestDirectivesWorksOnAnonymousInlineFragmentIfFalseOmitsAnonymousInlineFragment(t *testing.T) {
328+
query := `
329+
query Q {
330+
a
331+
... @include(if: false) {
332+
b
333+
}
334+
}
335+
`
336+
expected := &graphql.Result{
337+
Data: map[string]interface{}{
338+
"a": "a",
339+
},
340+
}
341+
result := executeDirectivesTestQuery(t, query)
342+
if len(result.Errors) != 0 {
343+
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
344+
}
345+
if !reflect.DeepEqual(expected, result) {
346+
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
347+
}
348+
}
349+
350+
func TestDirectivesWorksOnAnonymousInlineFragmentIfTrueIncludesAnonymousInlineFragment(t *testing.T) {
351+
query := `
352+
query Q {
353+
a
354+
... @include(if: true) {
355+
b
356+
}
357+
}
358+
`
359+
expected := &graphql.Result{
360+
Data: map[string]interface{}{
361+
"a": "a",
362+
"b": "b",
363+
},
364+
}
365+
result := executeDirectivesTestQuery(t, query)
366+
if len(result.Errors) != 0 {
367+
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
368+
}
369+
if !reflect.DeepEqual(expected, result) {
370+
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
371+
}
372+
}
373+
374+
func TestDirectivesWorksOnAnonymousInlineFragmentUnlessFalseIncludesAnonymousInlineFragment(t *testing.T) {
375+
query := `
376+
query Q {
377+
a
378+
... @skip(if: false) {
379+
b
380+
}
381+
}
382+
`
383+
expected := &graphql.Result{
384+
Data: map[string]interface{}{
385+
"a": "a",
386+
"b": "b",
387+
},
388+
}
389+
result := executeDirectivesTestQuery(t, query)
390+
if len(result.Errors) != 0 {
391+
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
392+
}
393+
if !reflect.DeepEqual(expected, result) {
394+
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
395+
}
396+
}
397+
398+
func TestDirectivesWorksOnAnonymousInlineFragmentUnlessTrueIncludesAnonymousInlineFragment(t *testing.T) {
399+
query := `
400+
query Q {
401+
a
402+
... @skip(if: true) {
403+
b
404+
}
405+
}
406+
`
407+
expected := &graphql.Result{
408+
Data: map[string]interface{}{
409+
"a": "a",
410+
},
411+
}
412+
result := executeDirectivesTestQuery(t, query)
413+
if len(result.Errors) != 0 {
414+
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
415+
}
416+
if !reflect.DeepEqual(expected, result) {
417+
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
418+
}
419+
}
420+
327421
func TestDirectivesWorksOnFragmentIfFalseOmitsFragment(t *testing.T) {
328422
query := `
329423
query Q {

enum_type_test.go

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/graphql-go/graphql"
88
"github.com/graphql-go/graphql/gqlerrors"
9+
"github.com/graphql-go/graphql/language/location"
910
"github.com/graphql-go/graphql/testutil"
1011
)
1112

@@ -93,9 +94,31 @@ var enumTypeTestMutationType = graphql.NewObject(graphql.ObjectConfig{
9394
},
9495
},
9596
})
97+
98+
var enumTypeTestSubscriptionType = graphql.NewObject(graphql.ObjectConfig{
99+
Name: "Subscription",
100+
Fields: graphql.Fields{
101+
"subscribeToEnum": &graphql.Field{
102+
Type: enumTypeTestColorType,
103+
Args: graphql.FieldConfigArgument{
104+
"color": &graphql.ArgumentConfig{
105+
Type: enumTypeTestColorType,
106+
},
107+
},
108+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
109+
if color, ok := p.Args["color"]; ok {
110+
return color, nil
111+
}
112+
return nil, nil
113+
},
114+
},
115+
},
116+
})
117+
96118
var enumTypeTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
97-
Query: enumTypeTestQueryType,
98-
Mutation: enumTypeTestMutationType,
119+
Query: enumTypeTestQueryType,
120+
Mutation: enumTypeTestMutationType,
121+
Subscription: enumTypeTestSubscriptionType,
99122
})
100123

101124
func executeEnumTypeTest(t *testing.T, query string) *graphql.Result {
@@ -156,7 +179,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptStringLiterals(t *testing.T) {
156179
Data: nil,
157180
Errors: []gqlerrors.FormattedError{
158181
gqlerrors.FormattedError{
159-
Message: `Argument "fromEnum" expected type "Color" but got: "GREEN".`,
182+
Message: "Argument \"fromEnum\" has invalid value \"GREEN\".\nExpected type \"Color\", found \"GREEN\".",
183+
Locations: []location.SourceLocation{
184+
{Line: 1, Column: 23},
185+
},
160186
},
161187
},
162188
}
@@ -183,7 +209,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptInternalValueInPlaceOfEnumLiteral(t
183209
Data: nil,
184210
Errors: []gqlerrors.FormattedError{
185211
gqlerrors.FormattedError{
186-
Message: `Argument "fromEnum" expected type "Color" but got: 1.`,
212+
Message: "Argument \"fromEnum\" has invalid value 1.\nExpected type \"Color\", found 1.",
213+
Locations: []location.SourceLocation{
214+
{Line: 1, Column: 23},
215+
},
187216
},
188217
},
189218
}
@@ -199,7 +228,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptEnumLiteralInPlaceOfInt(t *testing.T
199228
Data: nil,
200229
Errors: []gqlerrors.FormattedError{
201230
gqlerrors.FormattedError{
202-
Message: `Argument "fromInt" expected type "Int" but got: GREEN.`,
231+
Message: "Argument \"fromInt\" has invalid value GREEN.\nExpected type \"Int\", found GREEN.",
232+
Locations: []location.SourceLocation{
233+
{Line: 1, Column: 23},
234+
},
203235
},
204236
},
205237
}
@@ -240,6 +272,22 @@ func TestTypeSystem_EnumValues_AcceptsEnumLiteralsAsInputArgumentsToMutations(t
240272
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
241273
}
242274
}
275+
276+
func TestTypeSystem_EnumValues_AcceptsEnumLiteralsAsInputArgumentsToSubscriptions(t *testing.T) {
277+
query := `subscription x($color: Color!) { subscribeToEnum(color: $color) }`
278+
params := map[string]interface{}{
279+
"color": "GREEN",
280+
}
281+
expected := &graphql.Result{
282+
Data: map[string]interface{}{
283+
"subscribeToEnum": "GREEN",
284+
},
285+
}
286+
result := executeEnumTypeTestWithParams(t, query, params)
287+
if !reflect.DeepEqual(expected, result) {
288+
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
289+
}
290+
}
243291
func TestTypeSystem_EnumValues_DoesNotAcceptInternalValueAsEnumVariable(t *testing.T) {
244292
query := `query test($color: Color!) { colorEnum(fromEnum: $color) }`
245293
params := map[string]interface{}{
@@ -249,7 +297,10 @@ func TestTypeSystem_EnumValues_DoesNotAcceptInternalValueAsEnumVariable(t *testi
249297
Data: nil,
250298
Errors: []gqlerrors.FormattedError{
251299
gqlerrors.FormattedError{
252-
Message: `Variable "$color" expected value of type "Color!" but got: 2.`,
300+
Message: "Variable \"$color\" got invalid value 2.\nExpected type \"Color\", found \"2\".",
301+
Locations: []location.SourceLocation{
302+
{Line: 1, Column: 12},
303+
},
253304
},
254305
},
255306
}

examples/todo/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ var rootMutation = graphql.NewObject(graphql.ObjectConfig{
7575
text, _ := params.Args["text"].(string)
7676

7777
// figure out new id
78-
newId := RandStringRunes(8)
78+
newID := RandStringRunes(8)
7979

8080
// perform mutation operation here
8181
// for e.g. create a Todo and save to DB.
8282
newTodo := Todo{
83-
ID: newId,
83+
ID: newID,
8484
Text: text,
8585
Done: false,
8686
}

0 commit comments

Comments
 (0)