1
1
# Directives
2
2
3
3
* [ Build-In] ( #subkit-build-in )
4
- * [ @mock ] ( #@mock )
5
- * [ @fetchJSON ] ( #@fetchJSON )
6
- * [ @publish ] ( #@publish )
7
- * [ @contextify ] ( #@contextify )
4
+ * [ @mock ] ( #mock )
5
+ * [ @fetchJSON / @getJSON ] ( #fetchJSON-getJSON )
6
+ * [ @postJSON ] ( #postJSON )
7
+ * [ @putJSON ] ( #putJSON )
8
+ * [ @deleteJSON ] ( #deleteJSON )
9
+ * [ @publish ] ( #publish )
10
+ * [ @contextify ] ( #contextify )
8
11
* [ Programming Custom-Directives] ( #programming-custom-directives )
9
12
10
13
GraphQL directives are an extremely powerful mechanism to customize query
@@ -22,24 +25,29 @@ provides 3 kinds of GraphQL schema directives:
22
25
23
26
## SubKit Build-In
24
27
25
- * [ @mock ] ( #@mock )
26
- * [ @fetchJSON ] ( #@fetchJSON )
27
- * [ @publish ] ( #@publish )
28
- * [ @contextify ] ( #@contextify )
28
+ * [ @mock ] ( #mock )
29
+ * [ @fetchJSON / @getJSON ] ( #fetchJSON-getJSON )
30
+ * [ @postJSON ] ( #postJSON )
31
+ * [ @putJSON ] ( #putJSON )
32
+ * [ @deleteJSON ] ( #deleteJSON )
33
+ * [ @publish ] ( #publish )
34
+ * [ @contextify ] ( #contextify )
29
35
30
- ### @ mock
36
+ ### mock
31
37
32
38
Mock field data.
33
39
34
- ` @mock(value: JSON) `
40
+ ``` graphql
41
+ @mock (value : JSON )
42
+ ```
35
43
36
44
* **value ** - Mock data to result in query .
37
45
38
- ### @ mock examples
46
+ ### mock examples
39
47
40
48
```graphql
41
49
query loadItem {
42
- item (id : " mikebild " ) {
50
+ item (id : " johndoe " ) {
43
51
id
44
52
email @mock (
value :
" [email protected] " )
45
53
picture
@@ -56,7 +64,7 @@ query loadItem {
56
64
57
65
``` graphql
58
66
query loadItem {
59
- item (id : " mikebild " ) {
67
+ item (id : " johndoe " ) {
60
68
id
61
69
email @mock (
value :
" [email protected] " )
62
70
picture {
@@ -68,7 +76,7 @@ query loadItem {
68
76
69
77
``` graphql
70
78
query loadItem {
71
- item (id : " mikebild " ) {
79
+ item (id : " johndoe " ) {
72
80
id
73
81
email @mock (
value :
" [email protected] " )
74
82
picture @mock (value : {link : " https://subkit.io" }) {
@@ -78,24 +86,29 @@ query loadItem {
78
86
}
79
87
```
80
88
81
- ### @ fetchJSON
89
+ ### fetchJSON / getJSON
82
90
83
- Publish an event to subscriptions by channelName.
91
+ Fetch / get data from URL as JSON. Executable on fields on client- and
92
+ server-side.
84
93
85
- ` @fetchJSON(url: String!, jsonQuery: String, timeout: Int) `
94
+ ``` graphql
95
+ @fetchJSON (url : String !, jsonQuery : String , timeout : Int )
96
+ @getJSON (url : String !, jsonQuery : String , timeout : Int )
97
+ ```
86
98
87
99
* **url ** -
88
100
[ES6 template strings ](https ://www .npmjs .com /package /es6 -template -strings )
89
101
provided URL to fetch JSON data .
90
102
* **jsonQuery ** - [JSON Query ](https ://www .npmjs .com /package /json -query ) to
91
103
transform the `@fetchJSON ` JSON response .
92
104
* **timeout ** - Set a network timeout in ms .
105
+ * **jwt ** - Set to "pass" for JWT passthrough , else given JWT content .
93
106
94
- #### @ fetchJSON examples
107
+ #### fetchJSON / getJSON examples
95
108
96
109
```graphql
97
110
query loadItem {
98
- item (id : " mikebild " ) {
111
+ item (id : " johndoe " ) {
99
112
id
100
113
email @mock (
value :
" [email protected] " )
101
114
picture @mock (value : {link : " https://subkit.io" }) {
@@ -114,7 +127,130 @@ query loadItem {
114
127
}
115
128
```
116
129
117
- ### @publish
130
+ ### postJSON
131
+
132
+ HTTP POST input data (args.input) to URL using JSON. Executable on mutation
133
+ field on client- and server-side.
134
+
135
+ ``` graphql
136
+ @postJSON (url : String !, jsonQuery : String , timeout : Int )
137
+ ```
138
+
139
+ * **url ** -
140
+ [ES6 template strings ](https ://www .npmjs .com /package /es6 -template -strings )
141
+ provided URL to fetch JSON data .
142
+ * **jsonQuery ** - [JSON Query ](https ://www .npmjs .com /package /json -query ) to
143
+ transform the `@fetchJSON ` JSON response .
144
+ * **timeout ** - Set a network timeout in ms .
145
+ * **jwt ** - Set to "pass" for JWT passthrough , else given JWT content .
146
+
147
+ #### postJSON examples
148
+
149
+ **Client -Side **
150
+
151
+ ```graphql
152
+ mutation upsertItem {
153
+ upsertItem (
input : {
id :
" johndoe" ,
email :
" [email protected] " })
154
+ @postJSON (url : " http://localhost:3000" , timeout : 1000 ) {
155
+ id
156
+ email
157
+ }
158
+ }
159
+ ```
160
+
161
+ ** Server-Side**
162
+
163
+ ``` graphql
164
+ type Mutation {
165
+ # Insert or update an item
166
+ upsertItem (input : ItemInput ! ): Item
167
+ @postJSON (url : " http://localhost:3000" , timeout : 1000 )
168
+ }
169
+ ```
170
+
171
+ ### putJSON
172
+
173
+ HTTP PUT input data (args.input) to URL using JSON . Executable on mutation field
174
+ on client - and server -side .
175
+
176
+ ```graphql
177
+ @putJSON (url : String !, jsonQuery : String , timeout : Int )
178
+ ```
179
+
180
+ * **url ** -
181
+ [ES6 template strings ](https ://www .npmjs .com /package /es6 -template -strings )
182
+ provided URL to fetch JSON data .
183
+ * **jsonQuery ** - [JSON Query ](https ://www .npmjs .com /package /json -query ) to
184
+ transform the `@fetchJSON ` JSON response .
185
+ * **timeout ** - Set a network timeout in ms .
186
+ * **jwt ** - Set to "pass" for JWT passthrough , else given JWT content .
187
+
188
+ #### putJSON examples
189
+
190
+ **Client -Side **
191
+
192
+ ```graphql
193
+ mutation upsertItem {
194
+ upsertItem (
input : {
id :
" johndoe" ,
email :
" [email protected] " })
195
+ @putJSON (url : " http://localhost:3000/${args.input.id}" , timeout : 1000 ) {
196
+ id
197
+ email
198
+ }
199
+ }
200
+ ```
201
+
202
+ ** Server-Side**
203
+
204
+ ``` graphql
205
+ type Mutation {
206
+ # Insert or update an item
207
+ upsertItem (input : ItemInput ! ): Item
208
+ @putJSON (url : " http://localhost:3000/${args.input.id}" , timeout : 1000 )
209
+ }
210
+ ```
211
+
212
+ ### deleteJSON
213
+
214
+ HTTP DELETE input id (args.id) to URL . Executable on mutation field on client -
215
+ and server -side .
216
+
217
+ ```graphql
218
+ @deleteJSON (url : String !, jsonQuery : String , timeout : Int )
219
+ ```
220
+
221
+ * **url ** -
222
+ [ES6 template strings ](https ://www .npmjs .com /package /es6 -template -strings )
223
+ provided URL to fetch JSON data .
224
+ * **jsonQuery ** - [JSON Query ](https ://www .npmjs .com /package /json -query ) to
225
+ transform the `@fetchJSON ` JSON response .
226
+ * **timeout ** - Set a network timeout in ms .
227
+ * **jwt ** - Set to "pass" for JWT passthrough , else given JWT content .
228
+
229
+ #### deleteJSON examples
230
+
231
+ **Client -Side **
232
+
233
+ ```graphql
234
+ mutation deleteItem {
235
+ deleteItem (id : " johndoe" )
236
+ @deleteJSON (url : " http://localhost:3000/${args.id}" , timeout : 1000 ) {
237
+ id
238
+ email
239
+ }
240
+ }
241
+ ```
242
+
243
+ ** Server-Side**
244
+
245
+ ``` graphql
246
+ type Mutation {
247
+ # Insert or update an item
248
+ deleteItem (id : ID ! ): Item
249
+ @deleteJSON (url : " http://localhost:3000/${args.id}" , timeout : 1000 )
250
+ }
251
+ ```
252
+
253
+ ### publish
118
254
119
255
Publish an event to subscriptions by channelName .
120
256
@@ -124,15 +260,15 @@ Publish an event to subscriptions by channelName.
124
260
* **payload ** - Mock payload data of event , otherwise input data is the payload
125
261
of the published event .
126
262
127
- #### @ publish examples
263
+ #### publish examples
128
264
129
265
Publish event to **itemsChannel ** with **mock payload ** after successful
130
266
mutation .
131
267
132
268
```graphql
133
269
mutation upsertItem {
134
- upsertItem (input : {id : " mikebild " , email : " mike@mikebild .com" })
135
- @publish (channelName : " itemsChannel" , payload : {id : " mikebild " }) {
270
+ upsertItem (input : {id : " johndoe " , email : " johndoe@example .com" })
271
+ @publish (channelName : " itemsChannel" , payload : {id : " johndoe " }) {
136
272
id
137
273
email
138
274
}
@@ -143,20 +279,20 @@ Publish event to itemsChannel with input payload after successful mutation.
143
279
144
280
``` graphql
145
281
mutation upsertItem {
146
- upsertItem (input : {id : " mikebild " , email : " mike@mikebild .com" }) @publish (channelName : " itemsChannel" }) {
282
+ upsertItem (input : {id : " johndoe " , email : " johndoe@example .com" }) @publish (channelName : " itemsChannel" }) {
147
283
id
148
284
email
149
285
}
150
286
}
151
287
```
152
288
153
- ### @ contextify
289
+ ### contextify
154
290
155
291
Temporary store field data in context and use it later in the execution process.
156
292
157
293
` @contextify `
158
294
159
- #### @ contextify examples
295
+ #### contextify examples
160
296
161
297
Contextify the result of viewer field to use it in subordinate field resolvers.
162
298
@@ -183,7 +319,7 @@ export const resolvers = {
183
319
184
320
## Programming Custom-Directives
185
321
186
- ### ` @ toUpperCase`
322
+ ### toUpperCase
187
323
188
324
Implement a custom ` @toUpperCase ` directive without arguments.
189
325
@@ -206,7 +342,7 @@ GraphQL query using `@toUpperCase` directive.
206
342
207
343
``` graphql
208
344
query loadItem {
209
- item (id : " mikebild " ) {
345
+ item (id : " johndoe " ) {
210
346
# Apply @toUpperCase directive on id field
211
347
id @toUpperCase
212
348
}
@@ -247,7 +383,7 @@ GraphQL query using `@toFormatString` directive.
247
383
248
384
``` graphql
249
385
query loadItem {
250
- item (id : " mikebild " ) {
386
+ item (id : " johndoe " ) {
251
387
# Apply @toFormatString directive on id field
252
388
id @toFormatString (template : " user-${id}" )
253
389
}
0 commit comments