@hyper-fetch/graphql 9.8.0 → 9.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,19 +48,19 @@ operations.
|
|
|
48
48
|
## 🚀 Quick Start
|
|
49
49
|
|
|
50
50
|
```bash
|
|
51
|
-
npm install @hyper-fetch/core @hyper-fetch/graphql
|
|
51
|
+
npm install @hyper-fetch/core @hyper-fetch/graphql graphql-tag
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
```ts
|
|
55
55
|
import { createClient } from "@hyper-fetch/core";
|
|
56
|
-
import {
|
|
56
|
+
import { GraphqlAdapter } from "@hyper-fetch/graphql";
|
|
57
57
|
|
|
58
|
-
const client = createClient({ url: "https://api.example.com/graphql" }).setAdapter(
|
|
58
|
+
const client = createClient({ url: "https://api.example.com/graphql" }).setAdapter(GraphqlAdapter());
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
## 📚 Documentation
|
|
62
62
|
|
|
63
|
-
- [GraphQL Adapter Docs](https://hyperfetch.bettertyped.com/docs/integrations/adapter-graphql)
|
|
63
|
+
- [GraphQL Adapter Docs](https://hyperfetch.bettertyped.com/docs/integrations/adapter-graphql/overview)
|
|
64
64
|
- [Getting Started](https://hyperfetch.bettertyped.com/docs/getting-started/quick-start)
|
|
65
65
|
- [API Reference](https://hyperfetch.bettertyped.com/api/)
|
|
66
66
|
|
|
@@ -68,32 +68,31 @@ const client = createClient({ url: "https://api.example.com/graphql" }).setAdapt
|
|
|
68
68
|
|
|
69
69
|
### Query
|
|
70
70
|
|
|
71
|
+
The query goes in `endpoint` as a `gql` document.
|
|
72
|
+
|
|
71
73
|
```ts
|
|
74
|
+
import gql from "graphql-tag";
|
|
75
|
+
|
|
72
76
|
interface UsersResponse {
|
|
73
77
|
users: { id: string; name: string; email: string }[];
|
|
74
78
|
}
|
|
75
79
|
|
|
76
|
-
const getUsers = client.createRequest<{ response: UsersResponse }>()({
|
|
77
|
-
endpoint:
|
|
78
|
-
|
|
80
|
+
const getUsers = client.createRequest<{ response: UsersResponse; payload: { limit: number } }>()({
|
|
81
|
+
endpoint: gql`
|
|
82
|
+
query GetUsers($limit: Int) {
|
|
83
|
+
users(limit: $limit) {
|
|
84
|
+
id
|
|
85
|
+
name
|
|
86
|
+
email
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
`,
|
|
79
90
|
});
|
|
80
91
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
query: `
|
|
84
|
-
query GetUsers($limit: Int) {
|
|
85
|
-
users(limit: $limit) {
|
|
86
|
-
id
|
|
87
|
-
name
|
|
88
|
-
email
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
`,
|
|
92
|
-
variables: { limit: 10 },
|
|
93
|
-
})
|
|
94
|
-
.send();
|
|
92
|
+
// The payload is sent as the GraphQL variables
|
|
93
|
+
const { data } = await getUsers.send({ payload: { limit: 10 } });
|
|
95
94
|
|
|
96
|
-
console.log(data
|
|
95
|
+
console.log(data?.users);
|
|
97
96
|
```
|
|
98
97
|
|
|
99
98
|
### Mutation
|
|
@@ -105,26 +104,23 @@ interface CreateUserResponse {
|
|
|
105
104
|
|
|
106
105
|
const createUser = client.createRequest<{
|
|
107
106
|
response: CreateUserResponse;
|
|
107
|
+
payload: { name: string; email: string };
|
|
108
108
|
}>()({
|
|
109
|
-
endpoint:
|
|
110
|
-
|
|
109
|
+
endpoint: gql`
|
|
110
|
+
mutation CreateUser($name: String!, $email: String!) {
|
|
111
|
+
createUser(name: $name, email: $email) {
|
|
112
|
+
id
|
|
113
|
+
name
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
`,
|
|
111
117
|
});
|
|
112
118
|
|
|
113
|
-
const { data } = await createUser
|
|
114
|
-
.
|
|
115
|
-
|
|
116
|
-
mutation CreateUser($name: String!, $email: String!) {
|
|
117
|
-
createUser(name: $name, email: $email) {
|
|
118
|
-
id
|
|
119
|
-
name
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
`,
|
|
123
|
-
variables: { name: "Jane", email: "jane@example.com" },
|
|
124
|
-
})
|
|
125
|
-
.send();
|
|
119
|
+
const { data } = await createUser.send({
|
|
120
|
+
payload: { name: "Jane", email: "jane@example.com" },
|
|
121
|
+
});
|
|
126
122
|
|
|
127
|
-
console.log("Created:", data
|
|
123
|
+
console.log("Created:", data?.createUser.name);
|
|
128
124
|
```
|
|
129
125
|
|
|
130
126
|
### With React hooks
|
|
@@ -133,11 +129,7 @@ console.log("Created:", data.createUser.name);
|
|
|
133
129
|
import { useFetch } from "@hyper-fetch/react";
|
|
134
130
|
|
|
135
131
|
const UserList = () => {
|
|
136
|
-
const { data, loading } = useFetch(
|
|
137
|
-
getUsers.setData({
|
|
138
|
-
query: `{ users { id name } }`,
|
|
139
|
-
}),
|
|
140
|
-
);
|
|
132
|
+
const { data, loading } = useFetch(getUsers.setPayload({ limit: 10 }));
|
|
141
133
|
|
|
142
134
|
if (loading) return <p>Loading...</p>;
|
|
143
135
|
return (
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/graphql",
|
|
3
|
-
"version": "9.8.
|
|
3
|
+
"version": "9.8.1",
|
|
4
4
|
"description": "Simple graphql adapter",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|