Skip to content

Commit

Permalink
first pass at tests for subscriptions
Browse files Browse the repository at this point in the history
Commit:
87c6a274fb24e08a2c85a93a9294e5711eed874b [87c6a27]
Parents:
a0c30d3475
Author:
Adam Miskiewicz <[email protected]>
Date:
17 October 2015 at 4:55:00 AM SGT
  • Loading branch information
sogko committed Mar 11, 2016
1 parent 1109e22 commit 9287d08
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 33 deletions.
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
42 changes: 40 additions & 2 deletions enum_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,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 @@ -240,6 +262,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 Down
65 changes: 64 additions & 1 deletion executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func TestThrowsIfNoOperationIsProvidedWithMultipleOperations(t *testing.T) {

func TestUsesTheQuerySchemaForQueries(t *testing.T) {

doc := `query Q { a } mutation M { c }`
doc := `query Q { a } mutation M { c } subscription S { a }`
data := map[string]interface{}{
"a": "b",
"c": "d",
Expand Down Expand Up @@ -691,6 +691,14 @@ func TestUsesTheQuerySchemaForQueries(t *testing.T) {
},
},
}),
Subscription: graphql.NewObject(graphql.ObjectConfig{
Name: "S",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
Expand Down Expand Up @@ -770,6 +778,61 @@ func TestUsesTheMutationSchemaForMutations(t *testing.T) {
}
}

func TestUsesTheSubscriptionSchemaForSubscriptions(t *testing.T) {

doc := `query Q { a } subscription S { a }`
data := map[string]interface{}{
"a": "b",
"c": "d",
}

expected := &graphql.Result{
Data: map[string]interface{}{
"a": "b",
},
}

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Q",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
Subscription: graphql.NewObject(graphql.ObjectConfig{
Name: "S",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}

// parse query
ast := testutil.TestParse(t, doc)

// execute
ep := graphql.ExecuteParams{
Schema: schema,
AST: ast,
Root: data,
OperationName: "S",
}
result := testutil.TestExecute(t, ep)
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 TestCorrectFieldOrderingDespiteExecutionOrder(t *testing.T) {

doc := `
Expand Down
7 changes: 3 additions & 4 deletions introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ func init() {

__Schema = NewObject(ObjectConfig{
Name: "__Schema",
Description: `A GraphQL Schema defines the capabilities of a GraphQL
server. It exposes all available types and directives on
the server, as well as the entry points for query and
mutation operations.`,
Description: `A GraphQL Schema defines the capabilities of a GraphQL server. ` +
`It exposes all available types and directives on the server, as well as ` +
`the entry points for query, mutation, and subscription operations.`,
Fields: Fields{
"types": &Field{
Description: "A list of all types supported by this server.",
Expand Down
34 changes: 16 additions & 18 deletions introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func TestIntrospection_ExecutesAnIntrospectionQuery(t *testing.T) {
}
expectedDataSubSet := map[string]interface{}{
"__schema": map[string]interface{}{
"mutationType": nil,
"mutationType": nil,
"subscriptionType": nil,
"queryType": map[string]interface{}{
"name": "QueryRoot",
},
Expand Down Expand Up @@ -93,6 +94,16 @@ func TestIntrospection_ExecutesAnIntrospectionQuery(t *testing.T) {
"isDeprecated": false,
"deprecationReason": nil,
},
map[string]interface{}{
"name": "subscriptionType",
"args": []interface{}{},
"type": map[string]interface{}{
"kind": "OBJECT",
"name": "__Type",
},
"isDeprecated": false,
"deprecationReason": nil,
},
map[string]interface{}{
"name": "directives",
"args": []interface{}{},
Expand Down Expand Up @@ -1258,27 +1269,14 @@ func TestIntrospection_ExposesDescriptionsOnTypesAndFields(t *testing.T) {
}
`

/*
[Data["schemaType"]["fields"][0]["name"]: "types" != "queryType" Data["schemaType"]["fields"][0]["description"]: "A list of all types supported by this server." != "The type that query operations will be rooted at."
Data["schemaType"]["fields"][1]["name"]: "queryType" != "mutationType"
Data["schemaType"]["fields"][1]["description"]: "The type that query operations will be rooted at." != "If this server supports mutation, the type that mutation operations will be rooted at."
Data["schemaType"]["fields"][2]["name"]: "mutationType" != "subscriptionType"
Data["schemaType"]["fields"][2]["description"]: "If this server supports mutation, the type that mutation
operations will be rooted at." != "If this server support subscription, the type that subscription operations will be rooted at." Data["schemaType"]["fields"][3]["name"]: "subscriptionType" != "directives"
Data["schemaType"]["fields"][3]["description"]: "If this server supports subscription, the type that subscription operations will be rooted at." != "A list of all directives supported by this server."
Data["schemaType"]["fields"][4]["description"]: "A list of all directives supported by this server." != "A list of all types supported by this server."
Data["schemaType"]["fields"][4]["name"]: "directives" != "types"]
*/
expected := &graphql.Result{
Data: map[string]interface{}{
"schemaType": map[string]interface{}{
"name": "__Schema",
"description": `A GraphQL Schema defines the capabilities of a GraphQL
server. It exposes all available types and directives on
the server, as well as the entry points for query and
mutation operations.`,
"description": `A GraphQL Schema defines the capabilities of a GraphQL ` +
`server. It exposes all available types and directives on ` +
`the server, as well as the entry points for query, mutation, ` +
`and subscription operations.`,
"fields": []interface{}{
map[string]interface{}{
"name": "types",
Expand Down
13 changes: 13 additions & 0 deletions kitchen-sink.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ mutation favPost {
}
}

subscription PostFavSubscription($input: StoryLikeSubscribeInput) {
postFavSubscribe(input: $input) {
post {
favers {
count
}
favSentence {
text
}
}
}
}

fragment frag on Follower {
foo(size: $size, bar: $b, obj: {key: "value"})
}
Expand Down
13 changes: 13 additions & 0 deletions language/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ mutation favPost {
}
}
subscription PostFavSubscription($input: StoryLikeSubscribeInput) {
postFavSubscribe(input: $input) {
post {
favers {
count
}
favSentence {
text
}
}
}
}
fragment frag on Follower {
foo(size: $size, bar: $b, obj: {key: "value"})
}
Expand Down
Loading

0 comments on commit 9287d08

Please sign in to comment.