Skip to content

Commit 6b3396b

Browse files
authored
chore: normalize docs (dedupe) (#507)
1 parent 3a38f48 commit 6b3396b

23 files changed

+429
-668
lines changed

README.md

Lines changed: 70 additions & 652 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* `graphql-request@^5` supports `TypedDocumentNode`, the typed counterpart of `graphql`'s `DocumentNode`.
3+
*
4+
* Installing and configuring GraphQL Code Generator requires a few steps in order to get end-to-end typed GraphQL operations using the provided `graphql()`.
5+
*
6+
* The complete example is available in the GraphQL Code Generator repository:
7+
*
8+
* @see https://github.com/dotansimha/graphql-code-generator/tree/master/examples/front-end/react/graphql-request
9+
*
10+
* Visit GraphQL Code Generator's dedicated guide to get started:
11+
*
12+
* @see https://www.the-guild.dev/graphql/codegen/docs/guides/react-vue.
13+
*/
14+
15+
import request, { gql } from '../src/index.js'
16+
// @ts-expect-error todo make this actually work
17+
import { graphql } from './gql/gql'
18+
19+
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
20+
21+
// todo fixme
22+
// eslint-disable-next-line
23+
const document = graphql(gql`
24+
query getMovie($title: String!) {
25+
Movie(title: $title) {
26+
releaseDate
27+
actors {
28+
name
29+
}
30+
}
31+
}
32+
`)
33+
34+
// Variables are typed!
35+
const data = await request(endpoint, document, { title: `Inception` })
36+
37+
// @ts-expect-error todo make this actually work
38+
console.log(data.Movie) // typed!

examples/custom-fetch.ts renamed to examples/configuration-fetch-custom-function.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { gql, GraphQLClient } from '../src/index.js'
2-
import fetch from 'cross-fetch'
2+
import crossFetch from 'cross-fetch'
3+
import fetchCookie from 'fetch-cookie'
34

45
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
56

6-
const graphQLClient = new GraphQLClient(endpoint, { fetch: fetch })
7+
/**
8+
* Fetch with a cookie jar scoped to the client object.
9+
*/
10+
const fetch = fetchCookie(crossFetch)
11+
12+
const graphQLClient = new GraphQLClient(endpoint, { fetch })
713

814
const query = gql`
915
{
@@ -21,4 +27,4 @@ interface TData {
2127
}
2228

2329
const data = await graphQLClient.rawRequest<TData>(query)
24-
console.log(JSON.stringify(data, undefined, 2))
30+
console.log(data)

examples/passing-more-options-to-fetch.ts renamed to examples/configuration-fetch-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ interface TData {
2323
}
2424

2525
const data = await graphQLClient.request<TData>(query)
26-
console.log(JSON.stringify(data, undefined, 2))
26+
console.log(data)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* If you want to change the endpoint after the GraphQLClient has been initialized, you can use the `setEndpoint()` function.
3+
*/
4+
5+
import { GraphQLClient } from '../src/index.js'
6+
7+
const client = new GraphQLClient(`https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`)
8+
9+
client.setEndpoint(`https://api.graph.cool/simple/v2/cixos23120m0n0173veiiwrjr`)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* If you want to set headers after the GraphQLClient has been initialized, you can use the `setHeader()` or `setHeaders()` functions.
3+
*/
4+
5+
import { GraphQLClient } from '../src/index.js'
6+
7+
const client = new GraphQLClient(`https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`)
8+
9+
// Set a single header
10+
client.setHeader(`authorization`, `Bearer MY_TOKEN`)
11+
12+
// Override all existing headers
13+
client.setHeaders({
14+
authorization: `Bearer MY_TOKEN`,
15+
'x-another-header': `header_value`,
16+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* If you want to use non-standard JSON types, you can use your own JSON serializer to replace `JSON.parse`/`JSON.stringify` used by the `GraphQLClient`.
3+
* An original use case for this feature is `BigInt` support:
4+
*/
5+
6+
import { gql, GraphQLClient } from '../src/index.js'
7+
import JSONbig from 'json-bigint'
8+
9+
const jsonSerializer = JSONbig({ useNativeBigInt: true })
10+
const graphQLClient = new GraphQLClient(`https://some-api`, { jsonSerializer })
11+
const data = await graphQLClient.request<{ someBigInt: bigint }>(
12+
gql`
13+
{
14+
someBigInt
15+
}
16+
`
17+
)
18+
console.log(typeof data.someBigInt) // if >MAX_SAFE_INTEGER then 'bigint' else 'number'

examples/batching-requests.ts renamed to examples/graphql-batching-requests.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* It is possible with `graphql-request` to use batching via the `batchRequests()` function.
3+
* @see https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md
4+
*/
15
import { batchRequests, gql } from '../src/index.js'
26

37
const endpoint = `https://api.spacex.land/graphql/`
@@ -53,4 +57,4 @@ const data = await batchRequests<[TData1, TData2, TData3]>(endpoint, [
5357
{ document: query2 },
5458
{ document: query3, variables: variables3 },
5559
])
56-
console.log(JSON.stringify(data, undefined, 2))
60+
console.log(data)

examples/using-variables.ts renamed to examples/graphql-document-variables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ interface TData {
2222
}
2323

2424
const data = await request<TData>(endpoint, query, variables)
25-
console.log(JSON.stringify(data, undefined, 2))
25+
console.log(data)

examples/graphql-mutations.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { gql, GraphQLClient } from '../src/index.js'
2+
3+
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
4+
5+
const graphQLClient = new GraphQLClient(endpoint, {
6+
headers: {
7+
authorization: `Bearer MY_TOKEN`,
8+
},
9+
})
10+
11+
const mutation = gql`
12+
mutation AddMovie($title: String!, $releaseDate: Int!) {
13+
insert_movies_one(object: { title: $title, releaseDate: $releaseDate }) {
14+
title
15+
releaseDate
16+
}
17+
}
18+
`
19+
20+
const variables = {
21+
title: `Inception`,
22+
releaseDate: 2010,
23+
}
24+
25+
const data = await graphQLClient.request(mutation, variables)
26+
console.log(data)

0 commit comments

Comments
 (0)