Skip to content

Commit e596527

Browse files
committed
chore(docs): make use of gql tag
1 parent a446365 commit e596527

12 files changed

+245
-252
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -609,33 +609,33 @@ request('/api/graphql', UploadUserAvatar, {
609609
It is possible with `graphql-request` to use [batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md) via the `batchRequests()` function. Example available at [examples/batching-requests.ts](examples/batching-requests.ts)
610610

611611
```ts
612-
import { batchRequests } from 'graphql-request'
613-
;(async function () {
614-
const endpoint = 'https://api.spacex.land/graphql/'
615-
616-
const query1 = /* GraphQL */ `
617-
query ($id: ID!) {
618-
capsule(id: $id) {
619-
id
620-
landings
621-
}
612+
import { batchRequests, gql } from 'graphql-request'
613+
614+
const endpoint = 'https://api.spacex.land/graphql/'
615+
616+
const query1 = gql`
617+
query ($id: ID!) {
618+
capsule(id: $id) {
619+
id
620+
landings
622621
}
623-
`
622+
}
623+
`
624624

625-
const query2 = /* GraphQL */ `
626-
{
627-
rockets(limit: 10) {
628-
active
629-
}
625+
const query2 = gql`
626+
{
627+
rockets(limit: 10) {
628+
active
630629
}
631-
`
630+
}
631+
`
632632

633-
const data = await batchRequests(endpoint, [
634-
{ document: query1, variables: { id: 'C105' } },
635-
{ document: query2 },
636-
])
637-
console.log(JSON.stringify(data, undefined, 2))
638-
})().catch((error) => console.error(error))
633+
const data = await batchRequests(endpoint, [
634+
{ document: query1, variables: { id: 'C105' } },
635+
{ document: query2 },
636+
])
637+
638+
console.log(JSON.stringify(data, undefined, 2))
639639
```
640640

641641
### Cancellation
@@ -800,7 +800,7 @@ Installing and configuring [GraphQL Code Generator](https://www.the-guild.dev/gr
800800
import request from 'graphql-request'
801801
import { graphql } from './gql/gql'
802802

803-
const getMovieQueryDocument = graphql(/* GraphQL */ `
803+
const getMovieQueryDocument = graphql(gql`
804804
query getMovie($title: String!) {
805805
Movie(title: $title) {
806806
releaseDate
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
import { GraphQLClient } from '../src/index.js'
2-
;(async () => {
3-
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
1+
import { gql, GraphQLClient } from '../src/index.js'
42

5-
const graphQLClient = new GraphQLClient(endpoint, {
6-
headers: {
7-
authorization: `Bearer MY_TOKEN`,
8-
},
9-
})
3+
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
104

11-
const query = /* GraphQL */ `
12-
{
13-
Movie(title: "Inception") {
14-
releaseDate
15-
actors {
16-
name
17-
}
5+
const graphQLClient = new GraphQLClient(endpoint, {
6+
headers: {
7+
authorization: `Bearer MY_TOKEN`,
8+
},
9+
})
10+
11+
const query = gql`
12+
{
13+
Movie(title: "Inception") {
14+
releaseDate
15+
actors {
16+
name
1817
}
1918
}
20-
`
21-
22-
interface TData {
23-
Movie: { releaseDate: string; actors: Array<{ name: string }> }
2419
}
20+
`
21+
22+
interface TData {
23+
Movie: { releaseDate: string; actors: Array<{ name: string }> }
24+
}
2525

26-
const data = await graphQLClient.request<TData>(query)
27-
console.log(JSON.stringify(data, undefined, 2))
28-
})().catch((error) => console.error(error))
26+
const data = await graphQLClient.request<TData>(query)
27+
console.log(JSON.stringify(data, undefined, 2))

examples/batching-requests.ts

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,56 @@
1-
import { batchRequests } from '../src/index.js'
2-
;(async function () {
3-
const endpoint = `https://api.spacex.land/graphql/`
4-
5-
const query1 = /* GraphQL */ `
6-
query ($id: ID!) {
7-
capsule(id: $id) {
8-
id
9-
landings
10-
}
11-
}
12-
`
13-
const variables1 = {
14-
id: `C105`,
15-
}
1+
import { batchRequests, gql } from '../src/index.js'
162

17-
interface TData1 {
18-
data: { capsule: { id: string; landings: number } }
19-
}
3+
const endpoint = `https://api.spacex.land/graphql/`
204

21-
const query2 = /* GraphQL */ `
22-
{
23-
rockets(limit: 10) {
24-
active
25-
}
5+
const query1 = gql`
6+
query ($id: ID!) {
7+
capsule(id: $id) {
8+
id
9+
landings
2610
}
27-
`
28-
29-
interface TData2 {
30-
data: { rockets: { active: boolean }[] }
3111
}
32-
33-
const query3 = /* GraphQL */ `
34-
query ($id: ID!) {
35-
core(id: $id) {
36-
id
37-
block
38-
original_launch
39-
}
12+
`
13+
const variables1 = {
14+
id: `C105`,
15+
}
16+
17+
interface TData1 {
18+
data: { capsule: { id: string; landings: number } }
19+
}
20+
21+
const query2 = gql`
22+
{
23+
rockets(limit: 10) {
24+
active
4025
}
41-
`
42-
43-
const variables3 = {
44-
id: `B1015`,
4526
}
46-
47-
interface TData3 {
48-
data: { core: { id: string; block: number; original_launch: string } }
27+
`
28+
29+
interface TData2 {
30+
data: { rockets: { active: boolean }[] }
31+
}
32+
33+
const query3 = gql`
34+
query ($id: ID!) {
35+
core(id: $id) {
36+
id
37+
block
38+
original_launch
39+
}
4940
}
50-
51-
const data = await batchRequests<[TData1, TData2, TData3]>(endpoint, [
52-
{ document: query1, variables: variables1 },
53-
{ document: query2 },
54-
{ document: query3, variables: variables3 },
55-
])
56-
console.log(JSON.stringify(data, undefined, 2))
57-
})().catch((error) => console.error(error))
41+
`
42+
43+
const variables3 = {
44+
id: `B1015`,
45+
}
46+
47+
interface TData3 {
48+
data: { core: { id: string; block: number; original_launch: string } }
49+
}
50+
51+
const data = await batchRequests<[TData1, TData2, TData3]>(endpoint, [
52+
{ document: query1, variables: variables1 },
53+
{ document: query2 },
54+
{ document: query3, variables: variables3 },
55+
])
56+
console.log(JSON.stringify(data, undefined, 2))

examples/cookie-support-for-node.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
;(global as any).fetch = require(`fetch-cookie/node-fetch`)(require(`node-fetch`))
22

3-
import { GraphQLClient } from '../src/index.js'
4-
;(async function () {
5-
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
3+
import { gql, GraphQLClient } from '../src/index.js'
64

7-
const graphQLClient = new GraphQLClient(endpoint, {
8-
headers: {
9-
authorization: `Bearer MY_TOKEN`,
10-
},
11-
})
5+
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
126

13-
const query = /* GraphQL */ `
14-
{
15-
Movie(title: "Inception") {
16-
releaseDate
17-
actors {
18-
name
19-
}
7+
const graphQLClient = new GraphQLClient(endpoint, {
8+
headers: {
9+
authorization: `Bearer MY_TOKEN`,
10+
},
11+
})
12+
13+
const query = gql`
14+
{
15+
Movie(title: "Inception") {
16+
releaseDate
17+
actors {
18+
name
2019
}
2120
}
22-
`
23-
24-
interface TData {
25-
Movie: { releaseDate: string; actors: Array<{ name: string }> }
2621
}
22+
`
23+
24+
interface TData {
25+
Movie: { releaseDate: string; actors: Array<{ name: string }> }
26+
}
2727

28-
const data = await graphQLClient.rawRequest<TData>(query)
29-
console.log(JSON.stringify(data, undefined, 2))
30-
})().catch((error) => console.error(error))
28+
const data = await graphQLClient.rawRequest<TData>(query)
29+
console.log(JSON.stringify(data, undefined, 2))

examples/custom-fetch.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import { GraphQLClient } from '../src/index.js'
1+
import { gql, GraphQLClient } from '../src/index.js'
22
import fetch from 'cross-fetch'
3-
;(async function () {
4-
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
53

6-
const graphQLClient = new GraphQLClient(endpoint, { fetch: fetch })
4+
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
75

8-
const query = /* GraphQL */ `
9-
{
10-
Movie(title: "Inception") {
11-
releaseDate
12-
actors {
13-
name
14-
}
6+
const graphQLClient = new GraphQLClient(endpoint, { fetch: fetch })
7+
8+
const query = gql`
9+
{
10+
Movie(title: "Inception") {
11+
releaseDate
12+
actors {
13+
name
1514
}
1615
}
17-
`
18-
19-
interface TData {
20-
Movie: { releaseDate: string; actors: Array<{ name: string }> }
2116
}
17+
`
18+
19+
interface TData {
20+
Movie: { releaseDate: string; actors: Array<{ name: string }> }
21+
}
2222

23-
const data = await graphQLClient.rawRequest<TData>(query)
24-
console.log(JSON.stringify(data, undefined, 2))
25-
})().catch((error) => console.error(error))
23+
const data = await graphQLClient.rawRequest<TData>(query)
24+
console.log(JSON.stringify(data, undefined, 2))

examples/error-handling.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import { request } from '../src/index.js'
2-
;(async function () {
3-
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
1+
import { gql, request } from '../src/index.js'
42

5-
const query = /* GraphQL */ `
6-
{
7-
Movie(title: "Inception") {
8-
releaseDate
9-
actors {
10-
fullname # "Cannot query field 'fullname' on type 'Actor'. Did you mean 'name'?"
11-
}
3+
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
4+
5+
const query = gql`
6+
{
7+
Movie(title: "Inception") {
8+
releaseDate
9+
actors {
10+
fullname # "Cannot query field 'fullname' on type 'Actor'. Did you mean 'name'?"
1211
}
1312
}
14-
`
15-
16-
interface TData {
17-
Movie: { releaseDate: string; actors: Array<{ name: string }> }
1813
}
14+
`
1915

20-
try {
21-
const data = await request<TData>(endpoint, query)
22-
console.log(JSON.stringify(data, undefined, 2))
23-
} catch (error) {
24-
console.error(JSON.stringify(error, undefined, 2))
25-
process.exit(1)
26-
}
27-
})().catch((error) => console.error(error))
16+
interface TData {
17+
Movie: { releaseDate: string; actors: Array<{ name: string }> }
18+
}
19+
20+
try {
21+
const data = await request<TData>(endpoint, query)
22+
console.log(JSON.stringify(data, undefined, 2))
23+
} catch (error) {
24+
console.error(JSON.stringify(error, undefined, 2))
25+
process.exit(1)
26+
}

0 commit comments

Comments
 (0)