Skip to content

Commit

Permalink
📃 docs(README): Add error handling example
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowflyt committed Mar 9, 2024
1 parent 75a325f commit 2cd9ccb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,33 @@ const client = createClient('http://example.com/graphql', {
});
```

### Handle errors

It is worth noting graphql-intuitive-request still use the same error handling mechanism as graphql-request. You can handle errors by catching them. The type of the error is `ClientError` from `graphql-request`, and graphql-intuitive-request also re-exports it.

```typescript
import { ClientError } from 'graphql-intuitive-request';
import { query } from './client';

try {
const user = await query('user')
.select((user) => [user.id, user.name])
.byId(1);
console.log(user);
} catch (error) {
if (error instanceof ClientError) console.error(error.response.errors);
else console.error(error);
}

// ... or use `Promise.catch`
void query('user')
.select((user) => [user.id, user.name])
.byId(1)
.catch((err: ClientError) => {
console.error(err.response.errors);
});
```

### Work with graphql-ws

The subscription feature of graphql-intuitive-request is built on top of graphql-ws, so you can use all the features of graphql-ws with graphql-intuitive-request.
Expand Down

0 comments on commit 2cd9ccb

Please sign in to comment.