在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):leebenson/reactql开源软件地址(OpenSource Url):https://github.com/leebenson/reactql开源编程语言(OpenSource Language):TypeScript 97.2%开源软件介绍(OpenSource Introduction):Universal front-end React + GraphQL starter kit, written in Typescript. FeaturesFront-end stack
Server-side rendering
Real-time
Code optimisation
Styles
Production-ready
Developer support
Quick startGrab and unpack the latest version, install all dependencies, and start a server:
Your development server is now running on http://localhost:3000 Building GraphQL HOCsBy default, your GraphQL schema lives in schema/schema.graphql To create fully Typescript-typed Apollo React HOCs based on your schema, simply put the query in a
You can then import the query like we do in the HackerNews demo component: // Query to get top stories from HackerNews
import { GetHackerNewsTopStoriesComponent } from "@/graphql"; And use it as follows: <GetHackerNewsTopStoriesComponent>
{({ data, loading, error }) => (...)}
</GetHackerNewsTopStoriesComponent> To get access to the underlying See GraphQL Code Generator for more details on how it works. You can also edit codegen.yml in the root to point to a remote schema, or change the file location. Development modeDevelopment mode offers a few useful features:
To get started, simply run:
A server will be started on http://localhost:3000 Production modeIn production mode, the following happens:
To build and run for production, use:
Files will be generated in Clean the cached production build with Build modeIf you only want to build assets and not actually run the server, use:
This is used in the Dockerfile, for example, to pre-compile assets and ensure faster start-up times when spawning a new container. Static bundling for client-only SPAsIf you're targeting a client-only SPA and hosting statically, you probably don't want to run a Koa web server to handle HTTP requests and render React. Instead, you can use static mode, which produces the client-side JS, CSS and assets files, along with an You can then upload the contents of that folder wherever you like - et voila, you'll have a working client-side Single Page App! There are two static modes available -- for dev and production: ### Development (hot-code reload) Just like the full-stack version, dev mode gives you hot code reloading, so changes to your local files will be pushed to the browser. To activate static dev mode, just run:
Your client-side SPA will be online at http://localhost:3000, just like normal. ### Production (static deployment) To build your client-side files ready for production deployment, run:
You'll get everything in that 'regular' building provides you with plus a
Modifying the |
Command | What it does |
---|---|
npm run build:production |
Builds production-ready client/server bundles, but doesn't start a server. |
npm run build:static |
Builds production-ready client bundle and index.html ; ignores server bundling. |
npm run clean |
Removes the dist folder, and any previously built client/server bundle. |
npm run dev |
Runs a univeral dev server in memory; auto restarts on code changes and uses hot-code reload in the browser. Does not output anything to dist . |
npm run dev:static |
Runs a client-only dev server using [src/views/static.html] as the template; full hot-code reload. Also doesn't output anything to dist . |
npm run production |
Builds and runs a production-ready client/server bundle. If previously built, will re-use cached files automatically (run npm run clean to clear cache.) |
npm run production:clean |
Same as above, but cleans dist first to ensure a fresh re-build. |
npm start |
Shortcut for npm run dev . |
The important stuff is in src.
Here's a quick run-through of each sub-folder and what you'll find in it:
src/components - React components. Follow the import flow at root.tsx to figure out the component render chain and routing. I've included an example component that shows off some Apollo GraphQL and MobX features, including incrementing a local counter and pulling top news stories from Hacker News (a live GraphQL server endpoint.)
src/entry - The client and server entry points, which call on src/components/root.tsx to isomorphically render the React chain in both environments.
src/global - A good place for anything that's used through your entire app, like global styles. I've started you off with a styles.ts that sets globally inlined Emotion CSS, as well as pulls in a global .scss
file -- to show you how both types of CSS work.
src/lib - Internal libraries/helpers. There's an apollo.ts which builds a universal Apollo Client. Plus, Koa middleware to handle hot-code reloading in development and some other Webpack helpers.
src/queries - Your GraphQL queries. There's just one by default - for pulling the top stories from Hacker News to display in the example component.
src/runner - Development and production runners that spawn the Webpack build process in each environment.
src/views - View components that fall outside of the usual React component chain, for use on the server. In here, ssr.tsx takes care of rendering the root HTML that's sent down the wire to the client. Note this is also a React component - your whole app will render as React! - and static.html serves as a template for rendering a client-side SPA. Update it as needed.
src/webpack - The Webpack 4 configuration files that do the heavy lifting to transform our Typescript code, images and CSS into optimised and minified assets that wind up in the dist
folder at the root. Handles both the client and server environments.
You'll also find some other useful goodies in the root...
.env - Change your GRAPHQL
server endpoint, WS_SUBSCRIPTIONS=1
for built-in WebSocket support, HOST
if you want to bind the server to something other than localhost, and LOCAL_STORAGE_KEY
to set the root key for saving MobX state locally in the client for automatic re-loading in a later session.
.nvmrc - Specify your preferred Node.js version, for use with NVM and used by many continuous deployment tools. Defaults to v12.2.0
codegen.yml - Settings for GraphQL Code Generator (which you can run with npm run gen:graphql
to generate types/HOCs based on your GraphQL queries/mutations.)
netlify.toml - Build instructions for fast Netlify deployments. Tip: To quickly deploy a demo ReactQL app, click here.
types - Some basic types that allow you to import fonts, images, CSS/SASS/LESS files, and allow use of the global SERVER
boolean and GRAPHQL
endpoint data in your IDE.
Typescript configuration via tsconfig.json
A sample multi-build Dockerfile based on Node 11.8 and Alpine, for quickly deploying your code base to production.
Get the latest updates by following us on Twitter: https://twitter.com/reactql
Join the ReactQL slack channel here.
Watch my free 45 minute YouTube video, for a live coding walk-through of putting together a GraphQL server with a database. Learn how to write queries, mutations and handle nested/related data.
I'm a full-stack developer with 20+ years experience. As well as 9 years hands-on dev with Node.js, I'm fluent in Python, Go, SQL and NoSQL. I specialise in building robust, scalable products from scratch, and helping you deploy fast.
If you're looking for a senior developer who can help you get your product out the door quickly, reach me at [email protected]. I'm occasionally available to take on remote contracts when I'm not working on my own projects.
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论