mnr-graphql-client v0.1.0
mnr-graphql-client
Opinionated wrapper around apollo-client
You may not need it!
This is a custom, highly opinionated solution aimed at code reuse for a few private projects. You'd be better off using Apollo Client directly.
Installation
$ npm install --save mnr-graphql-clientUsage example
const createGraphQLClient = require('mnr-graphql-client');
const graphQLClient = createGraphQLClient('https://my-graphql-server.com/graphql');
// This is an Express endpoint handler
async function handleRequest (req, res, next) {
try {
const query = buildQuerySomehow();
const variables1 = { var: 'foo' };
const data1 = await graphQLClient.query({ query, variables: variables1 }, req);
const mutation = buildQuerySomehow();
const variables2 = { var: 'bar' };
const data2 = await graphQLClient.mutate({ mutation, variables: variables2 }, req);
} catch (err) {
if (err.name === 'ServiceUnavailable') {
res.status(503).end();
return;
}
next(err);
}
}API Reference
createGraphQLClient(uri)
{String} uriURI of the GraphQL server.Returns
{Object} graphQLClient.
Creates a client object which exposes methods for querying GraphQL server.
graphQLClient.query(requestOptions, req)
{Object} requestOptionsOptions to pass in Apollo's originalquerymethod.{Object} reqExpress'Requestobject.Returns
{Promise<Object>}Full original Apollo's response object.Throws
{ServiceUnavailable},BadGraphQLRequest,UnexpectedError.
Alias for Apollo's original query method.
graphQLClient.mutate(requestOptions, req)
{Object} requestOptionsOptions to pass in Apollo's originalmutatemethod.{Object} reqExpress'Requestobject.Returns
{Promise<Object>}Full original Apollo's response object.Throws
{ServiceUnavailable},BadGraphQLRequest,UnexpectedError.
Alias for Apollo's original mutate method.
7 years ago