增加一个 GraphQL 客户端(Apollo 客户端) 安装 apollo-client, react-apollo, 和 graphql-tag。 $ npm install apollo-client react-apollo graphql-tag --save 打开 Redux 应用的入口文件 src/containers/AppContainer.js 将从 react-redux 中导出的 connect 方法替换成从 react-apollo 中导出的 connect 方法,同时将 mapQueriesToProps 作为参数传入。 import React from 'react' import { connect } from 'react-apollo' // NOTE: different connect! 渲染从 props 中传递下来的数据: import React from 'react' import { connect } from 'react-apollo' // NOTE: different
安装依赖 安装如下依赖: $ cd client $ npm i apollo-boost react-apollo graphql 其中 apollo-boost 是apollo client本身, react-apollo 是react视图层的集成,graphql 用于解析graphql的查询语句。 Component } from 'react'; import ApolloClient from 'apollo-boost'; import { ApolloProvider } from 'react-apollo
放到 index.js 然后 node 跑起来即可 浏览器直接打开 localhost:4000/graphql 就是 graphiql 的界面,这玩意算是半个 IDE 吧 Client 轮子:react-apollo /reducers' +import gql from "graphql-tag"; +import { ApolloProvider } from "react-apollo"; +import ApolloClient
使用apollo-client和react-apollo简化了前端代码对缓存和数据的管理。 灵活的查询方式方便我们在未来构建移动端以及内部应用。 在此基础上,我们进一步使用apollo-typed-components来给每项操作生成react-apollo组件,也就是ApolloComps.tsx文件中的GetTodosQuery组件、CreateTodoMutation Generated using apollo-typed-components */ import * as React from "react"; import { Mutation } from "react-apollo CreateTodoMutationClass是继承自react-apollo的Mutation组件的一个子类,它的构造函数两个入参类型就是CreateTodoType和CreateTodoVariables
Apollo 所需的库是 apollo-boost,react-apollo,react-apollo-hooks,graphql-tag和graphql。 apollo-boost 包含了查询 API 和在内存中缓存数据所需的工具, react-apollo 为React提供绑定, react-apollo-hooks 在 React Hook 中包装了 1yarn add apollo-boost react-apollo react-apollo-hooks graphql-tag graphql graphql-code-generator 用于自动化 将生成的目标文件夹更新为 src/generated/graphql.tsx (react-apollo 插件需要 .tsx)。 不要生成 introspection file。 ReactDOM from 'react-dom'; 3import ApolloClient from 'apollo-boost'; 4import { ApolloProvider } from 'react-apollo
那么前后端的架构就可以是 react-apollo,vue-apollo, 后端的话比如express-apollo,koa-apollo等等。