Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/nearform/graphql-hooks

🎣 Minimal hooks-first GraphQL client
https://github.com/nearform/graphql-hooks

graphql graphql-client hacktoberfest hooks react react-hooks

Last synced: 25 days ago
JSON representation

🎣 Minimal hooks-first GraphQL client

Lists

README

        

# graphql-hooks

[![ci](https://github.com/nearform/graphql-hooks/workflows/ci/badge.svg)](https://github.com/nearform/graphql-hooks/actions?query=workflow%3Aci)
[![Coverage Status](https://coveralls.io/repos/github/nearform/graphql-hooks/badge.svg?branch=master)](https://coveralls.io/github/nearform/graphql-hooks?branch=master)
[![bundlephobia](https://img.shields.io/bundlephobia/minzip/graphql-hooks.svg?style=flat&label=size)](https://bundlephobia.com/result?p=graphql-hooks)
[![npm](https://img.shields.io/npm/v/graphql-hooks.svg?color=brightgreen)](https://www.npmjs.com/package/graphql-hooks)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)

🎣 Minimal hooks-first GraphQL client.

## Features

- πŸ₯‡ First-class hooks API
- βš–οΈ _Tiny_ bundle: only 7.6kB (2.8 gzipped)
- πŸ“„ Full SSR support: see [graphql-hooks-ssr](packages/graphql-hooks-ssr)
- πŸ”Œ Plugin Caching: see [graphql-hooks-memcache](packages/graphql-hooks-memcache)
- πŸ”₯ No more render props hell
- ⏳ Handle loading and error states with ease

## Install

`npm install graphql-hooks`

or

`yarn add graphql-hooks`

## Support

- Node LTS
- Browsers [`> 1%, not dead`](https://browserl.ist/?q=%3E+1%25%2C+not+dead)

Consider polyfilling:

- [`FormData`](https://developer.mozilla.org/docs/Web/API/FormData)
- [`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)
- [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API). NOTE: A custom implementation can also be provided instead of polyfilling, [see `GraphQLClient`](#GraphQLClient)

## Quick Start

First you'll need to create a client and wrap your app with the provider:

```js
import { GraphQLClient, ClientContext } from 'graphql-hooks'

const client = new GraphQLClient({
url: '/graphql'
})

function App() {
return (

{/* children */}

)
}
```

Now in your child components you can make use of `useQuery`

```js
import { useQuery } from 'graphql-hooks'

const HOMEPAGE_QUERY = `query HomePage($limit: Int) {
users(limit: $limit) {
id
name
}
}`

function MyComponent() {
const { loading, error, data } = useQuery(HOMEPAGE_QUERY, {
variables: {
limit: 10
}
})

if (loading) return 'Loading...'
if (error) return 'Something Bad Happened'

return (


    {data.users.map(({ id, name }) => (
  • {name}

  • ))}

)
}
```

## Why `graphql-hooks`?

The first thing you may ask when seeing `graphql-hooks` is "Why not use Apollo hooks?".
It's the comparison most will make. In fact, there's an [article comparing the two](https://blog.logrocket.com/comparing-hooks-libraries-for-graphql/) over on LogRocket.

We believe `graphql-hooks` is a great choice as a hooks-first GraphQL client due to its concise API and package size.

In terms of performance, this is more of a grey area as we have no official benchmarks yet.

If you need a client that offers more customization such as advanced cache configuration, then `apollo-hooks` may work out to be a good choice for your project if bundle size is not an issue.

| Pros | Cons |
| --------------------------- | ------------------------------------- |
| Small in size | Less "advanced" caching configuration |
| Concise API |
| Quick to get up and running |

# Table of Contents

- API
- [GraphQLClient](#GraphQLClient)
- [ClientContext](#ClientContext)
- [useQuery](#useQuery)
- [useManualQuery](#useManualQuery)
- [useMutation](#useMutation)
- [useSubscription](#useSubscription)
- Guides
- [SSR](#SSR)
- [Pagination](#Pagination)
- [File uploads](#File-uploads)
- [HTTP Get Support](#HTTP-Get-support)
- [Authentication](#Authentication)
- [Fragments](#Fragments)
- [Migrating from Apollo](#Migrating-from-Apollo)
- [ApolloClient ➑️ GraphQLClient](#apolloclient-️-graphqlclient)
- [ApolloProvider ➑️ ClientContext.Provider](#apolloprovider-️-clientcontextprovider)
- [Query Component ➑️ useQuery](#query-component-️-usequery)
- [Mutation Component ➑️ useMutation](#mutation-component-️-usemutation)
- [Testing and mocking](#testing-and-mocking)
- [Typescript Support](#typescript-support)
- [Other]
- [Request interceptors](#request-interceptors)
- [AbortController](#abortController)
- [GraphQL document support](#graphql-document-support)

## API

## `GraphQLClient`

**Usage**:

```js
import { GraphQLClient } from 'graphql-hooks'
const client = new GraphQLClient(config)
```

**`config`**: Object containing configuration properties

- `url`: The URL of your GraphQL **HTTP** server. If not specified, you must enable `fullWsTransport` and provide a valid `subscriptionClient`; otherwise is **required**.
- `fullWsTransport`: Boolean - set to `true` if you want to use `subscriptionClient` to also send query and mutations via WebSocket; defaults to `false`
- `ssrMode`: Boolean - set to `true` when using on the server for server-side rendering; defaults to `false`
- `useGETForQueries`: Boolean - set to `true` to use HTTP GET method for all queries; defaults to false. See [HTTP Get Support](#HTTP-Get-support) for more info
- `subscriptionClient`: The **WebSocket** client configuration. Accepts either an instance of `SubscriptionClient` from [subscriptions-transport-ws](https://github.com/apollographql/subscriptions-transport-ws) or `Client` from [graphql-ws](https://github.com/enisdenjo/graphql-ws). A factory function is also accepted e.g. to avoid the creation of the client in SSR environments.
- `cache` (**Required** if `ssrMode` is `true`, otherwise optional): Object with the following methods:
- `cache.get(key)`
- `cache.set(key, data)`
- `cache.delete(key)`
- `cache.clear()`
- `cache.keys()`
- `getInitialState()`
- See [graphql-hooks-memcache](packages/graphql-hooks-memcache) as a reference implementation
- `fetch(url, options)`: Fetch implementation - defaults to the global `fetch` API. Check [Request interceptors](#request-interceptors) for more details how to manage `fetch`.
- `FormData`: FormData implementation - defaults to the global `FormData` API. Polyfill this in a node.js environment. See [file-uploads-nodejs](#file-uploads-nodejs) for more info.
- `fetchOptions`: See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) for info on what options can be passed
- `headers`: Object, e.g. `{ 'My-Header': 'hello' }`
- `logErrors`: Boolean - defaults to `true`
- `middleware`: Accepts an array of middleware functions, default: none, see more in [middlewares readme](packages/graphql-hooks/src/middlewares/README.md)
- `onError({ operation, result })`: Custom error handler
- `operation`: Object with `query`, `variables` and `operationName`
- `result`: Object containing `data` and `error` object that contains `fetchError`, `httpError` and `graphqlErrors`

### `client` methods

- `client.setHeader(key, value)`: Updates `client.headers` adding the new header to the existing headers
- `client.setHeaders(headers)`: Replaces `client.headers`
- `client.removeHeader(key)`: Updates `client.headers` removing the header if it exists
- `client.logErrorResult({ operation, result })`: Default error logger; useful if you'd like to use it inside your custom `onError` handler
- `request(operation, options)`: Make a request to your GraphQL server; returning a Promise
- `operation`: Object with `query`, `variables` and `operationName`
- `options.fetchOptionsOverrides`: Object containing additional fetch options to be added to the default ones passed to `new GraphQLClient(config)`
- `options.responseReducer`: Reducer function to pick values from the original Fetch Response object. Values are merged to the `request` response under the `data` key. Example usage: `{responseReducer: (data, response) => ({...data, myKey: response.headers.get('content-length)})`
- `client.invalidateQuery(query)`: Will delete the older cache, re-fetch the new data using the same query, and store it in the cache as a new value
- `query`: The GraphQL query as a plain string to be re-fetched, or an Operation object (with `query`, `variables` and `operationName`)
- `client.setQueryData(query, (oldState) => [...oldState, newState]])`: Will override the older cache state with the new one provided by the function return
- `query`: The GraphQL query as a plain string, or an Operation object (with `query`, `variables` and `operationName`)
- `(oldState) => [...oldState, newState]]`: The callback function with returns will be the new state stored in the cache.
- `oldState`: The old value stored in the cache

## `ClientContext`

`ClientContext` is the result of `React.createContext()` - meaning it can be used directly with React's new context API:

**Example**:

```js
import { ClientContext } from 'graphql-hooks'

function App() {
return (

{/* children can now consume the client context */}

)
}
```

To access the `GraphQLClient` instance, call `React.useContext(ClientContext)`:

```js
import React, { useContext } from 'react'
import { ClientContext } from 'graphql-hooks'

function MyComponent() {
const client = useContext(ClientContext)
}
```

## `useQuery`

**Usage**:

```js
const state = useQuery(query, [options])
```

**Example:**

```js
import { useQuery } from 'graphql-hooks'

function MyComponent() {
const { loading, error, data } = useQuery(query)

if (loading) return 'Loading...'
if (error) return 'Something bad happened'

return

{data.thing}

}
```

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.

- `query`: Your GraphQL query as a plain string or DocumentNode
- `options`: Object with the following optional properties
- `variables`: Object e.g. `{ limit: 10 }`
- `operationName`: If your query has multiple operations, pass the name of the operation you wish to execute.
- `persisted`: Boolean - defaults to `false`; Pass `true` if your graphql server supports `persisted` flag to serve persisted queries.
- `useCache`: Boolean - defaults to `true`; cache the query result
- `skip`: Boolean - defaults to `false`; do not execute the query if set to `true`
- `skipCache`: Boolean - defaults to `false`; If `true` it will by-pass the cache and fetch, but the result will then be cached for subsequent calls. Note the `refetch` function will do this automatically
- `ssr`: Boolean - defaults to `true`. Set to `false` if you wish to skip this query during SSR
- `fetchOptionsOverrides`: Object - Specific overrides for this query. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) for info on what options can be passed
- `updateData(previousData, data)`: Function - Custom handler for merging previous & new query results; return value will replace `data` in `useQuery` return value
- `previousData`: Previous GraphQL query or `updateData` result
- `data`: New GraphQL query result
- `client`: GraphQLClient - If a GraphQLClient is explicitly passed as an option, then it will be used instead of the client from the `ClientContext`.
- `refetchAfterMutations`: String | Object | (String | Object)[] - You can specify when a mutation should trigger query refetch.
- If it's a string, it's the mutation string
- If it's an object then it has properties mutation and filter
- `mutation`: String - The mutation string
- `refetchOnMutationError`: boolean (optional, defaults to `true`) - It indicates whether the query must be re-fetched if the mutation returns an error
- `filter`: Function (optional) - It receives mutation's variables as parameter and blocks refetch if it returns false
- If it's an array, the elements can be of either type above

### `useQuery` return value

```js
const { loading, error, data, refetch, cacheHit } = useQuery(QUERY)
```

- `loading`: Boolean - `true` if the query is in flight
- `data`: Object - the result of your GraphQL query
- `refetch(options)`: Function - useful when refetching the same query after a mutation; NOTE this presets `skipCache=true` & will bypass the `options.updateData` function that was passed into `useQuery`. You can pass a new `updateData` into `refetch` if necessary.
- `options`: Object - options that will be merged into the `options` that were passed into `useQuery` (see above).
- `cacheHit`: Boolean - `true` if the query result came from the cache, useful for debugging
- `error`: Object - Set if at least one of the following errors has occurred and contains:
- `fetchError`: Object - Set if an error occurred during the `fetch` call
- `httpError`: Object - Set if an error response was returned from the server
- `graphQLErrors`: Array - Populated if any errors occurred whilst resolving the query

## `useManualQuery`

Use this when you don't want a query to automatically be fetched or wish to call a query programmatically.

**Usage**:

```js
const [queryFn, state] = useManualQuery(query, [options])
```

**Example**:

```js
import { useManualQuery } from 'graphql-hooks'

function MyComponent(props) {
const [fetchUser, { loading, error, data }] = useManualQuery(GET_USER_QUERY, {
variables: { id: props.userId }
})

return (


Get User!
{error &&
Failed to fetch user
}
{loading &&
Loading...
}
{data &&
Hello ${data.user.name}
}

)
}
```

If you don't know certain options when declaring the `useManualQuery` you can also pass the same options to the query function itself when calling it:

```js
import { useManualQuery } from 'graphql-hooks'

function MyComponent(props) {
const [fetchUser] = useManualQuery(GET_USER_QUERY)

const fetchUserThenSomething = async () => {
const user = await fetchUser({
variables: { id: props.userId }
})
return somethingElse()
}

return (


Get User!

)
}
```

## `useQueryClient`

Will return the graphql client provided to `ClientContext.Provider` as `value`

**Usage**:

```js
const client = useQueryClient()
```

**Example**:

```js
import { useQueryClient } from 'graphql-hooks'

function MyComponent() {
const client = useQueryClient()

return

...

}
```

## `useMutation`

Mutations unlike Queries are not cached.

**Usage**:

```js
const [mutationFn, state, resetFn] = useMutation(mutation, [options])
```

**Example**:

```js
import { useMutation } from 'graphql-hooks'

const UPDATE_USER_MUTATION = `mutation UpdateUser(id: String!, name: String!) {
updateUser(id: $id, name: $name) {
name
}
}`

function MyComponent({ id, name }) {
const [updateUser] = useMutation(UPDATE_USER_MUTATION)
const [newName, setNewName] = useState(name)

return (


setNewName(e.target.value)}
/>
updateUser({ variables: { id, name: newName } })}
/>

)
}
```

The `options` object that can be passed either to `useMutation(mutation, options)` or `mutationFn(options)` can be set with the following properties:

- `variables`: Object e.g. `{ limit: 10 }`
- `operationName`: If your query has multiple operations, pass the name of the operation you wish to execute.
- `fetchOptionsOverrides`: Object - Specific overrides for this query. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) for info on what options can be passed
- `client`: GraphQLClient - If a GraphQLClient is explicitly passed as an option, then it will be used instead of the client from the `ClientContext`.
- `onSuccess`: A function to be called after the mutation has been finished with success without raising any error

In addition, there is an option to reset the current state before calling the mutation again, by calling `resetFn(desiredState)` where `desiredState` is optional and if passed, it will override the initial state with:

- `data`: Object - the data
- `error`: Error - the error
- `loading`: Boolean - true if it is still loading
- `cacheHit`: Boolean - true if the result was cached

## `useSubscription`

To use subscription you can use either [subscriptions-transport-ws](https://github.com/apollographql/subscriptions-transport-ws) or [graphql-ws](https://github.com/enisdenjo/graphql-ws)

**API**

`useSubscription(operation, callback)`

- `operation`: Object - The GraphQL operation has the following properties:
- `query`: String (required) - the GraphQL query
- `variables`: Object (optional) - Any variables the query might need
- `operationName`: String (optional) - If your query has multiple operations, you can choose which operation you want to call.
- `client`: GraphQLClient - If a GraphQLClient is explicitly passed as an option, then it will be used instead of the client from the `ClientContext`.
- `callback`: Function - This will be invoked when the subscription receives an event from your GraphQL server - it will receive an object with the typical GraphQL response of `{ data: , errors?: [Error] }`

**Usage**:

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`:

```js
import { GraphQLClient } from 'graphql-hooks'
import { SubscriptionClient } from 'subscriptions-transport-ws'
// or
import { createClient } from 'graphql-ws'

const client = new GraphQLClient({
url: 'http://localhost:8000/graphql',
subscriptionClient: () =>
new SubscriptionClient('ws://localhost:8000/graphql', {
/* additional config options */
}),
// or
subscriptionClient: () =>
createClient({
url: 'ws://localhost:8000/graphql'
/* additional config options */
})
})
```

Next, within our React app, we can now make use of the `useSubscription` hook.

```js
import React, { useState } from 'react'
import { useSubscription } from 'graphql-hooks'

const TOTAL_COUNT_SUBSCRIPTION = `
subscription TotalCount {
totalCount {
count
}
}
`

function TotalCountComponent() {
const [count, setCount] = useState(0)
const [error, setError] = useState(null)

useSubscription({ query: TOTAL_COUNT_SUBSCRIPTION }, ({ data, errors }) => {
if (errors && errors.length > 0) {
// handle your errors
setError(errors[0])
return
}

// all good, handle the gql result
setCount(data.totalCount.count)
})

if (error) {
return An error occurred {error.message}
}

return

Current count: {count}

}
```

**Working Example**:

See our [subscription example](examples/subscription) which has both the client and server code to integrate subscriptions into your application.

See also the [full WS transport example](examples/full-ws-transport) if you want to see how to send every operation through WebSocket.

# Guides

## SSR

See [graphql-hooks-ssr](packages/graphql-hooks-ssr) for an in depth guide.

## Pagination

[GraphQL Pagination](https://graphql.org/learn/pagination/) can be implemented in various ways and it's down to the consumer to decide how to deal with the resulting data from paginated queries. Take the following query as an example of offset pagination:

```javascript
export const allPostsQuery = `
query allPosts($first: Int!, $skip: Int!) {
allPosts(first: $first, skip: $skip) {
id
title
url
}
_allPostsMeta {
count
}
}
`
```

In this query, the `$first` variable is used to limit the number of posts that are returned and the `$skip` variable is used to determine the offset at which to start. We can use these variables to break up large payloads into smaller chunks, or "pages". We could then choose to display these chunks as distinct pages to the user, or use an infinite loading approach and append each new chunk to the existing list of posts.

### Separate pages

Here is an example where we display the paginated queries on separate pages:

```jsx
import { React, useState } from 'react'
import { useQuery } from 'graphql-hooks'

export default function PostList() {
// set a default offset of 0 to load the first page
const [skipCount, setSkipCount] = useState(0)

const { loading, error, data } = useQuery(allPostsQuery, {
variables: { skip: skipCount, first: 10 }
})

if (error) return

There was an error!

if (loading && !data) return
Loading

const { allPosts, _allPostsMeta } = data
const areMorePosts = allPosts.length < _allPostsMeta.count

return (


setSkipCount(skipCount - 10)}
disabled={skipCount === 0}
>
Previous page

setSkipCount(skipCount + 10)}
disabled={!areMorePosts}
>
Next page


)
}
```

### Infinite loading

Here is an example where we append each paginated query to the bottom of the current list:

```jsx
import { React, useState } from 'react'
import { useQuery } from 'graphql-hooks'

// use options.updateData to append the new page of posts to our current list of posts
const updateData = (prevData, data) => ({
...data,
allPosts: [...prevData.allPosts, ...data.allPosts]
})

export default function PostList() {
const [skipCount, setSkipCount] = useState(0)

const { loading, error, data } = useQuery(allPostsQuery, {
variables: { skip: skipCount, first: 10 },
updateData
})

if (error) return

There was an error!

if (loading && !data) return
Loading

const { allPosts, _allPostsMeta } = data
const areMorePosts = allPosts.length < _allPostsMeta.count

return (


{areMorePosts && (
setSkipCount(allPosts.length)}
>
Show more

)}

)
}
```

## Refetch queries with mutations subscription

We can have a query to automatically refetch when any mutation from a provided list is executed.
In the following example we are refetching a list of posts for a given user.

**Example**

```jsx
export const allPostsByUserIdQuery = `
query allPosts($userId: Int!) {
allPosts(userId: $userId) {
id
title
url
}
}
`

export const createPostMutation = `
mutation createPost($userId: Int!, $text: String!) {
createPost(userId: $userId, text: $text) {
id
title
url
}
}
`

const myUserId = 5

useQuery(allPostsByUserIdQuery, {
variables: {
userId: myUserId
},
refetchAfterMutations: [
{
mutation: createPostMutation,
filter: variables => variables.userId === myUserId
}
]
})
```

## Manually updating the cache after some mutation

There are two ways to reach that:

### By re-fetching the query

```js
import { useMutation, useQueryClient } from 'graphql-hooks'
import React from 'react'

const MY_MUTATION = `...`
const MY_QUERY = `...`

export default function MyComponent() {
const client = useQueryClient()
const [applyMutation, { ... }] = useMutation(MY_MUTATION, {
onSuccess: () => client.invalidateQuery(MY_QUERY)
})

return (
...
)
}
```

### By overriding the old state in the cache without re-fetching data

```js
import { useMutation, useQueryClient } from 'graphql-hooks'
import React from 'react'

const MY_MUTATION = `...`
const MY_QUERY = `...`

export default function MyComponent() {
const client = useQueryClient()
const [applyMutation, { ... }] = useMutation(MY_MUTATION, {
onSuccess: (result) => {
client.setQueryData(MY_QUERY, oldState => [
...oldState,
result,
])
}
})

return (
...
)
}
```

## File uploads

`graphql-hooks` complies with the [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec), allowing files to be used as query or mutation arguments. The same spec is also supported by popular GraphQL servers, including [Apollo Server](https://www.apollographql.com/docs/apollo-server) (see list of supported servers [here](https://github.com/jaydenseric/graphql-multipart-request-spec#server)).

If there are files to upload, the request's body will be a [`FormData`](https://developer.mozilla.org/docs/Web/API/FormData) instance conforming to the GraphQL multipart request spec.

```jsx
import React, { useRef } from 'react'
import { useMutation } from 'graphql-hooks'

const uploadPostPictureMutation = `
mutation UploadPostPicture($picture: Upload!) {
uploadPostPicture(picture: $picture) {
id
pictureUrl
}
}
`

export default function PostForm() {
// File input is always uncontrolled in React.
// See: https://reactjs.org/docs/uncontrolled-components.html#the-file-input-tag.
const fileInputRef = useRef(null)

const [uploadPostPicture] = useMutation(uploadPostPictureMutation)

const handleSubmit = event => {
event.preventDefault()

uploadPostPicture({
variables: {
picture: fileInputRef.current.files[0]
}
})
}

return (


Upload

)
}
```

### File uploads Node.js

```js
import { FormData } from 'formdata-node'
import { fileFromPath } from 'formdata-node/file-from-path'

const client = new GraphQLClient({
url: 'https://domain.com/graphql',
fetch: require('node-fetch'),
FormData
})

const uploadPostPictureMutation = `
mutation UploadPostPicture($picture: Upload!) {
uploadPostPicture(picture: $picture) {
id
pictureUrl
}
}
`

const { data, error } = await client.request({
query: uploadPostPictureMutation,
variables: { picture: await fileFromPath('some-file.txt') }
})
```

## HTTP Get support

Using `GET` for queries can be useful, especially when implementing any sort of HTTP caching strategy. There are two ways you can do this:

**Per Query**

```js
const { loading, error, data } = useQuery(MY_QUERY, {
fetchOptionsOverrides: { method: 'GET' }
})

// same goes for useManualQuery
const [fetchSomething] = useManualQuery(MY_QUERY, {
fetchOptionsOverrides: { method: 'GET' }
})
```

**For All Queries**

When you create your client, set the `useGETForQueries` option as `true`:

```js
const client = new GraphQLClient({
url: '/graphql',
useGETForQueries: true
})
```

## Authentication

You can have access to the graphql-hooks client context by using React's new context API. `ClientContext` is actually the result of `React.createContext()`.

**Login Example**

```jsx
import React, { useState, useContext } from 'react'
import { useMutation, ClientContext } from 'graphql-hooks'

const LOGIN_MUTATION = `mutation LoginUser (name: String!, password: String!) {
loginUser(name: $name, password: $password) {
token
}
}`

const Login = () => {
const client = useContext(ClientContext)
const [loginUserMutation] = useMutation(LOGIN_MUTATION)
const [userName, setUserName] = useState()
const [password, setPassword] = useState()

const handleLogin = async e => {
e.preventDefault()
const { data, error } = await loginUserMutation({
variables: { userName, password }
})
if (error) {
// your code to handle login error
} else {
const { token } = data.loginUser
client.setHeader('Authorization', `Bearer ${token}`)
// your code to handle token in browser and login redirection
}
}
return (

User Name:{' '}
setUserName(e.target.value)}
/>
PassWord: setPassword(e.target.value)}
/>


)
}

export default Login
```

In the above example we use `useContext()` hook to get access to the graphql-hooks clientContext.
Then we request the token from the server by performing the `loginUser` mutation.
In the case the login is successful we set the token to the client's header (`client.setHeader`), otherwise we need to handle the error.
For more information about graphql-hooks clientContext refer to [GraphQLClient](#GraphQLClient) section.

## Fragments

Coming soon!

## Migrating from Apollo

For a real life example, compare the next.js [with-apollo](https://github.com/zeit/next.js/tree/canary/examples/with-apollo) vs [with-graphql-hooks](https://github.com/zeit/next.js/tree/canary/examples/with-graphql-hooks). We have feature parity and the `main-*.js` bundle is a whopping **93% smaller** (7.9KB vs 116KB).

### ApolloClient ➑️ GraphQLClient

```diff
- import { ApolloClient } from 'apollo-client'
- import { InMemoryCache } from 'apollo-cache-inmemory'
+ import { GraphQLClient } from 'graphql-hooks'
+ import memCache from 'graphql-hooks-memcache'

- const client = new ApolloClient({
- uri: '/graphql',
- cache: new InMemoryCache()
- })
+ const client = new GraphQLClient({
+ url: '/graphql',
+ cache: memCache()
+ })
```

A lot of the options you'd pass to `ApolloClient` are the same as `GraphQLClient`:

- `uri` ➑️ `url`
- `fetchOptions`
- `onError` - the function signature is slightly different
- `headers`
- `fetch`
- `cache`

### ApolloProvider ➑️ ClientContext.Provider

```diff
- import { ApolloProvider } from 'react-apollo'
+ import { ClientContext } from 'graphql-hooks'

function App({ client }) {
return (
-
+
{/* children */}
+
-
)
}
```

### Query Component ➑️ useQuery

```diff
- import { Query } from 'react-apollo'
- import gql from 'graphql-tag'
+ import { useQuery } from 'graphql-hooks'

function MyComponent() {
+ const { loading, error, data } = useQuery('...')

- return (
-
- {({ loading, error, data}) => {
if (loading) return 'Loading...'
if (error) return 'Error :('

return

{data}

- }}
-
- )
}
```

### Query Component Props

A lot of options can be carried over as-is, or have direct replacements:

- `query` ➑️ `useQuery(query)`: Remove any usage of `gql` and pass your queries as strings.
- `variables` ➑️ `useQuery(query, { variables })`
- `ssr` ➑️ `useQuery(query, { ssr })`
- **Fetch Policies**: See [#75](https://github.com/nearform/graphql-hooks/issues/75) for more info
- `cache-first`: This is the default behaviour of `graphql-hooks`
- `cache-and-network`: The refetch function provides this behavior it will set loading: true, but the old data will be still set until the fetch resolves.
- `network-only` ➑️ `useQuery(QUERY, { skipCache: true })`
- `cache-only`: Not supported
- `no-cache` ➑️ `useQuery(QUERY, { useCache: false })`

**Not yet supported**

- `errorPolicy`: Any error will set the `error` to be truthy. See [useQuery](#useQuery) for more details.
- `pollInterval`
- `notifyOnNetworkStatusChange`
- `skip`
- `onCompleted`: Similar ability if using `useManualQuery`
- `onError`: Similar ability if using `useManualQuery`
- `partialRefetch`

### Query Component Render Props

```diff
-
- {(props) => {}}
-
+ const state = useQuery(`...`)
```

- `props.loading` ➑️ `const { loading } = useQuery('...')`
- `props.error` ➑️ `const { error } = useQuery('...')`: The error value from `useQuery` is Boolean the details of the error can be found in either:
- `state.fetchError`
- `state.httpError`
- `state.graphQLErrors`
- `props.refetch` ️➑️ `const { refetch } = useQuery('...')`
- `props.updateData(prevResult, options)` ️➑️ `state.updateData(prevResult, newResult)`

**Not yet supported**

- `props.networkStatus`
- `props.startPolling`
- `props.stopPolling`
- `props.subscribeToMore`

### Mutation Component ➑️ useMutation

```diff
- import { Mutation } from 'react-apollo'
- import gql from 'graphql-tag'
+ import { useMutation } from 'graphql-hooks'

function MyComponent() {
+ const [mutateFn, { loading, error, data }] = useMutation('...')

- return (
-
- {(mutateFn, { loading, error }) => {
if (error) return 'Error :('

return mutateFn()}>Submit
- }}
-
- )
}
```

### Mutation Props

- `mutation` ➑️ `useMutation(mutation)` - no need to wrap it in `gql`
- `variables` ➑️️ `useMutation(mutation, { variables })` or `mutateFn({ variables })`
- `ignoreResults` ➑️️️️ `const [mutateFn] = useMutation(mutation)`
- `onCompleted` ➑️ ️`mutateFn().then(onCompleted)`
- `onError` ➑️ `mutateFn().then(({ error }) => {...})`

**Not yet supported**

- `update`: Coming soon [#52](https://github.com/nearform/graphql-hooks/issues/52)
- `optimisticResponse`
- `refetchQueries`
- `awaitRefetchQueries`
- `context`

## Mutation Component Render Props

```diff
-
- {(mutateFn, props) => {}}
-
+ const [mutateFn, state] = useMutation(`...`)
```

- `props.data` ➑️ `const [mutateFn, { data }] = useMutation()`
- `props.loading` ➑️ `const [mutateFn, { loading }] = useMutation()`
- `props.error` ➑️ `const [mutateFn, { error }] = useMutation()`: The the details of the error can be found in either:
- `state.fetchError`
- `state.httpError`
- `state.graphQLErrors`
- `client` ️➑️️ `const client = useContext(ClientContext)` see [ClientContext](#ClientContext)

**Not yet supported**

- `called`

## Testing and mocking

There is a `LocalGraphQLClient` class you can use to mock requests without a server for testing or development purposes.

This client inherits from `GraphQLClient` and provides the same API, but doesn't connect to any server and instead responds to pre-defined queries.

It needs to be supplied on creation with a `localQueries` object, which is an object where:

- the keys are the queries defined in the application;
- the values are query functions returning the mocked data.

```js
// src/components/Post.js
export const allPostsQuery = `
query {
allPosts {
id
title
url
}
}
`
```

```js
// test/Post.test.tsx
import { allPostsQuery, createPostMutation } from '../src/components/Post'

const localQueries = {
[allPostsQuery]: () => ({
allPosts: [
{
id: 1,
title: 'Test',
url: 'https://example.com'
}
]
}),
[createPostMutation]: () => ({ createPost: { id: 1 } })
}
const client = new LocalGraphQLClient({ localQueries })
const { data, error } = await client.request({
query: allPostsQuery
})
```

The `LocalGraphQLClient` will return `data` and `error` properties in the same format as the `GraphQLClient`

### Variables

Variables can be used in the local mock queries given to the `LocalGraphQLClient`, which can then be supplied to the `request` function:

```js
const localQueries = {
AddNumbersQuery: ({ a, b }) => ({
addedNumber: a + b
})
}
const client = new LocalGraphQLClient({ localQueries })
const result = await client.request({
query: 'AddNumbersQuery',
variables: {
a: 2,
b: 3
}
})
console.log(result.data.addedNumber) // Will be 5
```

### Error mocking

Errors can be simply mocked in `LocalGraphQLClient` queries by using the `LocalGraphQLError` class:

```js
// test/Post.test.tsx
import { allPostsQuery } from '../src/components/Post'

const localQueries = {
[allPostsQuery]: () =>
new LocalGraphQLError({
httpError: {
status: 404,
statusText: 'Not found',
body: 'Not found'
}
})
}
const client = new LocalGraphQLClient({ localQueries })
const result = await client.request({
query: allPostsQuery
})
console.log(result.error) // The `error` object will have an `httpError`
```

It is also possible to mock a partial error response (for example where one resolver encounters an error but other resolvers return successfully). To do this, include `Error` objects in the mock query resolver:

```js
import { allPostsQuery } from '../src/components/Post'

const localQueries = {
[allPostsQuery]: () => ({
field1: 'foo',
field2: new Error('something went wrong'),
nested: {
field3: new Error('a nested error')
}
})
}
const client = new LocalGraphQLClient({ localQueries })
const result = await client.request({
query: allPostsQuery
})
console.log(result.data) // The `data` object will have the correct value for `field1` and `null` for any fields returning `Error` objects
console.log(result.error) // The `error` object will have a `graphQLErrors` array containing each of the `Error` objects created above
```

### Testing with React

Example tests that use the `LocalGraphQLClient` are provided in [the examples/create-react-app/test folder](https://github.com/nearform/graphql-hooks/blob/master/mock-client/examples/create-react-app/test/Posts.test.js).

The [test-utils.js](https://github.com/nearform/graphql-hooks/blob/liana/mock-client/examples/create-react-app/test/test-utils.js) is a good example of how to create a custom render function using [@testing-library/react](https://testing-library.com/docs/react-testing-library/intro/) which can wrap the render of a React component in a `ClientContext` setup to use the `LocalGraphQLClient` with supplied local queries:

```js
const customRender = (ui, options) => {
const client = new LocalGraphQLClient({
localQueries: options.localQueries
})

const Wrapper = ({ children }) => {
return (
{children}
)
}

Wrapper.propTypes = {
children: T.node.isRequired
}

return render(ui, {
wrapper: Wrapper,
...options
})
}

export * from '@testing-library/react'

export { customRender as render }
```

Using this allows us to easily render a component using the `LocalGraphQLClient` with local queries when writing tests:

```js
// Comes from the above code
import { render, screen } from './test-utils'

const localQueries = {
[allPostsQuery]: () => ({
allPosts: [
{
id: 1,
title: 'Test',
url: 'https://example.com'
}
]
})
}

describe('Posts', () => {
it('should render successfully', async () => {
render(, {
localQueries
})

expect(
await screen.findByRole('link', {
name: /Test/i
})
).toBeTruthy()
})
})
```

### Changing mock queries during tests

Because the `LocalGraphQLClient` just uses the `localQueries` object supplied to it, it is possible to modify or spy the local queries during tests. For example:

```js
it('shows "No posts" if 0 posts are returned', async () => {
jest.spyOn(localQueries, allPostsQuery).mockImplementation(() => ({
allPosts: []
}))

render(, {
localQueries
})

expect(await screen.findByText('No posts')).toBeTruthy()
})
```

## Typescript Support

All client methods support the ability to provide type information for response data, query variables and error responses.

```typescript
import { useQuery } from 'graphql-hooks'

type User = {
id: string
name: string
}

type CustomError = {
message: string
extensions?: Record
}

const HOMEPAGE_QUERY = `query HomePage($limit: Int) {
users(limit: $limit) {
id
name
}
}`

function MyComponent() {
const { loading, error, data } = useQuery<
User,
{ limit: number },
CustomError
>(HOMEPAGE_QUERY, {
variables: {
limit: 10
}
})

if (loading) return 'Loading...'
if (error) return 'Something Bad Happened'

return (


    {data.users.map(({ id, name }) => (
  • {name}

  • ))}

)
}
```

`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.

```typescript
import { useQuery } from 'graphql-hooks'
import { graphql } from './gql'

const HOMEPAGE_QUERY = graphql(`query HomePage($limit: Int) {
users(limit: $limit) {
id
name
}
}`)

function MyComponent() {
// data will be typed as User objects with id, name properties
const { loading, error, data } = useQuery(HOMEPAGE_QUERY, {
variables: {
limit: 10
}
})

if (loading) return 'Loading...'
if (error) return 'Something Bad Happened'

return (


    {data.users.map(({ id, name }) => (
  • {name}

  • ))}

)
}
```

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.

## Other

### Request interceptors

It is possible to provide a custom library to handle network requests. Having that there is more control on how to handle the requests. The following example shows how to supply axios HTTP client with interceptors. It can be handy in the situations where JWT token has expired, needs to be refreshed and request retried.

```js
import axios from 'axios'
import { buildAxiosFetch } from '@lifeomic/axios-fetch'
import { GraphQLClient } from 'graphql-hooks'

const gqlAxios = axios.create()
gqlAxios.interceptors.response.use(
function (response) {
return response
},
function (error) {
// Handle expired JWT and refresh token
}
)

const client = new GraphQLClient({
url: '/graphql',
fetch: buildAxiosFetch(gqlAxios)
})
```

### AbortController

if you wish to abort a fetch it is possible to pass an AbortController signal to the `fetchOptionsOverrides` option of the fetch function. This is not `graphql-hooks` specific functionality, rather just an example of how to use it with the library.

```js
import { useManualQuery } from 'graphql-hooks'

function AbortControllerExample() {
const abortControllerRef = useRef()
const [fetchData, { loading }] = useManualQuery(`...`)

const handleFetch = () => {
abortControllerRef.current = new AbortController()
const { signal } = abortControllerRef.current
fetchData({
fetchOptionsOverrides: {
signal
}
})
}

const handleAbort = () => {
abortControllerRef.current?.abort()
}

return (
<>
Fetch Data
{loading && Abort}
>
)
}
```

### GraphQL Document Support

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`.

```js
import gql from 'graphql-tag'

const allPostsQuery = gql`
query {
posts {
id
name
}
}
`

function Posts() {
const { loading, error, data, refetch } = useQuery(allPostsQuery)

return (
<>

Add post



Posts


refetch()}>Reload

>
)
}

...
```

## Community

We now use GitHub Discussions for our community. To join, click on ["Discussions"](https://github.com/nearform/graphql-hooks/discussions). We encourage you to start a new discussion, share some ideas or ask questions from the community.
If you want to see the old community posts (on Spectrum) you can access them [here](https://spectrum.chat/graphql-hooks).

## Contributors

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Brian Mullan
Brian Mullan

πŸ’¬ πŸ› πŸ’» πŸ–‹ πŸ“– πŸ’‘ πŸ€” 🚧 πŸ‘€ ⚠️
Jack Clark
Jack Clark

πŸ’¬ πŸ› πŸ’» πŸ–‹ πŸ“– πŸ’‘ πŸ€” 🚧 πŸ‘€ ⚠️
Joe Warren
Joe Warren

πŸ’¬ πŸ› πŸ’» πŸ–‹ πŸ“– πŸ’‘ πŸ€” 🚧 πŸ‘€ ⚠️
Simone Busoli
Simone Busoli

πŸ’¬ πŸ› πŸ“–
jhey tompkins
jhey tompkins

⚠️ πŸ’¬ πŸ› πŸ’» πŸ–‹ πŸ‘€
Haroen Viaene
Haroen Viaene

πŸ›
Ari Bouius
Ari Bouius

πŸ“– πŸ› πŸ’» ⚠️


Klemen KogovΕ‘ek
Klemen KogovΕ‘ek

πŸ› πŸ€” πŸ’» ⚠️
WΓ©sley Queiroz
WΓ©sley Queiroz

πŸ› πŸ’»
Joseph Thomas
Joseph Thomas

πŸ› πŸ’» ⚠️
Edvinas Bartkus
Edvinas Bartkus

πŸ’» πŸ’¬ πŸ› πŸ“– πŸ’‘ πŸ€” 🚧 πŸ‘€ ⚠️
MatΓ­as Olivera
MatΓ­as Olivera

πŸ› πŸ’» ⚠️ πŸ“–
tcudok-jg
tcudok-jg

πŸ’»
Martin Adams
Martin Adams

πŸ“–


Gal Dubitski
Gal Dubitski

πŸ’» πŸ› πŸ“– ⚠️
Abhishek Shende
Abhishek Shende

πŸ’» πŸ›
fabienheureux
fabienheureux

πŸ‘€
Hugh Boylan
Hugh Boylan

πŸ‘€
Baqer Mamouri
Baqer Mamouri

πŸ’»
Guillermo Gonzalez
Guillermo Gonzalez

πŸ’»
Johan Brook
Johan Brook

πŸ’» πŸ› 🚧


Peter Balazs
Peter Balazs

πŸ’» πŸ“– πŸ’‘ ⚠️
Mattia Panzeri
Mattia Panzeri

πŸ’» ⚠️
Alex Kondratyuk
Alex Kondratyuk

πŸ’» ⚠️ πŸ“– πŸ›
Matias Cepeda
Matias Cepeda

πŸ“–
Jack Huey
Jack Huey

πŸ› πŸ’» πŸ“– ⚠️
Roman Zanettin
Roman Zanettin

πŸ’» ⚠️
Francesco Maida
Francesco Maida

🚧

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome!