1 module graphql.testschema;
2 
3 import std.algorithm.iteration : map;
4 import std.datetime : DateTime, Date;
5 import std.typecons : Nullable;
6 import std.format : format;
7 
8 import vibe.data.json;
9 import nullablestore;
10 
11 import graphql.schema.directives;
12 import graphql.helper;
13 
14 import graphql.uda;
15 
16 @safe:
17 
18 Json datetimeToJson(DateTime dt) {
19 	return Json(dt.toISOExtString());
20 }
21 
22 // The Schema used by graphqld
23 
24 union SearchResult {
25 	Android android;
26 	Humanoid humanoid;
27 	Starship ship;
28 }
29 
30 string dtToString(DateTime dt) {
31 	return dt.toISOExtString();
32 }
33 
34 @GQLDUda(TypeKind.OBJECT)
35 struct Query {
36 	@GQLDUda(
37 		GQLDDescription("Get the captain by Series")
38 	)
39 	Character captain(Series series);
40 	@GQLDUda(
41 		GQLDDeprecated(IsDeprecated.yes, "To complex")
42 	)
43 	SearchResult search(string name);
44 	Nullable!Starship starship(long id);
45 	Starship starshipDoesNotExist();
46 	Starship[] starships(float overSize = 100.0);
47 	Starship[] shipsselection(long[] ids);
48 	Nullable!Character character(long id);
49 	Character[] character(Series series);
50 	Humanoid[] humanoids();
51 	Android[] androids();
52 	Android[] resolverWillThrow();
53 	GQLDCustomLeaf!(DateTime, dtToString) currentTime();
54 	int currentTime();
55 
56 	@GQLDUda(Ignore.yes)
57 	void ignoreMe() {
58 	}
59 }
60 
61 unittest {
62 	Query d;
63 }
64 
65 @GQLDUda(TypeKind.INPUT_OBJECT)
66 struct AddCrewmanData {
67 	string name;
68 	long shipId;
69 	Series[] series;
70 }
71 
72 /*@GQLDUda(TypeKind.INPUT_OBJECT)
73 struct AddCrewmanComplexInput {
74 	Character crewman;
75 	Starship starship;
76 }*/
77 
78 interface Mutation {
79 	Character addCrewman(AddCrewmanData input);
80 	//Starship addCrewmanCompley(AddCrewmanComplexInput input);
81 }
82 
83 interface Subscription {
84 	Starship[] starships();
85 }
86 
87 class Schema {
88 	Query queryType;
89 	Mutation mutationType;
90 	Subscription subscriptionType;
91 	DefaultDirectives directives;
92 }
93 
94 enum Series {
95 	TheOriginalSeries,
96 	TheNextGeneration,
97 	DeepSpaceNine,
98 	Voyager,
99 	Enterprise,
100 	Discovery
101 }
102 
103 @GQLDUda(TypeKind.INTERFACE)
104 abstract class Character {
105 	long id;
106 	string name;
107 	Series[] series;
108 	Character[] commands;
109 	Nullable!Starship ship;
110 	NullableStore!Starship ships;
111 	Character[] commanders;
112 	Nullable!Starship allwaysNull;
113 	Nullable!int alsoAllwaysNull;
114 
115 	//NullableStore!AddCrewmanData data;
116 }
117 
118 class Humanoid : Character {
119 	string species;
120 	GQLDCustomLeaf!(Date, dToString) dateOfBirth;
121 }
122 
123 class Android : Character {
124 	string primaryFunction;
125 
126 	@GQLDUda(Ignore.yes)
127 	void ignoreMeToo() {
128 	}
129 }
130 
131 @GQLDUda(
132 	GQLDDescription("The thing Chracters fly around in")
133 )
134 class Starship {
135 	long id;
136 	string name;
137 
138 	@GQLDUda(
139 		GQLDDescription("The name used when speaking about the ship")
140 	)
141 	string designation;
142 	double size;
143 
144 	@GQLDUda(
145 		GQLDDescription("The person in charge")
146 	)
147 	Character commander;
148 	Nullable!(Series)[] series;
149 	Character[] crew;
150 
151 	this(long id, string designation, double size, string name) {
152 		this.id = id;
153 		this.designation = designation;
154 		this.size = size;
155 		this.name = name;
156 	}
157 
158 	override string toString() const @safe {
159 		return format!("Ship(id(%d), designation(%s), size(%.2f), name(%s)"
160 					~ "commander(%s), series[%(%s,%)], crew[%(%s,%)])")
161 			(
162 				 id, designation, size, name, commander.name, series,
163 				 crew.map!(a => a.name)
164 			);
165 	}
166 }