fusion-apollo-universal-client v2.2.0
fusion-apollo-universal-client
A simple universal client for GraphQL apps using fusion-apollo.
The Apollo Client is the entrypoint for most Apollo applications. This plugin provides a client with the minimum amount of configuration necessary to create a universally rendered Apollo application.
Table of contents
Installation
yarn add fusion-apollo-universal-client
Usage
Usage with fusion-apollo
import App, {ApolloClientToken} from 'fusion-apollo';
import GetApolloClient, {ApolloClientEndpointToken} from 'fusion-apollo-universal-client';
import unfetch from 'unfetch';
export default () => {
const app = new App(root);
app.register(ApolloClientToken, GetApolloClient);
app.register(ApolloClientEndpointToken, '...');
__NODE__ && app.register(FetchToken, unfetch);
return app;
};
Usage with local server
If your app hosts the Apollo server a schema must be provided.
The schema can be provided using the GraphQLSchemaToken
from fusion-apollo
.
import App, {ApolloClientToken, GraphQLSchemaToken} from 'fusion-apollo';
import {makeExecutableSchema} from 'graphql-tools';
import GetApolloClient, {ApolloClientEndpointToken} from 'fusion-apollo-universal-client';
import unfetch from 'unfetch';
export default () => {
const app = new App(root);
app.register(ApolloClientToken, GetApolloClient);
app.register(ApolloClientEndpointToken, '...');
app.register(GraphQLSchemaToken, makeExecutableSchema(...));
__NODE__ && app.register(FetchToken, unfetch);
return app;
};
See the Apollo Documentation for how to generate a schema.
Authorization
Authorization will work with hosted GraphQL services such as scaphold and graph.cool. This works by passing a stored authentication token inside of the authorization header. This token is currently assumed to stored in a cookie and cookies by the value provided in ApolloClientAuthKeyToken
(defaults to "token").
API
Registration API
ApolloClientEndpointToken
import {ApolloClientEndpointToken} from 'fusion-apollo';
A token with the GraphQL endpoint which the Apollo HttpLink client communicates with. This can be an absolute path to a local GraphQL server, or a remote hosted GraphQL server.
Type
string
- Required. The URI to make GraphQL requests from.
Dependencies
FetchToken
import {FetchToken} from 'fusion-tokens';
A fetch
implementation. Browser-only.
Type
type Fetch = (url: string, options: Object) => Promise<Response>;
url: string
- Required. Path or URL to the resource you wish to fetch.options: Object
- Optional. You may optionally pass aninit
options object as the second argument. See Request for more details.[return]: Promise<Request>
- Return value from fetch. See Response(A function that loads appropriate translations and locale information given an HTTP request context) for more details.
Default value
If no fetch implementation is provided, window.fetch
is used.
ApolloClientAuthKeyToken
import {ApolloClientAuthKeyToken} from 'fusion-tokens';
(Optional) A configuration value that provides the value of an authentication token to use from the document cookies. If provided, this token is used in the Apollo auth middleware as an authorization header.
Type
string
- Required. Name of the cookie which contains the authorization token.
Default value
If no token name is provided, authorization headers are not sent.
Examples
Using fusion-apollo-universal-client with fusion-plugin-csrf-protection
When registering CSRFProtection to the FetchToken, it may be necessary to provide a non-CSRF protected implementation of the FetchToken to the client. You can use token aliasing to register a different service for the FetchToken for fusion-apollo-universal-client.
import {createToken} from 'fusion-core';
const FetchTokenPolyfill = createToken('FetchTokenPolyfill');
app.register(FetchTokenPolyfill, require('node-fetch'));
app
.register(ApolloClientToken, ApolloClient)
.alias(FetchToken, FetchTokenPolyfill);
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago