Skip to content

Commit b360722

Browse files
authored
Merge pull request #2 from ArtCodeStudio/upstream-4-1-0
Merge upstream v4.1.0
2 parents e646652 + 67d9a7f commit b360722

25 files changed

+915
-318
lines changed

.github/workflows/dev.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: development
2+
3+
on:
4+
push:
5+
pull_request:
6+
branches:
7+
- "*"
8+
9+
jobs:
10+
examples:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [ubuntu-latest, windows-latest, macos-latest]
15+
deno:
16+
[1.x, canary]
17+
18+
steps:
19+
- uses: actions/checkout@v3
20+
21+
- name: Setup Deno
22+
uses: denoland/setup-deno@v1
23+
with:
24+
deno-version: ${{ matrix.deno }}
25+
26+
- name: examples/authentication-via-http-header.ts
27+
run: deno run --allow-net examples/authentication-via-http-header.ts
28+
29+
- name: examples/batching-requests.ts
30+
run: deno run --allow-net examples/batching-requests.ts
31+
32+
- name: examples/custom-fetch.ts
33+
run: deno run --allow-net examples/custom-fetch.ts
34+
35+
- name: examples/error-handling.ts
36+
run: deno run --allow-net examples/error-handling.ts
37+
38+
- name: examples/passing-custom-header-per-request.ts
39+
run: deno run --allow-net examples/passing-custom-header-per-request.ts
40+
41+
- name: examples/passing-more-options-to-fetch.ts
42+
run: deno run --allow-net examples/passing-more-options-to-fetch.ts
43+
44+
- name: examples/receiving-a-raw-response.ts
45+
run: deno run --allow-net examples/receiving-a-raw-response.ts
46+
47+
- name: examples/using-variables.ts
48+
run: deno run --allow-net examples/using-variables.ts

.github/workflows/pr.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

.github/workflows/trunk.yml

Lines changed: 0 additions & 66 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
dist
44
.DS_Store
55
*.log
6+
coverage

README.md

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@ Now it also supports Deno 🦕
88
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
99
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1010

