https://github.com/andycmaj/graphql-codegen-typescript-graphql-request-with-retries
forked from @graphql-codegen/typescript-graphql-request with additional retry handling for requests
https://github.com/andycmaj/graphql-codegen-typescript-graphql-request-with-retries
error-handling graphql graphql-code-generator polly-js typescript
Last synced: about 1 month ago
JSON representation
forked from @graphql-codegen/typescript-graphql-request with additional retry handling for requests
- Host: GitHub
- URL: https://github.com/andycmaj/graphql-codegen-typescript-graphql-request-with-retries
- Owner: andycmaj
- Created: 2020-03-01T23:02:26.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T08:46:22.000Z (over 3 years ago)
- Last Synced: 2024-05-21T06:23:10.809Z (almost 2 years ago)
- Topics: error-handling, graphql, graphql-code-generator, polly-js, typescript
- Language: TypeScript
- Size: 1.07 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# -graphql-codegen-typescript-graphql-request-with-retries
forked from @graphql-codegen/typescript-graphql-request with additional retry handling for requests
# example of using the generated SDK with a retry policy from [`polly-js`](https://github.com/mauricedb/polly-js).
```typescript
import { GraphQLClient } from 'graphql-request';
import { getSdk } from './generated/types';
import { ASTNode, print } from 'graphql';
import { Variables } from 'graphql-request/dist/src/types';
import { RetryWrapper } from 'typescript-graphql-request-with-retries';
import polly from 'polly-js';
const client = new GraphQLClient(process.env.GRAPHQL_URL, {
headers: {
'x-hasura-admin-secret': process.env.HASURA_GRAPHQL_ADMIN_SECRET,
},
});
export const request = (query: ASTNode, variables?: Variables) =>
client.request(print(query), variables);
const withRetries: RetryWrapper = (action: () => Promise) =>
polly()
.handle((err: Error) => {
console.log('HANDLE', err.message);
return err.message.includes('connect ETIMEDOUT');
})
.waitAndRetry(3)
.executeForPromise(info => {
console.log('RETRY', info);
return action();
});
const sdk = getSdk(client, withRetries);
export default sdk;
```
# Unit test
```typescript
import nock from 'nock';
import graphqlClient from 'shared/graphqlClient';
describe('graphqlClient', () => {
beforeAll(() => nock.disableNetConnect());
afterAll(() => nock.enableNetConnect());
afterEach(() => nock.cleanAll());
it('retries twice when ETIMEDOUT', async () => {
nock(/localhost|botany-db/)
.post('/v1/graphql')
.replyWithError({ code: 'ETIMEDOUT', message: 'connect ETIMEDOUT' });
nock(/localhost|botany-db/)
.post('/v1/graphql')
.replyWithError({ code: 'ETIMEDOUT', message: 'connect ETIMEDOUT' });
nock(/localhost|botany-db/)
.post('/v1/graphql')
.replyWithError({ code: 'ETIMEDOUT', message: 'connect ETIMEDOUT' });
try {
await graphqlClient.GetDataSource({
dataSourceId: 'github',
});
} catch (e) {
console.log('LAST ERROR', e, e.stack);
}
});
});
```