npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lee-fusion-plugin-apollo

v0.0.6

Published

FusionJS entry point for React universal rendering /w Apollo

Downloads

9

Readme

fusion-plugin-apollo

Build status

Fusion.js plugin for universal rendering with React and Apollo.

This package provides universal rendering for Fusion.js applications leveraging GraphQL.

The plugin will perform GraphQL queries on the server, thereby rendering your applications initial HTML view on the server before sending it to the client. Additionally this plugin will also provide initial state hydration on the client side.


Table of contents


Installation

yarn add fusion-plugin-apollo

Usage

import React from 'react';
import App from 'fusion-react';
import {RenderToken} from 'fusion-core';

import {
  ApolloRenderEnhancer,
  ApolloClientPlugin,
  ApolloClientToken,
  GraphQLSchemaToken,
} from 'fusion-plugin-apollo';

export default function() {
  const app = new App(<Hello />);
  app.enhance(RenderToken, ApolloRenderEnhancer);
  app.register(ApolloClientToken, ApolloClientPlugin);
  if (__NODE__) {
    app.register(GraphQLSchemaToken, YourGraphQLSchema);
  }
  return app;
}

Usage with external server

When a schema file is not provided, the plugin will not run a GraphQL server locally. The endpoint of the external GraphQL server can be then provided using the GraphQLEndpointToken.

import React from 'react';
import App from 'fusion-react';
import {RenderToken} from 'fusion-core';

import {
  ApolloRenderEnhancer,
  ApolloClientPlugin,
  ApolloClientToken,
  GraphQLSchemaToken,
} from 'fusion-plugin-apollo';

export default function() {
  const app = new App(<Hello />);
  app.enhance(RenderToken, ApolloRenderEnhancer);
  app.register(ApolloClientToken, ApolloClientPlugin);
  app.register(GraphQLEndpointToken, 'http://website.com/graphql');
  return app;
}

Loading GraphQL Queries/Schemas

fusion-plugin-apollo ships with a compiler plugin that lets you load GraphQL queries and schemas with the gql macro. This macro takes a relative path argument and returns the query/schema as a string.

NOTE: Because this is a build time feature, the path argument must be a string literal. Dynamic paths are not supported.

import {gql} from 'fusion-plugin-apollo';
const query = gql('./some-query.graphql');
const schema = gql('./some-schema.graphql');

API

Registration API

ApolloClientToken
import {ApolloClientToken} from 'fusion-plugin-apollo';

A plugin, which provides an instance of Apollo Client, to be registered and used within the Apollo Provider. You can use ApolloClientPlugin as a barebones Apollo Client token.

type ApolloClient<TInitialState> = (ctx: Context, initialState: TInitialState) => ApolloClientType;
GraphQLSchemaToken
import {GraphQLSchemaToken} from 'fusion-plugin-apollo';

Your GraphQL schema is registered on the GraphQLSchemaToken token. This is the result of makeExecutableSchema or makeRemoteExecutableSchema from the graphql-tools library.

GraphQLEndpointToken
import {GraphQLEndpointToken} from 'fusion-plugin-apollo';

Optional - the endpoint for serving the GraphQL API. Defaults to '/graphql'. This can also be an endpoint of an external GraphQL server (hosted outside the Fusion.js app).

type GraphQLEndpoint = string;
GetApolloClientCacheToken
import {GetApolloClientCacheToken} from 'fusion-plugin-apollo';

Optional - A function that returns an Apollo cache implementation.

type GetApolloClientCache = (ctx: Context) => ApolloCache
Default value

The default cache implementation uses InMemoryCache.

ApolloClientCredentialsToken
import {ApolloClientCredentialsToken} from 'fusion-plugin-apollo';

Optional - A configuration value that provides the value of credentials passed directly into the fetch implementation. The default value is same-origin.

type ApolloClientCredentials = 'omit' | 'include' | 'same-origin'
GetApolloClientLinksToken
import {GetApolloClientLinksToken} from 'fusion-plugin-apollo';

Optional - A configuration value that provides a array of ApolloLinks. The default links are provided as an argument to the provided function.

type GetApolloClientLinks = (Array<ApolloLinkType>) => Array<ApolloLinkType>
GetDataFromTreeToken
import {GetDataFromTreeToken} from 'fusion-plugin-apollo';

Optional - A configuration value that provides an option to provide custom getDataFromTree function for server side rendering. The default value is the react-apollo implementation but with this token you can provide proper server side rendering with react-apollo-hooks.

ApolloClientResolversToken
import {ApolloClientResolversToken} from 'fusion-plugin-apollo';

Optional - Provides the resolvers for local state management.

ApolloClientLocalSchemaToken
import {ApolloClientLocalSchemaToken} from 'fusion-plugin-apollo';

Optional - Provides the typeDefs for local state management.

ApolloBodyParserConfigToken
import {ApolloBodyParserConfigToken} from 'fusion-plugin-apollo';
// Example for increasing the json limit
app.register(ApolloBodyParserConfigToken, {
  jsonLimit: '5mb',
});

Optional - Provides body parser config to koa-bodyparser for apollo-server. See https://github.com/koajs/bodyparser.

ApolloDefaultOptionsConfigToken
import {ApolloDefaultOptionsConfigToken} from 'fusion-plugin-apollo';
// Example for enabling schema introspection
app.register(ApolloDefaultOptionsConfigToken, {
  introspection: true,
});

Optional - Provides default options config to apollo-server. See https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-core/src/graphqlOptions.ts.

ApolloContextToken (DEPRECATED)
import {ApolloContextToken} from 'fusion-plugin-apollo';

DEPRECATED - A function which returns the apollo context. Defaults to the Fusion.js context. It is not recommended to add custom overrides here. Instead, you can provide dependencies to your resolvers using the Fusion.js dependency injection system.

See the Apollo Client context documentation for more details.

type ApolloContext<T> = (ctx: Context => T) | T;

gql

import {gql} from 'fusion-plugin-apollo';

A macro for loading GraphQL queries and schemas. Takes a relative path string and returns the contents of the GraphQL schema/query as a string.

type gql = (path: string): DocumentNode
  • path: string - Relative path to the graphql schema/query file. NOTE: This must be a string literal, dynamic paths are not supported.