1 module graphql.argumentextractortests;
2 
3 import std.format : format;
4 
5 import vibe.data.json;
6 
7 import graphql.ast;
8 import graphql.astselector;
9 import graphql.helper : lexAndParse;
10 import graphql.argumentextractor;
11 import graphql.testschema;
12 import graphql.graphql;
13 
14 unittest {
15 	string q = `
16 query a($s: boolean, $after: String) {
17 	starships(overSize: 10) {
18 		name
19 		crew @skip(if: $s) {
20 			...hyooman
21 			...robot
22 			...charac
23 		}
24 	}
25 	numberBetween(searchInput:
26 		{ first: 10
27 		, after: $after
28 		}
29 	) {
30 		id
31 	}
32 }
33 
34 fragment hyooman on Humanoid {
35 	species
36 	dateOfBirth
37 }
38 
39 fragment robot on Android {
40 	primaryFunction
41 }
42 
43 fragment charac on Character {
44 	...robot
45 	id
46 	...hyooman
47 	name(get: $s)
48 	series
49 }`;
50 
51 	Json vars = parseJsonString(`{ "s": false, "after": "Hello World" }`);
52 
53 	Document doc = cast()lexAndParse(q);
54 	assert(doc !is null);
55 
56 	{
57 		auto startships = astSelect!Field(doc, "a.starships");
58 		assert(startships !is null);
59 
60 		Json args = getArguments(cast()startships, vars);
61 		assert(args == parseJsonString(`{"overSize" : 10}`),
62 				format("%s", args)
63 			);
64 	}
65 
66 	{
67 		auto crew = astSelect!Field(doc, "a.starships.crew");
68 		assert(crew !is null);
69 
70 		Json args = getArguments(cast()crew, vars);
71 		assert(args == parseJsonString(`{"if" : false}`), format("%s", args));
72 	}
73 
74 	{
75 		auto name = astSelect!Field(doc, "a.starships.crew.name");
76 		assert(name !is null);
77 
78 		Json args = getArguments(cast()name, vars);
79 		assert(args == parseJsonString(`{"get" : false}`), format("%s", args));
80 	}
81 
82 	{
83 		auto searchInput = astSelect!Field(doc, "a.numberBetween");
84 		assert(searchInput !is null);
85 
86 		Json args = getArguments(cast()searchInput, vars);
87 		assert(args == parseJsonString(
88 			`{ "searchInput": {"first" : 10, "after": "Hello World"}}`),
89 				format("%s", args));
90 	}
91 }