Skip to content

feat: Implement client #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 4, 2022
Merged

feat: Implement client #3

merged 16 commits into from
Aug 4, 2022

Conversation

enisdenjo
Copy link
Member

@enisdenjo enisdenjo commented Aug 4, 2022

Complying with the GraphQL over HTTP Specification Working Draft (Jul 28, 2022).

Basic usage with authentication and request retries:

import { createClient, NetworkError } from 'graphql-http';
import { getSession } from './my-auth';

const client = createClient({
  url: 'http://unstable.service:4000/graphql',
  headers: async () => {
    const session = await getSession();
    if (session) {
      return {
        Authorization: `Bearer ${session.token}`,
      };
    }
  },
  shouldRetry: async (err: NetworkError, retries: number) => {
    if (retries > 3) {
      // max 3 retries and then report service down
      return false;
    }

    // try again when service unavailable, could be temporary
    if (err.response?.status === 503) {
      // wait one second (you can alternatively time the promise resolution to your preference)
      await new Promise((resolve) => setTimeout(resolve, 1000));
      return true;
    }

    // otherwise report error immediately
    return false;
  },
});

(async () => {
  let cancel = () => {
    /* abort the request if it is in-flight */
  };

  const result = await new Promise((resolve, reject) => {
    let result;
    cancel = client.subscribe(
      {
        query: '{ hello }',
      },
      {
        next: (data) => (result = data),
        error: reject,
        complete: () => resolve(result),
      },
    );
  });

  expect(result).toEqual({ hello: 'world' });
})();

@enisdenjo enisdenjo marked this pull request as ready for review August 4, 2022 15:26
@enisdenjo enisdenjo merged commit c1b4c79 into master Aug 4, 2022
@enisdenjo enisdenjo deleted the client branch August 4, 2022 16:06
enisdenjo pushed a commit that referenced this pull request Aug 4, 2022
# 1.0.0 (2022-08-04)

### Features

* Implement client ([#3](#3)) ([c1b4c79](c1b4c79))
* Implement server handler ([#2](#2)) ([99b888b](99b888b))
@enisdenjo
Copy link
Member Author

🎉 This PR is included in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@enisdenjo enisdenjo added the released Has been released and published label Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Has been released and published
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant