An example app using React, Apollo and Koa
An example web app using React, Apollo (GraphQL), Koa v.2.
This project was generated by create-react-app
and is not ejected.
Some settings are overwritten on runtime though.
passport
branch uses Passport to integrate authentication with
social services such as Google+, Facebook, etc., whereas master
branch uses
simple-oauth2 to authenticate with
GitHub.
Install required packages.
% yarn
Copy .env.default
to .env
and edit it to change server-side settings.
Copy src/config-default.json
to src/config.json
and edit it to change
client-side settings.
Launch the API server:
% yarn run server
Launch the development web server which serves the static client-side files:
% yarn start
Open the URL (default: http://localhost:3000
) in your browser.
The application code was generated by create-react-app.
See package.json for more information.
See package.json for more information.
The API server does not use DB and stores data on memory.
All changes will be cleared when you stop the server.
GraphQL querires, mutations and subscriptions are defined in src/graphql
as .graphql
files.
They can be used as persisted queries.
You can enable persisted queries by modifying config.json
.
{
"persistedQueries": true
}
You need to generate extracted_queries.json
by executing this.
% yarn run persistgraphql
Read the following article to know about persisted queries.
This example project uses subscriptions.
Select “GraphQL Subscription” tab in the header to see it.
Read the following article to know about subscriptions.
JWT access token and refresh token are
used to authenticate a client.
The access token is automatically refreshed by
WebClient
and Apollo client (including GraphQL subscription).
When the refresh token is expired, the user will be automatically signed out.
The WebSocket connection will also be authenticated with a JWT access token.
It will be reconnected on signing in/out to do the authentication.
There are two types of connection, authenticated and unauthenticated.
You can set the expiration on server-side by editing .env
file.
e.g.:
ACCESS_TOKEN_EXPIRES_IN=10s
REFRESH_TOKEN_EXPIRES_IN=2h
You need to sign out and sign in again to apply the new expiration.
It is also possible to set them in client-side localStorage if the server is
running on debugging mode.
Set devHeaders
as the following (JSON encoded string):
{ "X-ACCESS-TOKEN-EXPIRES-IN": "10s", "X-REFRESH-TOKEN-EXPIRES-IN": "2h" }
Setting devHeaders
is useful for testing.
See auth_token_test.js.
This project uses React Storybook.
to develop and check presentational components.
Launch the Storybook server:
% yarn run storybook
StoryShots is also used to enable
snapshot tests.
When you modify a component and the render result is changed, Jest reports an
error. If the shown result is intended, press u
to update the snapshot file.
Redux actions, reducers and logic are managed by
Ducks rules in src/ducks
directory.
Tests are stored in __tests__
directories under each component.
Git hooks are managed by Husky.
When you run yarn
in this repo, Git hooks will be automatically created.
If you have a problem, you can create them manually.
% node node_modules/husky/bin/install.js
Our Git hooks are defined in package.json
.