react-apollo入门教程

声明: 转载请注明出处
官网

1.简介

react-apollo是Apollo数据栈在React环境的集成包之一,要在React里使用Apollo数据栈前端至少需要三个包:apollo-client、graphql-tag和react-apollo,apollo-client用于连接到Apollo后端,graphql-tag用于编写GraphQL查询,react-apollo用于执行GraphQL查询并将结果传给React的展示组件。

2.创建client

//默认client会将url设为 `/graphql`
import { ApolloClient } from 'react-apollo';
const client = new ApolloClient();
//当你需要改变graphql server的url的时候,需使用networkInterface
import { ApolloClient, createNetworkInterface } from 'react-apollo';
const networkInterface = createNetworkInterface({
  uri: 'http://api.example.com/graphql'
});
const client = new ApolloClient({
  networkInterface: networkInterface
);

3.创建provider,用于连接client和component

import { ApolloClient, ApolloProvider } from 'react-apollo';
const client = new ApolloClient();
ReactDOM.render(
  <ApolloProvider client={client}>
    <MyAppComponent />
  </ApolloProvider>,
  document.getElementById('root')
)

4.使用

(1)Queries

1.1基本查询
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
class Profile extends Component { ... }
// We use the gql tag to parse our query string into a query document
const CurrentUserForLayout = gql`
  query CurrentUserForLayout {
    currentUser {
      login
      avatar_url
    }
  }
`;
const ProfileWithData = graphql(CurrentUserForLayout)(Profile)
1.2 参数查询
const CurrentUserForLayout = gql`
  query CurrentUserForLayout($avatarSize: Int!) {
    currentUser {
      login
      avatar_url(avatarSize: $avatarSize)
    }
  }
`;
const ProfileWithData = graphql(CurrentUserForLayout, {
  options: { variables: { avatarSize: 100 } },
})(Profile);

(2)Mutation

2.1基本修改
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { gql, graphql } from 'react-apollo';
class NewEntry extends Component { ... }
const submitRepository = gql`
  mutation submitRepository {
    submitRepository(repoFullName: "apollographql/apollo-client") {
      createdAt
    }
  }
`;
const NewEntryWithData = graphql(submitRepository)(NewEntry);
2.2传参修改
//被包裹的组件会传递一个mutate作为props传递过去,可以用来传递mutation操作参数,如:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { gql, graphql } from 'react-apollo';
class NewEntry extends Component {
  onClick() {
    this.props.mutate({
      variables: { repoFullName: 'apollographql/apollo-client' }
    })
      .then(({ data }) => {
        console.log('got data', data);
      }).catch((error) => {
        console.log('there was an error sending the query', error);
      });
  }
  render() {
    return <div onClick={this.onClick.bind(this)}>Click me</div>;
  }
}
const submitRepository = gql`
  mutation submitRepository($repoFullName: String!) {
    submitRepository(repoFullName: $repoFullName) {
      createdAt
    }
  }
`;
const NewEntryWithData = graphql(submitRepository)(NewEntry);

//也可以对mutate根据需要进行自己的函数封装
const NewEntryWithData = graphql(submitRepository, {
  props: ({ mutate }) => ({
    submit: (repoFullName) => mutate({ variables: { repoFullName } }),
  }),
})(NewEntry);

//这时在component中可直接调用submit
const NewEntry = ({ submit }) => (
  <div onClick={() => submit('apollographql/apollo-client')}>
    Click me
  </div>
);

(3)使用compose

很多时候我们的页面中存在多个queries和mutation操作,这时候我们需要使用compose来进行组合
import { compose } from 'react-apollo';
const ComponentWithMutations = compose(
  graphql(submitNewUser, { name: 'newUserMutation' }),
  graphql(submitRepository, { name: 'newRepositoryMutation' })
)(Component);

5.执行Mutation后进行状态更新

5.1 refetchQueries
refetchQueries是用来更新缓存的简单方法,当你在执行完一次mutation操作后,可能会有一个或多个状态被影响,这时可以指定一个或多个需要执行的queries来刷新这部分store
mutate({
  //... insert comment mutation
  refetchQueries: [{
    query: gql`
      query updateCache($repoName: String!) {
        entry(repoFullName: $repoName) {
          id
          comments {
            postedBy {
              login
              html_url
            }
            createdAt
            content
          }
        }
      }
    `,
    variables: { repoFullName: 'apollographql/apollo-client' },
  }],
})
5.2 update
import CommentAppQuery from '../queries/CommentAppQuery';
const SUBMIT_COMMENT_MUTATION = gql`
  mutation submitComment($repoFullName: String!, $commentContent: String!) {
    submitComment(repoFullName: $repoFullName, commentContent: $commentContent) {
      postedBy {
        login
        html_url
      }
      createdAt
      content
    }
  }
`;
const CommentsPageWithMutations = graphql(SUBMIT_COMMENT_MUTATION, {
  props({ ownProps, mutate }) {
    return {
      submit({ repoFullName, commentContent }) {
        return mutate({
          variables: { repoFullName, commentContent },
          update: (store, { data: { submitComment } }) => {
            // 从CommentAppQuery 中读取出查询得到的缓存数据
            const data = store.readQuery({ query: CommentAppQuery });
           // 将mutation的结果放入查询得到的数据中
            data.comments.push(submitComment);
            // 将数据重新写入缓存中,实现数据的更新            
            store.writeQuery({ query: CommentAppQuery, data });         
          },
        });
      },
    };
  },
})(CommentsPage);
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要入门react-native,首先你需要安装react-native-image-to-pdf库。你可以通过以下命令进行自动安装: $ npm install react-native-image-to-pdf --save 或者 $ yarn add react-native-image-to-pdf 然后,你需要将这个库链接到你的项目中,如果是iOS项目,在Xcode的项目导航器中,右键单击项目,选择"Link Binary With Libraries"选项。 接下来,你需要导入React和View模块,创建一个React组件,并实现render方法。在render方法中,使用View组件来布局你的界面。比如,你可以使用flex属性控制子组件的布局和比例。 最后,你需要注意在使用Chrome调试时无法观测到React Native中的网络请求。你可以使用第三方的调试工具,如react-native-debugger,或者使用抓包工具如Charles、Fiddler来进行观测。同时,你还需要处理服务器的响应数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [react-native-image-to-pdf:react-native插件可将图像转换为PDF](https://download.csdn.net/download/weixin_42118701/19074635)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ReactNative入门](https://blog.csdn.net/SeaState/article/details/113697867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值