-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: normalize docs (dedupe) (#507)
- Loading branch information
1 parent
3a38f48
commit 6b3396b
Showing
23 changed files
with
429 additions
and
668 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* `graphql-request@^5` supports `TypedDocumentNode`, the typed counterpart of `graphql`'s `DocumentNode`. | ||
* | ||
* Installing and configuring GraphQL Code Generator requires a few steps in order to get end-to-end typed GraphQL operations using the provided `graphql()`. | ||
* | ||
* The complete example is available in the GraphQL Code Generator repository: | ||
* | ||
* @see https://github.com/dotansimha/graphql-code-generator/tree/master/examples/front-end/react/graphql-request | ||
* | ||
* Visit GraphQL Code Generator's dedicated guide to get started: | ||
* | ||
* @see https://www.the-guild.dev/graphql/codegen/docs/guides/react-vue. | ||
*/ | ||
|
||
import request, { gql } from '../src/index.js' | ||
// @ts-expect-error todo make this actually work | ||
import { graphql } from './gql/gql' | ||
|
||
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` | ||
|
||
// todo fixme | ||
// eslint-disable-next-line | ||
const document = graphql(gql` | ||
query getMovie($title: String!) { | ||
Movie(title: $title) { | ||
releaseDate | ||
actors { | ||
name | ||
} | ||
} | ||
} | ||
`) | ||
|
||
// Variables are typed! | ||
const data = await request(endpoint, document, { title: `Inception` }) | ||
|
||
// @ts-expect-error todo make this actually work | ||
console.log(data.Movie) // typed! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* If you want to change the endpoint after the GraphQLClient has been initialized, you can use the `setEndpoint()` function. | ||
*/ | ||
|
||
import { GraphQLClient } from '../src/index.js' | ||
|
||
const client = new GraphQLClient(`https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`) | ||
|
||
client.setEndpoint(`https://api.graph.cool/simple/v2/cixos23120m0n0173veiiwrjr`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* If you want to set headers after the GraphQLClient has been initialized, you can use the `setHeader()` or `setHeaders()` functions. | ||
*/ | ||
|
||
import { GraphQLClient } from '../src/index.js' | ||
|
||
const client = new GraphQLClient(`https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`) | ||
|
||
// Set a single header | ||
client.setHeader(`authorization`, `Bearer MY_TOKEN`) | ||
|
||
// Override all existing headers | ||
client.setHeaders({ | ||
authorization: `Bearer MY_TOKEN`, | ||
'x-another-header': `header_value`, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* 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`. | ||
* An original use case for this feature is `BigInt` support: | ||
*/ | ||
|
||
import { gql, GraphQLClient } from '../src/index.js' | ||
import JSONbig from 'json-bigint' | ||
|
||
const jsonSerializer = JSONbig({ useNativeBigInt: true }) | ||
const graphQLClient = new GraphQLClient(`https://some-api`, { jsonSerializer }) | ||
const data = await graphQLClient.request<{ someBigInt: bigint }>( | ||
gql` | ||
{ | ||
someBigInt | ||
} | ||
` | ||
) | ||
console.log(typeof data.someBigInt) // if >MAX_SAFE_INTEGER then 'bigint' else 'number' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { gql, GraphQLClient } from '../src/index.js' | ||
|
||
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` | ||
|
||
const graphQLClient = new GraphQLClient(endpoint, { | ||
headers: { | ||
authorization: `Bearer MY_TOKEN`, | ||
}, | ||
}) | ||
|
||
const mutation = gql` | ||
mutation AddMovie($title: String!, $releaseDate: Int!) { | ||
insert_movies_one(object: { title: $title, releaseDate: $releaseDate }) { | ||
title | ||
releaseDate | ||
} | ||
} | ||
` | ||
|
||
const variables = { | ||
title: `Inception`, | ||
releaseDate: 2010, | ||
} | ||
|
||
const data = await graphQLClient.request(mutation, variables) | ||
console.log(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* eslint-disable */ | ||
|
||
/** | ||
* It's possible to use a middleware to pre-process any request or handle raw response. | ||
*/ | ||
|
||
import { GraphQLClient } from '../src/index.js' | ||
import type { RequestMiddleware } from '../src/types.js' | ||
|
||
const endpoint = `https://api.spacex.land/graphql/` | ||
|
||
const getAccessToken = () => Promise.resolve(`some special token here`) | ||
|
||
{ | ||
/** | ||
* Request middleware example (set actual auth token to each request): | ||
*/ | ||
|
||
const requestMiddleware: RequestMiddleware = async (request) => { | ||
const token = await getAccessToken() | ||
return { | ||
...request, | ||
headers: { ...request.headers, 'x-auth-token': token }, | ||
} | ||
} | ||
|
||
const _client = new GraphQLClient(endpoint, { requestMiddleware }) | ||
} | ||
{ | ||
/** | ||
* It's also possible to use an async function as a request middleware. The resolved data will be passed to the request: | ||
*/ | ||
|
||
const requestMiddleware: RequestMiddleware = async (request) => { | ||
const token = await getAccessToken() | ||
return { | ||
...request, | ||
headers: { ...request.headers, 'x-auth-token': token }, | ||
} | ||
} | ||
|
||
const _client = new GraphQLClient(endpoint, { requestMiddleware }) | ||
} | ||
{ | ||
/** | ||
* Response middleware example (log request trace id if error caused): | ||
*/ | ||
|
||
// @ts-expect-error TODO export a response middleware type | ||
const responseMiddleware = (response: Response<unknown>) => { | ||
if (response.errors) { | ||
const traceId = response.headers.get(`x-b3-trace-id`) || `unknown` | ||
console.error( | ||
`[${traceId}] Request error: | ||
status ${response.status} | ||
details: ${response.errors}` | ||
) | ||
} | ||
} | ||
|
||
const _client = new GraphQLClient(endpoint, { responseMiddleware }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* eslint-disable */ | ||
|
||
const { request, gql } = require(`graphql-request`) | ||
|
||
main() | ||
|
||
async function main() { | ||
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` | ||
|
||
const query = gql` | ||
{ | ||
Movie(title: "Inception") { | ||
releaseDate | ||
actors { | ||
name | ||
} | ||
} | ||
} | ||
` | ||
|
||
const data = await request(endpoint, query) | ||
console.log(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* It is possible to cancel a request using an `AbortController` signal. | ||
*/ | ||
|
||
import { gql, GraphQLClient } from '../src/index.js' | ||
|
||
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr` | ||
|
||
/** | ||
* You can define the `signal` in the `GraphQLClient` constructor: | ||
*/ | ||
|
||
{ | ||
const abortController = new AbortController() | ||
|
||
const client = new GraphQLClient(endpoint, { | ||
signal: abortController.signal, | ||
}) | ||
|
||
// todo | ||
// eslint-disable-next-line | ||
client.request(gql` | ||
{ | ||
Movie(title: "Inception") { | ||
releaseDate | ||
actors { | ||
name | ||
} | ||
} | ||
} | ||
`) | ||
|
||
abortController.abort() | ||
} | ||
|
||
/** | ||
* You can also set the signal per request (this will override an existing GraphQLClient signal): | ||
*/ | ||
|
||
{ | ||
const abortController = new AbortController() | ||
|
||
const client = new GraphQLClient(endpoint) | ||
const document = gql` | ||
{ | ||
Movie(title: "Inception") { | ||
releaseDate | ||
actors { | ||
name | ||
} | ||
} | ||
} | ||
` | ||
const _requesting = client.request({ | ||
document, | ||
signal: abortController.signal, | ||
}) | ||
abortController.abort() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* It's possible to recalculate the global client headers dynamically before each request. | ||
* To do that, pass a function that returns the headers to the `headers` property when creating a new `GraphQLClient`. | ||
*/ | ||
|
||
import { gql, GraphQLClient } from '../src/index.js' | ||
|
||
const client = new GraphQLClient(`https://some-api`, { | ||
headers: () => ({ 'X-Sent-At-Time': Date.now().toString() }), | ||
}) | ||
|
||
const query = gql` | ||
query getCars { | ||
cars { | ||
name | ||
} | ||
} | ||
` | ||
// Function saved in the client runs and calculates fresh headers before each request | ||
const data = await client.request(query) | ||
console.log(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.