Skip to content

Commit 3aaab2b

Browse files
committed
add test for kitchen-sink.graphql output
1 parent 48b15b2 commit 3aaab2b

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
66

77
FILE(COPY valgrind.supp DESTINATION .)
88

9+
FILE(COPY kitchen-sink.graphql DESTINATION .)
10+
11+
FILE(COPY kitchen-sink.json DESTINATION .)
12+
913
ADD_EXECUTABLE(runTests ParserTests.cpp JsonVisitorTests.cpp BuildCAPI.c)
1014

1115
TARGET_LINK_LIBRARIES(runTests gtest gtest_main)

test/ParserTests.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
#include <gtest/gtest.h>
1111
#include <cstdlib>
12+
#include <fstream>
13+
#include <sstream>
1214

1315
#include "Ast.h"
1416
#include "GraphQLParser.h"
17+
#include "c/GraphQLAstToJSON.h"
1518

1619
using namespace facebook::graphql;
1720
using namespace facebook::graphql::ast;
@@ -259,3 +262,24 @@ TEST(ParserTests, AllowsNonKeywordsForNames) {
259262
expectSuccess(str.str().c_str());
260263
}
261264
}
265+
266+
TEST(ParserTests, ProducesCorrectOutputForKitchenSink) {
267+
// Make sure we produce correct saved output for
268+
// kitchen-sink.graphql from graphql-js.
269+
FILE *fp = fopen("test/kitchen-sink.graphql", "r");
270+
ASSERT_NE(nullptr, fp);
271+
const char *error = nullptr;
272+
auto ast = parseFile(fp, &error);
273+
ASSERT_TRUE(ast);
274+
ASSERT_FALSE(error);
275+
fclose(fp);
276+
277+
const char *json = graphql_ast_to_json((const struct GraphQLAstNode *)ast.get());
278+
std::ifstream ifs("test/kitchen-sink.json");
279+
std::stringstream ss;
280+
ss << ifs.rdbuf();
281+
EXPECT_STREQ(
282+
json,
283+
ss.str().c_str());
284+
free((void *)json);
285+
}

test/kitchen-sink.graphql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2015, Facebook, Inc.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree. An additional grant
6+
# of patent rights can be found in the PATENTS file in the same directory.
7+
8+
query queryName($foo: ComplexType, $site: Site = MOBILE) {
9+
whoever123is: node(id: [123, 456]) {
10+
id ,
11+
... on User @defer {
12+
field2 {
13+
id ,
14+
alias: field1(first:10, after:$foo,) @include(if: $foo) {
15+
id,
16+
...frag
17+
}
18+
}
19+
}
20+
... @skip(unless: $foo) {
21+
id
22+
}
23+
... {
24+
id
25+
}
26+
}
27+
}
28+
29+
mutation likeStory {
30+
like(story: 123) @defer {
31+
story {
32+
id
33+
}
34+
}
35+
}
36+
37+
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {
38+
storyLikeSubscribe(input: $input) {
39+
story {
40+
likers {
41+
count
42+
}
43+
likeSentence {
44+
text
45+
}
46+
}
47+
}
48+
}
49+
50+
fragment frag on Friend {
51+
foo(size: $size, bar: $b, obj: {key: "value"})
52+
}
53+
54+
{
55+
unnamed(truthy: true, falsey: false, nullish: null),
56+
query
57+
}

