Skip to content

Commit b4c9441

Browse files
committed
Add directives
1 parent ac25409 commit b4c9441

File tree

9 files changed

+643
-709
lines changed

9 files changed

+643
-709
lines changed

bin/subkit-serve

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function serve({
4141
secret = JWT_SECRET,
4242
graphiql = false,
4343
extentions = false,
44-
logStyle = 'extended',
44+
logStyle = 'error',
4545
logFormat = 'json'
4646
}) {
4747
if (!['none', 'error', 'short', 'extended'].includes(logStyle)) {

docs/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ Anyone is welcome to contribute to SubKit GraphQL-Server, just read
4545
* [Subscriptions](subscriptions.md)
4646
* [Setup and Subscribe to GraphQL-Subscriptions](subscriptions.md#graphql-subscriptions)
4747
* [Use Publish/Subscribe programmatically](subscriptions.md#use-publish/subscribe-programmatically)
48+
* [Publish via Web-Hooks](subscriptions.md#publish-via-web-hooks)
4849
* [Directives](directives.md)
4950
* [Build-In](directives.md#subkit-build-in)
50-
* [@mock](directives.md#@mock)
51-
* [@fetchJSON](directives.md#@fetchJSON)
52-
* [@publish](directives.md#@publish)
53-
* [@contextify](directives.md#@contextify)
51+
* [@mock](directives.md#mock)
52+
* [@fetchJSON / @getJSON](directives.md#fetchJSON-getJSON)
53+
* [@postJSON](directives.md#postJSON)
54+
* [@putJSON](directives.md#putJSON)
55+
* [@deleteJSON](directives.md#deleteJSON)
56+
* [@publish](directives.md#publish)
57+
* [@contextify](directives.md#contextify)
5458
* [Programming Custom-Directives](directives.md#programming-custom-directives)
5559
* [JSON Web Token (JWT) Authentication](jwt-auth.md)
5660
* [Encode a JSON Web Token](jwt-auth.md#encode-a-json-web-token)

docs/directives.md

Lines changed: 165 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# Directives
22

33
* [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)
811
* [Programming Custom-Directives](#programming-custom-directives)
912

1013
GraphQL directives are an extremely powerful mechanism to customize query
@@ -22,24 +25,29 @@ provides 3 kinds of GraphQL schema directives:
2225

2326
## SubKit Build-In
2427

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)
2935

30-
### @mock
36+
### mock
3137

3238
Mock field data.
3339

34-
`@mock(value: JSON)`
40+
```graphql
41+
@mock(value: JSON)
42+
```
3543

3644
* **value** - Mock data to result in query.
3745

38-
### @mock examples
46+
### mock examples
3947

4048
```graphql
4149
query loadItem {
42-
item(id: "mikebild") {
50+
item(id: "johndoe") {
4351
id
4452
email @mock(value: "[email protected]")
4553
picture
@@ -56,7 +64,7 @@ query loadItem {
5664

5765
```graphql
5866
query loadItem {
59-
item(id: "mikebild") {
67+
item(id: "johndoe") {
6068
id
6169
email @mock(value: "[email protected]")
6270
picture {
@@ -68,7 +76,7 @@ query loadItem {
6876

6977
```graphql
7078
query loadItem {
71-
item(id: "mikebild") {
79+
item(id: "johndoe") {
7280
id
7381
email @mock(value: "[email protected]")
7482
picture @mock(value: {link: "https://subkit.io"}) {
@@ -78,24 +86,29 @@ query loadItem {
7886
}
7987
```
8088

81-
### @fetchJSON
89+
### fetchJSON / getJSON
8290

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.
8493

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+
```
8698

8799
* **url** -
88100
[ES6 template strings](https://www.npmjs.com/package/es6-template-strings)
89101
provided URL to fetch JSON data.
90102
* **jsonQuery** - [JSON Query](https://www.npmjs.com/package/json-query) to
91103
transform the `@fetchJSON` JSON response.
92104
* **timeout** - Set a network timeout in ms.
105+
* **jwt** - Set to "pass" for JWT passthrough, else given JWT content.
93106

94-
#### @fetchJSON examples
107+
#### fetchJSON / getJSON examples
95108

96109
```graphql
97110
query loadItem {
98-
item(id: "mikebild") {
111+
item(id: "johndoe") {
99112
id
100113
email @mock(value: "[email protected]")
101114
picture @mock(value: {link: "https://subkit.io"}) {
@@ -114,7 +127,130 @@ query loadItem {
114127
}
115128
```
116129

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
118254

119255
Publish an event to subscriptions by channelName.
120256

@@ -124,15 +260,15 @@ Publish an event to subscriptions by channelName.
124260
* **payload** - Mock payload data of event, otherwise input data is the payload
125261
of the published event.
126262

127-
#### @publish examples
263+
#### publish examples
128264

129265
Publish event to **itemsChannel** with **mock payload** after successful
130266
mutation.
131267

132268
```graphql
133269
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"}) {
136272
id
137273
email
138274
}
@@ -143,20 +279,20 @@ Publish event to itemsChannel with input payload after successful mutation.
143279

144280
```graphql
145281
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"}) {
147283
id
148284
email
149285
}
150286
}
151287
```
152288

153-
### @contextify
289+
### contextify
154290

155291
Temporary store field data in context and use it later in the execution process.
156292

157293
`@contextify`
158294

159-
#### @contextify examples
295+
#### contextify examples
160296

161297
Contextify the result of viewer field to use it in subordinate field resolvers.
162298

@@ -183,7 +319,7 @@ export const resolvers = {
183319

184320
## Programming Custom-Directives
185321

186-
### `@toUpperCase`
322+
### toUpperCase
187323

188324
Implement a custom `@toUpperCase` directive without arguments.
189325

@@ -206,7 +342,7 @@ GraphQL query using `@toUpperCase` directive.
206342

207343
```graphql
208344
query loadItem {
209-
item(id: "mikebild") {
345+
item(id: "johndoe") {
210346
# Apply @toUpperCase directive on id field
211347
id @toUpperCase
212348
}
@@ -247,7 +383,7 @@ GraphQL query using `@toFormatString` directive.
247383

248384
```graphql
249385
query loadItem {
250-
item(id: "mikebild") {
386+
item(id: "johndoe") {
251387
# Apply @toFormatString directive on id field
252388
id @toFormatString(template: "user-${id}")
253389
}

0 commit comments

Comments
 (0)