11-
- [Features](#features)
12-
- [Quickstart](#quickstart)
13-
- [Usage](#usage)
14-
- [Community](#community)
15-
- [GraphQL Code Generator's GraphQL-Request TypeScript Plugin](#graphql-code-generators-graphql-request-typescript-plugin)
16-
- [Examples](#examples)
17-
- [Authentication via HTTP header](#authentication-via-http-header)
18-
- [Incrementally setting headers](#incrementally-setting-headers)
19-
- [Passing Headers in each request](#passing-headers-in-each-request)
20-
- [Passing more options to `fetch`](#passing-more-options-to-fetch)
21-
- [Using GraphQL Document variables](#using-graphql-document-variables)
22-
- [GraphQL Mutations](#graphql-mutations)
23-
- [Error handling](#error-handling)
24-
- [Using a custom `fetch` method](#using-a-custom-fetch-method)
25-
- [Receiving a raw response](#receiving-a-raw-response)
26-
- [File Upload](#file-upload)
27-
- [Browser](#browser)
28-
- [Node](#node)
29-
- [Deno](#deno)
30-
- [Batching](#batching)
31-
- [FAQ](#faq)
32-
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
33-
- [Do I need to wrap my GraphQL documents inside the `gql` template exported by `graphql-request`?](#do-i-need-to-wrap-my-graphql-documents-inside-the-gql-template-exported-by-graphql-request)
34-
- [What's the difference between `graphql-request`, Apollo and Relay?](#whats-the-difference-between-graphql-request-apollo-and-relay)
11+
- [graphql-request](#graphql-request)
12+
- [Features](#features)
13+
- [Quickstart](#quickstart)
14+
- [Usage](#usage)
15+
- [Community](#community)
16+
- [GraphQL Code Generator's GraphQL-Request TypeScript Plugin](#graphql-code-generators-graphql-request-typescript-plugin)
17+
- [Examples](#examples)
18+
- [Authentication via HTTP header](#authentication-via-http-header)
19+
- [Incrementally setting headers](#incrementally-setting-headers)
20+
- [Set endpoint](#set-endpoint)
21+
- [passing-headers-in-each-request](#passing-headers-in-each-request)
22+
- [Passing more options to `fetch`](#passing-more-options-to-fetch)
23+
- [Using GraphQL Document variables](#using-graphql-document-variables)
24+
- [GraphQL Mutations](#graphql-mutations)
25+
- [Error handling](#error-handling)
26+
- [Using a custom `fetch` method](#using-a-custom-fetch-method)
27+
- [Receiving a raw response](#receiving-a-raw-response)
28+
- [File Upload](#file-upload)
29+
- [Browser](#browser)
30+
- [Node](#node)
31+
- [Deno](#deno)
32+
- [Batching](#batching)
33+
- [Cancellation](#cancellation)
34+
- [FAQ](#faq)
35+
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
36+
- [Do I need to wrap my GraphQL documents inside the `gql` template exported by `graphql-request`?](#do-i-need-to-wrap-my-graphql-documents-inside-the-gql-template-exported-by-graphql-request)
37+
- [What's the difference between `graphql-request`, Apollo and Relay?](#whats-the-difference-between-graphql-request-apollo-and-relay)
3538

3639
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3740

@@ -549,19 +552,50 @@ try {
549552
deno run --allow-net examples/batching-requests.ts
550553
```
551554
555+
### Cancellation
556+
557+
It is possible to cancel a request using an `AbortController` signal.
558+
559+
You can define the `signal` in the `GraphQLClient` constructor:
560+
561+
```ts
562+
const abortController = new AbortController();
563+
564+
const client = new GraphQLClient(endpoint, { signal: abortController.signal });
565+
client.request(query);
566+
567+
abortController.abort();
568+
```
569+
570+
You can also set the signal per request (this will override an existing
571+
GraphQLClient signal):
572+
573+
```ts
574+
const abortController = new AbortController();
575+
576+
const client = new GraphQLClient(endpoint);
577+
client.request({ document: query, signal: abortController.signal });
578+
579+
abortController.abort();
580+
```
581+
582+
In Node environment, `AbortController` is supported since version v14.17.0. For
583+
Node.js v12 you can use
584+
[abort-controller](https://github.com/mysticatea/abort-controller) polyfill.
585+
586+
```
587+
import 'abort-controller/polyfill'
588+
589+
const abortController = new AbortController()
590+
```
591+
552592
## FAQ
553593
554594
#### Why do I have to install `graphql`?
555595
556-
`graphql-request` uses a TypeScript type from the `graphql` package such that if
557-
you are using TypeScript to build your project and you are using
558-
`graphql-request` but don't have `graphql` installed TypeScript build will fail.
559-
Details
560-
[here](https://github.com/prisma-labs/graphql-request/pull/183#discussion_r464453076).
561-
If you are a JS user then you do not technically need to install `graphql`.
562-
However if you use an IDE that picks up TS types even for JS (like VSCode) then
563-
its still in your interest to install `graphql` so that you can benefit from
564-
enhanced type safety during development.
596+
`graphql-request` uses methods exposed by the `graphql` package to handle some
597+
internal logic. On top of that, for TypeScript users, some types are used from
598+
the `graphql` package to provide better typings.
565599
566600
#### Do I need to wrap my GraphQL documents inside the `gql` template exported by `graphql-request`?
567601
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
import { GraphQLClient } from '../src/index.ts'
2-
;(async function () {
3-
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
2+
; (async function () {
3+
const endpoint = 'https://fruits-api.netlify.app/graphql'
44

5-
const graphQLClient = new GraphQLClient(endpoint, {
6-
headers: {
7-
authorization: 'Bearer MY_TOKEN',
8-
},
9-
})
5+
const graphQLClient = new GraphQLClient(endpoint, {
6+
headers: {
7+
authorization: 'Bearer MY_TOKEN',
8+
},
9+
})
1010

11-
const query = /* GraphQL */ `
11+
const query = /* GraphQL */ `
1212
{
13-
Movie(title: "Inception") {
14-
releaseDate
15-
actors {
16-
name
17-
}
13+
filterFruitsFam(family: "Rosaceae") {
14+
id
15+
tree_name
16+
fruit_name
17+
family
1818
}
1919
}
2020
`
2121

22-
interface TData {
23-
Movie: { releaseDate: string; actors: Array<{ name: string }> }
24-
}
22+
interface Fruit {
23+
"id": string;
24+
"tree_name": string;
25+
"fruit_name": string;
26+
"family": string;
27+
}
28+
29+
interface TData {
30+
filterFruitsFam: Fruit[]
31+
}
2532

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

examples/custom-fetch.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ import { GraphQLClient } from '../src/index.ts'
22
import { wrapFetch } from 'https://deno.land/x/fetch_goody@v5.0.0/mod.ts';
33

44
const start = async () => {
5-
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
5+
const endpoint = 'https://fruits-api.netlify.app/graphql'
66

7-
const graphQLClient = new GraphQLClient(endpoint, { fetch: wrapFetch()})
7+
const graphQLClient = new GraphQLClient(endpoint, { fetch: wrapFetch() })
88

99
const query = /* GraphQL */ `
1010
{
11-
Movie(title: "Inception") {
12-
releaseDate
13-
actors {
14-
name
15-
}
11+
filterFruitsFam(family: "Rosaceae") {
12+
id
13+
tree_name
14+
fruit_name
15+
family
1616
}
17-
}
18-
`
17+
}`
18+
19+
interface Fruit {
20+
"id": string;
21+
"tree_name": string;
22+
"fruit_name": string;
23+
"family": string;
24+
}
1925

2026
interface TData {
21-
Movie: { releaseDate: string; actors: Array<{ name: string }> }
27+
filterFruitsFam: Fruit[]
2228
}
2329

2430
const data = await graphQLClient.rawRequest<TData>(query)

0 commit comments

Comments
 (0)