test/kitchen-sink.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"kind":"Document","loc":{"start":1,"end":2},"definitions":[{"kind":"OperationDefinition","loc":{"start":1,"end":2},"operation":"query","name": {"kind":"Name","loc":{"start":7,"end":16},"value":"queryName"},"variableDefinitions":[{"kind":"VariableDefinition","loc":{"start":17,"end":34},"variable":{"kind":"Variable","loc":{"start":17,"end":21},"name":{"kind":"Name","loc":{"start":17,"end":21},"value":"foo"}},"type":{"kind":"NamedType","loc":{"start":23,"end":34},"name":{"kind":"Name","loc":{"start":23,"end":34},"value":"ComplexType"}},"defaultValue":null},{"kind":"VariableDefinition","loc":{"start":36,"end":56},"variable":{"kind":"Variable","loc":{"start":36,"end":41},"name":{"kind":"Name","loc":{"start":36,"end":41},"value":"site"}},"type":{"kind":"NamedType","loc":{"start":43,"end":47},"name":{"kind":"Name","loc":{"start":43,"end":47},"value":"Site"}},"defaultValue":{"kind":"EnumValue","loc":{"start":50,"end":56},"value":"MOBILE"}}],"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":58,"end":2},"selections":[{"kind":"Field","loc":{"start":3,"end":4},"alias":{"kind":"Name","loc":{"start":3,"end":15},"value":"whoever123is"},"name":{"kind":"Name","loc":{"start":17,"end":21},"value":"node"},"arguments":[{"kind":"Argument","loc":{"start":22,"end":36},"name":{"kind":"Name","loc":{"start":22,"end":24},"value":"id"},"value":{"kind":"ListValue","loc":{"start":26,"end":36},"values":[{"kind":"IntValue","loc":{"start":27,"end":30},"value":"123"},{"kind":"IntValue","loc":{"start":32,"end":35},"value":"456"}]}}],"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":38,"end":4},"selections":[{"kind":"Field","loc":{"start":5,"end":7},"alias":null,"name":{"kind":"Name","loc":{"start":5,"end":7},"value":"id"},"arguments":null,"directives":null,"selectionSet":null},{"kind":"InlineFragment","loc":{"start":5,"end":6},"typeCondition":{"kind":"NamedType","loc":{"start":12,"end":16},"name":{"kind":"Name","loc":{"start":12,"end":16},"value":"User"}},"directives":[{"kind":"Directive","loc":{"start":17,"end":23},"name":{"kind":"Name","loc":{"start":18,"end":23},"value":"defer"},"arguments":null}],"selectionSet":{"kind":"SelectionSet","loc":{"start":24,"end":6},"selections":[{"kind":"Field","loc":{"start":7,"end":8},"alias":null,"name":{"kind":"Name","loc":{"start":7,"end":13},"value":"field2"},"arguments":null,"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":14,"end":8},"selections":[{"kind":"Field","loc":{"start":9,"end":11},"alias":null,"name":{"kind":"Name","loc":{"start":9,"end":11},"value":"id"},"arguments":null,"directives":null,"selectionSet":null},{"kind":"Field","loc":{"start":9,"end":10},"alias":{"kind":"Name","loc":{"start":9,"end":14},"value":"alias"},"name":{"kind":"Name","loc":{"start":16,"end":22},"value":"field1"},"arguments":[{"kind":"Argument","loc":{"start":23,"end":31},"name":{"kind":"Name","loc":{"start":23,"end":28},"value":"first"},"value":{"kind":"IntValue","loc":{"start":29,"end":31},"value":"10"}},{"kind":"Argument","loc":{"start":33,"end":43},"name":{"kind":"Name","loc":{"start":33,"end":38},"value":"after"},"value":{"kind":"Variable","loc":{"start":39,"end":43},"name":{"kind":"Name","loc":{"start":39,"end":43},"value":"foo"}}}],"directives":[{"kind":"Directive","loc":{"start":46,"end":64},"name":{"kind":"Name","loc":{"start":47,"end":54},"value":"include"},"arguments":[{"kind":"Argument","loc":{"start":55,"end":63},"name":{"kind":"Name","loc":{"start":55,"end":57},"value":"if"},"value":{"kind":"Variable","loc":{"start":59,"end":63},"name":{"kind":"Name","loc":{"start":59,"end":63},"value":"foo"}}}]}],"selectionSet":{"kind":"SelectionSet","loc":{"start":65,"end":10},"selections":[{"kind":"Field","loc":{"start":11,"end":13},"alias":null,"name":{"kind":"Name","loc":{"start":11,"end":13},"value":"id"},"arguments":null,"directives":null,"selectionSet":null},{"kind":"FragmentSpread","loc":{"start":11,"end":18},"name":{"kind":"Name","loc":{"start":14,"end":18},"value":"frag"},"directives":null}]}}]}}]}},{"kind":"InlineFragment","loc":{"start":5,"end":6},"directives":[{"kind":"Directive","loc":{"start":9,"end":28},"name":{"kind":"Name","loc":{"start":10,"end":14},"value":"skip"},"arguments":[{"kind":"Argument","loc":{"start":15,"end":27},"name":{"kind":"Name","loc":{"start":15,"end":21},"value":"unless"},"value":{"kind":"Variable","loc":{"start":23,"end":27},"name":{"kind":"Name","loc":{"start":23,"end":27},"value":"foo"}}}]}],"selectionSet":{"kind":"SelectionSet","loc":{"start":29,"end":6},"selections":[{"kind":"Field","loc":{"start":7,"end":9},"alias":null,"name":{"kind":"Name","loc":{"start":7,"end":9},"value":"id"},"arguments":null,"directives":null,"selectionSet":null}]}},{"kind":"InlineFragment","loc":{"start":5,"end":6},"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":9,"end":6},"selections":[{"kind":"Field","loc":{"start":7,"end":9},"alias":null,"name":{"kind":"Name","loc":{"start":7,"end":9},"value":"id"},"arguments":null,"directives":null,"selectionSet":null}]}}]}}]}},{"kind":"OperationDefinition","loc":{"start":1,"end":2},"operation":"mutation","name": {"kind":"Name","loc":{"start":10,"end":19},"value":"likeStory"},"variableDefinitions":null,"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":20,"end":2},"selections":[{"kind":"Field","loc":{"start":3,"end":4},"alias":null,"name":{"kind":"Name","loc":{"start":3,"end":7},"value":"like"},"arguments":[{"kind":"Argument","loc":{"start":8,"end":18},"name":{"kind":"Name","loc":{"start":8,"end":13},"value":"story"},"value":{"kind":"IntValue","loc":{"start":15,"end":18},"value":"123"}}],"directives":[{"kind":"Directive","loc":{"start":20,"end":26},"name":{"kind":"Name","loc":{"start":21,"end":26},"value":"defer"},"arguments":null}],"selectionSet":{"kind":"SelectionSet","loc":{"start":27,"end":4},"selections":[{"kind":"Field","loc":{"start":5,"end":6},"alias":null,"name":{"kind":"Name","loc":{"start":5,"end":10},"value":"story"},"arguments":null,"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":11,"end":6},"selections":[{"kind":"Field","loc":{"start":7,"end":9},"alias":null,"name":{"kind":"Name","loc":{"start":7,"end":9},"value":"id"},"arguments":null,"directives":null,"selectionSet":null}]}}]}}]}},{"kind":"OperationDefinition","loc":{"start":1,"end":2},"operation":"subscription","name": {"kind":"Name","loc":{"start":14,"end":35},"value":"StoryLikeSubscription"},"variableDefinitions":[{"kind":"VariableDefinition","loc":{"start":36,"end":67},"variable":{"kind":"Variable","loc":{"start":36,"end":42},"name":{"kind":"Name","loc":{"start":36,"end":42},"value":"input"}},"type":{"kind":"NamedType","loc":{"start":44,"end":67},"name":{"kind":"Name","loc":{"start":44,"end":67},"value":"StoryLikeSubscribeInput"}},"defaultValue":null}],"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":69,"end":2},"selections":[{"kind":"Field","loc":{"start":3,"end":4},"alias":null,"name":{"kind":"Name","loc":{"start":3,"end":21},"value":"storyLikeSubscribe"},"arguments":[{"kind":"Argument","loc":{"start":22,"end":35},"name":{"kind":"Name","loc":{"start":22,"end":27},"value":"input"},"value":{"kind":"Variable","loc":{"start":29,"end":35},"name":{"kind":"Name","loc":{"start":29,"end":35},"value":"input"}}}],"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":37,"end":4},"selections":[{"kind":"Field","loc":{"start":5,"end":6},"alias":null,"name":{"kind":"Name","loc":{"start":5,"end":10},"value":"story"},"arguments":null,"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":11,"end":6},"selections":[{"kind":"Field","loc":{"start":7,"end":8},"alias":null,"name":{"kind":"Name","loc":{"start":7,"end":13},"value":"likers"},"arguments":null,"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":14,"end":8},"selections":[{"kind":"Field","loc":{"start":9,"end":14},"alias":null,"name":{"kind":"Name","loc":{"start":9,"end":14},"value":"count"},"arguments":null,"directives":null,"selectionSet":null}]}},{"kind":"Field","loc":{"start":7,"end":8},"alias":null,"name":{"kind":"Name","loc":{"start":7,"end":19},"value":"likeSentence"},"arguments":null,"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":20,"end":8},"selections":[{"kind":"Field","loc":{"start":9,"end":13},"alias":null,"name":{"kind":"Name","loc":{"start":9,"end":13},"value":"text"},"arguments":null,"directives":null,"selectionSet":null}]}}]}}]}}]}},{"kind":"FragmentDefinition","loc":{"start":1,"end":2},"name":{"kind":"Name","loc":{"start":10,"end":14},"value":"frag"},"typeCondition":{"kind":"NamedType","loc":{"start":18,"end":24},"name":{"kind":"Name","loc":{"start":18,"end":24},"value":"Friend"}},"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":25,"end":2},"selections":[{"kind":"Field","loc":{"start":3,"end":54},"alias":null,"name":{"kind":"Name","loc":{"start":3,"end":6},"value":"foo"},"arguments":[{"kind":"Argument","loc":{"start":7,"end":18},"name":{"kind":"Name","loc":{"start":7,"end":11},"value":"size"},"value":{"kind":"Variable","loc":{"start":13,"end":18},"name":{"kind":"Name","loc":{"start":13,"end":18},"value":"size"}}},{"kind":"Argument","loc":{"start":20,"end":27},"name":{"kind":"Name","loc":{"start":20,"end":23},"value":"bar"},"value":{"kind":"Variable","loc":{"start":25,"end":27},"name":{"kind":"Name","loc":{"start":25,"end":27},"value":"b"}}},{"kind":"Argument","loc":{"start":29,"end":53},"name":{"kind":"Name","loc":{"start":29,"end":32},"value":"obj"},"value":{"kind":"ObjectValue","loc":{"start":34,"end":53},"fields":[{"kind":"ObjectField","loc":{"start":35,"end":39},"name":{"kind":"Name","loc":{"start":35,"end":38},"value":"key"},"value":{"kind":"StringValue","loc":{"start":38,"end":39},"value":"value"}}]}}],"directives":null,"selectionSet":null}]}},{"kind":"OperationDefinition","loc":{"start":1,"end":2},"operation":"query","name": null,"variableDefinitions":null,"directives":null,"selectionSet":{"kind":"SelectionSet","loc":{"start":1,"end":2},"selections":[{"kind":"Field","loc":{"start":3,"end":54},"alias":null,"name":{"kind":"Name","loc":{"start":3,"end":10},"value":"unnamed"},"arguments":[{"kind":"Argument","loc":{"start":11,"end":23},"name":{"kind":"Name","loc":{"start":11,"end":17},"value":"truthy"},"value":{"kind":"BooleanValue","loc":{"start":19,"end":23},"value":true}},{"kind":"Argument","loc":{"start":25,"end":38},"name":{"kind":"Name","loc":{"start":25,"end":31},"value":"falsey"},"value":{"kind":"BooleanValue","loc":{"start":33,"end":38},"value":false}},{"kind":"Argument","loc":{"start":40,"end":53},"name":{"kind":"Name","loc":{"start":40,"end":47},"value":"nullish"},"value":{"kind":"NullValue","loc":{"start":49,"end":53}}}],"directives":null,"selectionSet":null},{"kind":"Field","loc":{"start":3,"end":8},"alias":null,"name":{"kind":"Name","loc":{"start":3,"end":8},"value":"query"},"arguments":null,"directives":null,"selectionSet":null}]}}]}

0 commit comments

Comments
 (0)