Giter Site home page Giter Site logo

andycmaj / graphql-codegen-typescript-graphql-request-with-retries Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 1.1 MB

forked from @graphql-codegen/typescript-graphql-request with additional retry handling for requests

JavaScript 6.76% TypeScript 93.24%
graphql graphql-code-generator typescript polly-js error-handling

graphql-codegen-typescript-graphql-request-with-retries's Introduction

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

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 = <T>(query: ASTNode, variables?: Variables) =>
  client.request<T>(print(query), variables);

const withRetries: RetryWrapper = <T>(action: () => Promise<T>) =>
  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

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);
    }
  });
});

graphql-codegen-typescript-graphql-request-with-retries's People

Contributors

andycmaj avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.