You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a custom hook that takes care of fetching your query and storing the result in the cache. It won't refetch the query unless `query` or `options.variables` changes.
241
242
242
-
-`query`: Your GraphQL query as a plain string
243
+
-`query`: Your GraphQL query as a plain string or DocumentNode
243
244
-`options`: Object with the following optional properties
244
245
-`variables`: Object e.g. `{ limit: 10 }`
245
246
-`operationName`: If your query has multiple operations, pass the name of the operation you wish to execute.
Use this when you don't want a query to automatically be fetched, or wish to call a query programmatically.
283
+
Use this when you don't want a query to automatically be fetched or wish to call a query programmatically.
283
284
284
285
**Usage**:
285
286
@@ -416,7 +417,7 @@ To use subscription you can use either [subscriptions-transport-ws](https://gith
416
417
417
418
`useSubscription(operation, callback)`
418
419
419
-
-`operation`: Object - The GraphQL operation the following properties:
420
+
-`operation`: Object - The GraphQL operation has the following properties:
420
421
-`query`: String (required) - the GraphQL query
421
422
-`variables`: Object (optional) - Any variables the query might need
422
423
-`operationName`: String (optional) - If your query has multiple operations, you can choose which operation you want to call.
@@ -425,7 +426,7 @@ To use subscription you can use either [subscriptions-transport-ws](https://gith
425
426
426
427
**Usage**:
427
428
428
-
First follow the [quick start guide](#Quick-Start) to create the client and povider. Then we need to update the config for our `GraphQLClient` passing in the `subscriptionClient`:
429
+
First, follow the [quick start guide](#Quick-Start) to create the client and provider. Then we need to update the config for our `GraphQLClient` passing in the `subscriptionClient`:
429
430
430
431
```js
431
432
import { GraphQLClient } from'graphql-hooks'
@@ -1255,6 +1256,91 @@ it('shows "No posts" if 0 posts are returned', async () => {
1255
1256
})
1256
1257
```
1257
1258
1259
+
## Typescript Support
1260
+
1261
+
All client methods support the ability to provide type information for response data, query variables and error responses.
`graphql-hooks` also supports `TypedDocumentNode`. This allows you to use GraphQL code gen to create `DocumentNode`s for your GQL queries and receive full type support.
// data will be typed as User objects with id, name properties
1322
+
const { loading, error, data } =useQuery(HOMEPAGE_QUERY, {
1323
+
variables: {
1324
+
limit: 10
1325
+
}
1326
+
})
1327
+
1328
+
if (loading) return'Loading...'
1329
+
if (error) return'Something Bad Happened'
1330
+
1331
+
return (
1332
+
<ul>
1333
+
{data.users.map(({ id, name }) => (
1334
+
<likey={id}>{name}</li>
1335
+
))}
1336
+
</ul>
1337
+
)
1338
+
}
1339
+
```
1340
+
1341
+
Full details of the features of `TypedDocumentNode` and GraphQL Code Generator can be found [here](https://the-guild.dev/graphql/codegen). Full examples of this implementation are in the examples folder.
1342
+
1343
+
1258
1344
## Other
1259
1345
1260
1346
### Request interceptors
@@ -1316,52 +1402,38 @@ function AbortControllerExample() {
1316
1402
}
1317
1403
```
1318
1404
1319
-
## Typescript Support
1405
+
### GraphQL Document Support
1320
1406
1321
-
All client methods support the ability to provide type information for response data, query variables and error responses.
1407
+
As well as supporting input of your queries as strings, this library also supports using a `DocumentNode`. Document nodes can be generated using a code-generation tool such as [GraphQL codegen](https://the-guild.dev/graphql/codegen) which will provide typing information for your queries based on your GraphQL schema (see the typescript example). If you don't want to use a code-generation library you can use `graphql-tag` to generate a `DocumentNode`.
0 commit comments