GithubHelp home page GithubHelp logo

asafaeirad / graphql-utils Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 256 KB

Useful utilities for create graphql server and client

License: MIT License

TypeScript 100.00%
graphql apollo-resolvers apollo-client

graphql-utils's Introduction

@frontendmonster/graphql-utils

Client Module

API

The simple way to generate apollo-client with jwt refresh token and authentication helpers.

api description
createApolloClient create zero-config apollo-client with retriable-fetch.
createRetriableFetch create apollo-http-link fetch that refreshes token when jwt token is expired.
import { createApolloClient } from '@frontendmonster/graphql-utils/client';

const apolloClinet = createApolloClient({
    endpoint: 'http://localhost:3000/graphql',
    intialState: {},
    refreshTokenMutation: `graphqlMutationToRefreshToken($userId, $refreshToken) { token, refreshToken } `,
    resolvers: {},
    debug: isDev,
    storage: MyStorage,
    onLogin: (storage, cache, user, accessToken, refreshToken) => console.log(`${user} logged in let's save that on storage and update apollo-cache.`),
    onLogout: (storage, cache) => console.log('user logged out lets reset the apollo-cache and storage'),
  });
};

Server Module

graphql-errors

Extending apollo-server-errors

  • NotFoundError
  • AlreadyExistedError
  • ForbiddenError
  • AlreadyAuthenticatedError
  • UnknownError
  • UserInputError
  • ForbidenError
  • AuthenticationError
  • ValidationError

Usage

import { NotFoundError } from '@frontendmonster/graphql-utils/server';

const resolvers = {
  user: (root, input, context) => {
    // ...
    if(user == null) {
      throw new NotFoundError('user'); // throws NOT_FOUND error with UserNotFound message
    }
    // ...
  }
}

graphql-client

Simple graphql client for testing purpose. You can inject mongodb models and redis instance.

Usage

  1. Create a graphql-client instance with schema, mongoose models and redis client.
  2. Use instance.run(query);

GraphqlClient Instace Api

run<T>:: (query, [user], [variables]) => Promise<T>

arguments:

  • query: graphql query
  • user: will pass to query context for authorization purpos
  • variables: query variables

Example

import { GraphqlClient } from '@frontendmonster/graphql-utils/server';
import { schema, context } from './schema';

const graphqlClient = new GraphqlClient({ schema, context });

const query = `{
  hello
}`;

const result = await graphqlClient.run(query); // result.data.hello > 'world'

graphql-utils's People

Contributors

asafaeirad avatar

Stargazers

 avatar Amin darabniya avatar Yasin Silavi avatar

Watchers

James Cloos avatar  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.