Skip to content

Commit 7012d28

Browse files
committed
Update examples in README.md
1 parent 72950b9 commit 7012d28

File tree

1 file changed

+91
-30
lines changed

1 file changed

+91
-30
lines changed

README.md

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Now it also supports Deno 🦕
2626
- [File Upload](#file-upload)
2727
- [Browser](#browser)
2828
- [Node](#node)
29+
- [Deno](#deno)
2930
- [Batching](#batching)
3031
- [FAQ](#faq)
3132
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
@@ -60,9 +61,9 @@ const query = gql`
6061
}
6162
`;
6263

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);
6667
```
6768

6869
## Usage
@@ -74,16 +75,18 @@ import {
7475
} from "https://deno.land/x/graphql_request/mod.ts";
7576

7677
// 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);
7880

7981
// ... or create a GraphQL client instance to send requests
8082
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);
8285
```
8386

8487
## Community
8588

86-
#### GraphQL Code Generator's GraphQL-Request TypeScript Plugin
89+
### GraphQL Code Generator's GraphQL-Request TypeScript Plugin
8790

8891
A
8992
[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.
9699
```ts
97100
import { gql, GraphQLClient } from "https://deno.land/x/graphql_request/mod.ts";
98101

99-
async function main() {
102+
const main = async () => {
100103
const endpoint = "https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr";
101104

102105
const graphQLClient = new GraphQLClient(endpoint, {
@@ -118,13 +121,21 @@ async function main() {
118121

119122
const data = await graphQLClient.request(query);
120123
console.log(JSON.stringify(data, undefined, 2));
121-
}
124+
};
122125

123-
main().catch((error) => console.error(error));
126+
try {
127+
main();
128+
} catch (error) {
129+
console.error(error);
130+
}
124131
```
125132

126133
[TypeScript Source](examples/authentication-via-http-header.ts)
127134

135+
```bash
136+
deno run --allow-net examples/authentication-via-http-header.ts
137+
```
138+
128139
#### Incrementally setting headers
129140

130141
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);
196207
```ts
197208
import { gql, GraphQLClient } from "https://deno.land/x/graphql_request/mod.ts";
198209

199-
async function main() {
210+
const main = async () => {
200211
const endpoint = "https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr";
201212

202213
const graphQLClient = new GraphQLClient(endpoint, {
@@ -217,9 +228,13 @@ async function main() {
217228

218229
const data = await graphQLClient.request(query);
219230
console.log(JSON.stringify(data, undefined, 2));
220-
}
231+
};
221232

222-
main().catch((error) => console.error(error));
233+
try {
234+
main();
235+
} catch (error) {
236+
console.error(error);
237+
}
223238
```
224239

225240
[TypeScript Source](examples/passing-more-options-to-fetch.ts)
@@ -233,7 +248,7 @@ deno run --allow-net examples/passing-more-options-to-fetch.ts
233248
```ts
234249
import { gql, request } from "https://deno.land/x/graphql_request/mod.ts";
235250

236-
async function main() {
251+
const main = async () => {
237252
const endpoint = "https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr";
238253

239254
const query = gql`
@@ -253,17 +268,21 @@ async function main() {
253268

254269
const data = await request(endpoint, query, variables);
255270
console.log(JSON.stringify(data, undefined, 2));
256-
}
271+
};
257272

258-
main().catch((error) => console.error(error));
273+
try {
274+
main();
275+
} catch (error) {
276+
console.error(error);
277+
}
259278
```
260279

261280
### GraphQL Mutations
262281

263282
```ts
264283
import { gql, GraphQLClient } from "https://deno.land/x/graphql_request/mod.ts";
265284

266-
async function main() {
285+
const main = async () => {
267286
const endpoint = "https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr";
268287

269288
const graphQLClient = new GraphQLClient(endpoint, {
@@ -288,9 +307,13 @@ async function main() {
288307
const data = await graphQLClient.request(mutation, variables);
289308

290309
console.log(JSON.stringify(data, undefined, 2));
291-
}
310+
};
292311

293-
main().catch((error) => console.error(error));
312+
try {
313+
main();
314+
} catch (error) {
315+
console.error(error);
316+
}
294317
```
295318

296319
[TypeScript Source](examples/using-variables.ts)
@@ -304,7 +327,7 @@ deno run --allow-net examples/using-variables.ts
304327
```ts
305328
import { gql, request } from "https://deno.land/x/graphql_request/mod.ts";
306329

307-
async function main() {
330+
const main = async () => {
308331
const endpoint = "https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr";
309332

310333
const query = gql`
@@ -325,9 +348,13 @@ async function main() {
325348
console.error(JSON.stringify(error, undefined, 2));
326349
process.exit(1);
327350
}
328-
}
351+
};
329352

330-
main().catch((error) => console.error(error));
353+
try {
354+
main();
355+
} catch (error) {
356+
console.error(error);
357+
}
331358
```
332359

333360
[TypeScript Source](examples/error-handling.ts)
@@ -342,7 +369,7 @@ deno run --allow-net examples/error-handling.ts
342369
import { GraphQLClient } from "https://deno.land/x/graphql_request/mod.ts";
343370
import { wrapFetch } from "https://deno.land/x/fetch_goody@v5.0.0/mod.ts";
344371

345-
const start = async () => {
372+
const main = async () => {
346373
const endpoint = "https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr";
347374

348375
const graphQLClient = new GraphQLClient(endpoint, { fetch: wrapFetch() });
@@ -366,7 +393,11 @@ const start = async () => {
366393
console.log(JSON.stringify(data, undefined, 2));
367394
};
368395

369-
start().catch((error) => console.error(error));
396+
try {
397+
main();
398+
} catch (error) {
399+
console.error(error);
400+
}
370401
```
371402

372403
### Receiving a raw response
@@ -377,7 +408,7 @@ If you need to access the `extensions` key you can use the `rawRequest` method:
377408
```ts
378409
import { gql, rawRequest } from "https://deno.land/x/graphql_request/mod.ts";
379410

380-
async function main() {
411+
const main = async () => {
381412
const endpoint = "https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr";
382413

383414
const query = gql`
@@ -398,9 +429,13 @@ async function main() {
398429
console.log(
399430
JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2),
400431
);
401-
}
432+
};
402433

403-
main().catch((error) => console.error(error));
434+
try {
435+
main();
436+
} catch (error) {
437+
console.error(error);
438+
}
404439
```
405440

406441
[TypeScript Source](examples/receiving-a-raw-response.ts)
@@ -414,7 +449,7 @@ deno run --allow-net examples/receiving-a-raw-response.ts
414449
#### Browser
415450

416451
```ts
417-
import { request } from "https://deno.land/x/graphql_request/mod.ts";
452+
import { request } from "graphql-request";
418453

419454
const UploadUserAvatar = gql`
420455
mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
@@ -432,7 +467,7 @@ request("/api/graphql", UploadUserAvatar, {
432467

433468
```ts
434469
import { createReadStream } from "fs";
435-
import { request } from "https://deno.land/x/graphql_request/mod.ts";
470+
import { request } from "graphql-request";
436471

437472
const UploadUserAvatar = gql`
438473
mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
@@ -446,6 +481,24 @@ request("/api/graphql", UploadUserAvatar, {
446481
});
447482
```
448483

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+
449502
### Batching
450503
451504
It is possible with `graphql-request` to use
@@ -456,7 +509,7 @@ via the `batchRequests()` function. Example available at
456509
```ts
457510
import { batchRequests } from "https://deno.land/x/graphql_request/mod.ts";
458511

459-
(async function () {
512+
const main = async () => {
460513
const endpoint = "https://api.spacex.land/graphql/";
461514

462515
const query1 = /* GraphQL */ `
@@ -481,9 +534,17 @@ import { batchRequests } from "https://deno.land/x/graphql_request/mod.ts";
481534
{ document: query2 },
482535
]);
483536
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+
}
485544
```
486545
546+
[TypeScript Source](examples/batching-requests.ts)
547+
487548
```bash
488549
deno run --allow-net examples/batching-requests.ts
489550
```

0 commit comments

Comments
 (0)