@@ -26,6 +26,7 @@ Now it also supports Deno 🦕
26
26
- [ File Upload] ( #file-upload )
27
27
- [ Browser] ( #browser )
28
28
- [ Node] ( #node )
29
+ - [ Deno] ( #deno )
29
30
- [ Batching] ( #batching )
30
31
- [ FAQ] ( #faq )
31
32
- [ Why do I have to install ` graphql ` ?] ( #why-do-i-have-to-install-graphql )
@@ -60,9 +61,9 @@ const query = gql`
60
61
}
61
62
` ;
62
63
63
- request (" https://api.graph.cool/simple/v1/movies" , query ). then (( data ) =>
64
- console . log ( data )
65
- );
64
+ const data = await request (" https://api.graph.cool/simple/v1/movies" , query );
65
+
66
+ console . log ( data );
66
67
```
67
68
68
69
## Usage
@@ -74,16 +75,18 @@ import {
74
75
} from " https://deno.land/x/graphql_request/mod.ts" ;
75
76
76
77
// Run GraphQL queries/mutations using a static function
77
- request (endpoint , query , variables ).then ((data ) => console .log (data ));
78
+ const data = await request (endpoint , query , variables );
79
+ console .log (data );
78
80
79
81
// ... or create a GraphQL client instance to send requests
80
82
const client = new GraphQLClient (endpoint , { headers: {} });
81
- client .request (query , variables ).then ((data ) => console .log (data ));
83
+ const data = client .request (query , variables );
84
+ console .log (data );
82
85
```
83
86
84
87
## Community
85
88
86
- #### GraphQL Code Generator's GraphQL-Request TypeScript Plugin
89
+ ### GraphQL Code Generator's GraphQL-Request TypeScript Plugin
87
90
88
91
A
89
92
[ GraphQL-Codegen plugin] ( https://graphql-code-generator.com/docs/plugins/typescript-graphql-request )
@@ -96,7 +99,7 @@ that generates a `graphql-request` ready-to-use SDK, which is fully-typed.
96
99
``` ts
97
100
import { gql , GraphQLClient } from " https://deno.land/x/graphql_request/mod.ts" ;
98
101
99
- async function main() {
102
+ const main = async () => {
100
103
const endpoint = " https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr" ;
101
104
102
105
const graphQLClient = new GraphQLClient (endpoint , {
@@ -118,13 +121,21 @@ async function main() {
118
121
119
122
const data = await graphQLClient .request (query );
120
123
console .log (JSON .stringify (data , undefined , 2 ));
121
- }
124
+ };
122
125
123
- main ().catch ((error ) => console .error (error ));
126
+ try {
127
+ main ();
128
+ } catch (error ) {
129
+ console .error (error );
130
+ }
124
131
```
125
132
126
133
[ TypeScript Source] ( examples/authentication-via-http-header.ts )
127
134
135
+ ``` bash
136
+ deno run --allow-net examples/authentication-via-http-header.ts
137
+ ```
138
+
128
139
#### Incrementally setting headers
129
140
130
141
If you want to set headers after the GraphQLClient has been initialised, you can
@@ -196,7 +207,7 @@ const data = await client.request(query, variables, requestHeaders);
196
207
``` ts
197
208
import { gql , GraphQLClient } from " https://deno.land/x/graphql_request/mod.ts" ;
198
209
199
- async function main() {
210
+ const main = async () => {
200
211
const endpoint = " https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr" ;
201
212
202
213
const graphQLClient = new GraphQLClient (endpoint , {
@@ -217,9 +228,13 @@ async function main() {
217
228
218
229
const data = await graphQLClient .request (query );
219
230
console .log (JSON .stringify (data , undefined , 2 ));
220
- }
231
+ };
221
232
222
- main ().catch ((error ) => console .error (error ));
233
+ try {
234
+ main ();
235
+ } catch (error ) {
236
+ console .error (error );
237
+ }
223
238
```
224
239
225
240
[ TypeScript Source] ( examples/passing-more-options-to-fetch.ts )
@@ -233,7 +248,7 @@ deno run --allow-net examples/passing-more-options-to-fetch.ts
233
248
``` ts
234
249
import { gql , request } from " https://deno.land/x/graphql_request/mod.ts" ;
235
250
236
- async function main() {
251
+ const main = async () => {
237
252
const endpoint = " https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr" ;
238
253
239
254
const query = gql `
@@ -253,17 +268,21 @@ async function main() {
253
268
254
269
const data = await request (endpoint , query , variables );
255
270
console .log (JSON .stringify (data , undefined , 2 ));
256
- }
271
+ };
257
272
258
- main ().catch ((error ) => console .error (error ));
273
+ try {
274
+ main ();
275
+ } catch (error ) {
276
+ console .error (error );
277
+ }
259
278
```
260
279
261
280
### GraphQL Mutations
262
281
263
282
``` ts
264
283
import { gql , GraphQLClient } from " https://deno.land/x/graphql_request/mod.ts" ;
265
284
266
- async function main() {
285
+ const main = async () => {
267
286
const endpoint = " https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr" ;
268
287
269
288
const graphQLClient = new GraphQLClient (endpoint , {
@@ -288,9 +307,13 @@ async function main() {
288
307
const data = await graphQLClient .request (mutation , variables );
289
308
290
309
console .log (JSON .stringify (data , undefined , 2 ));
291
- }
310
+ };
292
311
293
- main ().catch ((error ) => console .error (error ));
312
+ try {
313
+ main ();
314
+ } catch (error ) {
315
+ console .error (error );
316
+ }
294
317
```
295
318
296
319
[ TypeScript Source] ( examples/using-variables.ts )
@@ -304,7 +327,7 @@ deno run --allow-net examples/using-variables.ts
304
327
``` ts
305
328
import { gql , request } from " https://deno.land/x/graphql_request/mod.ts" ;
306
329
307
- async function main() {
330
+ const main = async () => {
308
331
const endpoint = " https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr" ;
309
332
310
333
const query = gql `
@@ -325,9 +348,13 @@ async function main() {
325
348
console .error (JSON .stringify (error , undefined , 2 ));
326
349
process .exit (1 );
327
350
}
328
- }
351
+ };
329
352
330
- main ().catch ((error ) => console .error (error ));
353
+ try {
354
+ main ();
355
+ } catch (error ) {
356
+ console .error (error );
357
+ }
331
358
```
332
359
333
360
[ TypeScript Source] ( examples/error-handling.ts )
@@ -342,7 +369,7 @@ deno run --allow-net examples/error-handling.ts
342
369
import { GraphQLClient } from " https://deno.land/x/graphql_request/mod.ts" ;
343
370
import { wrapFetch } from " https://deno.land/x/fetch_goody@v5.0.0/mod.ts" ;
344
371
345
- const start = async () => {
372
+ const main = async () => {
346
373
const endpoint = " https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr" ;
347
374
348
375
const graphQLClient = new GraphQLClient (endpoint , { fetch: wrapFetch () });
@@ -366,7 +393,11 @@ const start = async () => {
366
393
console .log (JSON .stringify (data , undefined , 2 ));
367
394
};
368
395
369
- start ().catch ((error ) => console .error (error ));
396
+ try {
397
+ main ();
398
+ } catch (error ) {
399
+ console .error (error );
400
+ }
370
401
```
371
402
372
403
### Receiving a raw response
@@ -377,7 +408,7 @@ If you need to access the `extensions` key you can use the `rawRequest` method:
377
408
``` ts
378
409
import { gql , rawRequest } from " https://deno.land/x/graphql_request/mod.ts" ;
379
410
380
- async function main() {
411
+ const main = async () => {
381
412
const endpoint = " https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr" ;
382
413
383
414
const query = gql `
@@ -398,9 +429,13 @@ async function main() {
398
429
console .log (
399
430
JSON .stringify ({ data , errors , extensions , headers , status }, undefined , 2 ),
400
431
);
401
- }
432
+ };
402
433
403
- main ().catch ((error ) => console .error (error ));
434
+ try {
435
+ main ();
436
+ } catch (error ) {
437
+ console .error (error );
438
+ }
404
439
```
405
440
406
441
[ TypeScript Source] ( examples/receiving-a-raw-response.ts )
@@ -414,7 +449,7 @@ deno run --allow-net examples/receiving-a-raw-response.ts
414
449
#### Browser
415
450
416
451
``` ts
417
- import { request } from " https://deno.land/x/graphql_request/mod.ts " ;
452
+ import { request } from " graphql-request " ;
418
453
419
454
const UploadUserAvatar = gql `
420
455
mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
@@ -432,7 +467,7 @@ request("/api/graphql", UploadUserAvatar, {
432
467
433
468
``` ts
434
469
import { createReadStream } from " fs" ;
435
- import { request } from " https://deno.land/x/graphql_request/mod.ts " ;
470
+ import { request } from " graphql-request " ;
436
471
437
472
const UploadUserAvatar = gql `
438
473
mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
@@ -446,6 +481,24 @@ request("/api/graphql", UploadUserAvatar, {
446
481
});
447
482
```
448
483
484
+ #### Deno
485
+
486
+ ``` ts
487
+ import { readableStreamFromReader as toStream } from " https://deno.land/std/io/mod.ts" ;
488
+ import { request } from " https://deno.land/x/graphql_request/mod.ts" ;
489
+
490
+ const UploadUserAvatar = gql `
491
+ mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
492
+ updateUser(id: $userId, input: { avatar: $file })
493
+ }
494
+ ` ;
495
+
496
+ request (" /api/graphql" , UploadUserAvatar , {
497
+ userId: 1 ,
498
+ file: toStream (await Deno .open ((" ./avatar.img" )),
499
+ });
500
+ ` ` `
501
+
449
502
### Batching
450
503
451
504
It is possible with ` graphql - request ` to use
@@ -456,7 +509,7 @@ via the `batchRequests()` function. Example available at
456
509
` ` ` ts
457
510
import { batchRequests } from " https://deno.land/x/graphql_request/mod.ts" ;
458
511
459
- ( async function () {
512
+ const main = async () => {
460
513
const endpoint = " https://api.spacex.land/graphql/" ;
461
514
462
515
const query1 = /* GraphQL */ `
@@ -481,9 +534,17 @@ import { batchRequests } from "https://deno.land/x/graphql_request/mod.ts";
481
534
{ document: query2 },
482
535
]);
483
536
console .log (JSON .stringify (data , undefined , 2 ));
484
- })().catch ((error ) => console .error (error ));
537
+ };
538
+
539
+ try {
540
+ main();
541
+ } catch (error ) {
542
+ console.error(error);
543
+ }
485
544
` ` `
486
545
546
+ [TypeScript Source](examples/batching-requests.ts)
547
+
487
548
` ` ` bash
488
549
deno run -- allow - net examples / batching - requests .ts
489
550
` ` `
0 commit comments