GithubHelp home page GithubHelp logo

fusionjs / fusion-plugin-apollo Goto Github PK

View Code? Open in Web Editor NEW
16.0 4.0 19.0 760 KB

Migrated to https://github.com/fusionjs/fusionjs

License: MIT License

Shell 0.73% JavaScript 98.72% Dockerfile 0.55%

fusion-plugin-apollo's Introduction

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

// ./src/main.js
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 schema file is not provided, the plugin will not run graphQL server locally. The endpoint of the external graphQL server can be then provided using GraphQLEndpointToken.

// ./src/main.js
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 as within the Apollo Provider. You can use fusion-apollo-universal-client as a barebones Apollo Client token.

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

Optional - A function which returns the apollo context. Defaults to the fusion context. See the Apollo Client context documentation for more details.

type ApolloContext<T> = (ctx: Context => T) | T;
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 fusion app).

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

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-apollo-universal-client';

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

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

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>
ApolloClientResolversToken
import { ApolloClientResolversToken } from "fusion-apollo-universal-client";

Optional - Provides the resolvers for local state management.

ApolloBodyParserConfigToken
import { ApolloBodyParserConfigToken } from "fusion-apollo-universal-client";
// 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

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.

fusion-plugin-apollo's People

Contributors

alexmsmithca avatar angus-c avatar damaera avatar ganemone avatar kevhuang avatar kevingrandon avatar lhorie avatar nadiia avatar rajeshsegu avatar renovate-bot avatar renovate[bot] avatar revskill10 avatar saspre avatar simonrobb avatar szymn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fusion-plugin-apollo's Issues

Old package in Npm?

Old package version in NPM

Description

In package.json I can see version 1.2.1 but npm istalls only version 1.2.0
(https://www.npmjs.com/package/fusion-apollo)
The old version probably causes this error:
Module not found: Error: Can't resolve 'fusion-react-async' in '/Users/jbalaty/Projects/javascript/fusion-boilerplate/redux-boilerplate/node_modules/fusion-apollo/dist'

Your environment

  • fusion-apollo version: 1.2.0

  • Node.js version (node --version): v8.11.3

  • npm version (npm --version): 5.6.0

  • Operating System: MacOS

Log errors

I want to continue a discussion from this PR #166

The previous version has the ability to specify formatError function, which allows to log errors and used by me only for logging.
The new version doesn't have anything to log errors.
This is the critical feature.

I want to get an official decision will it be implemented (in some way or another). If yes, when?

As a workaround, I can implement my custom wrapper on top of all resolvers.

React.createContext on server

Type of issue

Feature request and bug

Description

Due to this PR, we need to keep track of when using ReactContext universally.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset name not found within published preset config (monorepo:angularmaterial). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Required Token Imports don't work

Type of issue

Bug

Description

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

Imports do not work.
Same error with ApolloRenderEnhancer import.

Current behavior

"export 'ApolloClientPlugin' was not found in 'fusion-plugin-apollo'

Expected behavior

It should work

Steps to reproduce

Followed instructions to register fusion-plugin-apollo on a new app created with cli.

Your environment

  • fusion-plugin-apollo version: 2.0.0-0

  • Node.js version (node --version): 11.10.0

  • npm version (npm --version): 6.7.0

  • Operating System: MacOS 10.14.4

Flow error with [email protected]

There's a Flow error with [email protected].

Type of issue

Flow type error

Description

Flow error is gone.

Current behavior

Screen Shot 2019-03-19 at 2 47 22 PM

Expected behavior

No error

Your environment

  • fusion-plugin-apollo version:

  • Node.js version (node --version):

  • npm version (npm --version):

  • Operating System:

GraphQlSchemaToken is required whenever GraphQLEndpointToken is provided

Type of issue

Bug

Description

After upgrading to newest fusion-plugin-apollo the app throws an error regarding missing token.

Current behavior

Throws an error about missing GraphQlSchemaToken while GraphQLEndpointToken is provided.
Important note: I'm trying to connect to external graphql server.

Screenshot 2019-04-03 at 14 54 52

Expected behavior

Shouldn't throw an error :)

Steps to reproduce

clone the repository: https://github.com/dontmakemethink/fusionreproduced

or

  1. yarn create fusion-app my-fusion-app
  2. follow the docs to add apollo to the project (https://fusionjs.com/api/fusion-apollo/)
  3. use GraphQLEndpointToken

Your environment

  • fusion-plugin-apollo version:
    2.0.0-3

  • Node.js version (node --version):
    v11.12.0

  • npm version (npm --version):
    6.7.0

  • Operating System:
    macOS Mojave 10.14.3

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.