• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

yatsu/react-apollo-koa-example: An example app using React, Apollo and Koa

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

yatsu/react-apollo-koa-example

开源软件地址:

https://github.com/yatsu/react-apollo-koa-example

开源编程语言:

JavaScript 96.0%

开源软件介绍:

react-apollo-koa-example

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.

screen image

Setup

Install Node.js and Yarn.

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.

Usage

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.

Frameworks/Libraries

Client-side

The application code was generated by create-react-app.

See package.json for more information.

Server-side (the API server)

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.

Tests

Repo and code management

GraphQL/Apollo

Persisted Queries

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.

Subscriptions

This example project uses subscriptions. Select "GraphQL Subscription" tab in the header to see it.

Read the following article to know about subscriptions.

Authentication

JWT access token and refresh token are used to authenticate a client.

  • Access token
    • Expires in 2 hours (default)
  • Refresh token
    • Expires in 60 days (default)
    • It is used only for refreshing the access token

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.

Storybook

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.

Code Organization

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

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
thzt/scheme-koans发布时间:2022-07-09
下一篇:
ArthurYung/koa2-myblog: 基于koa2 mongodb的个人网站后台代码发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap