javascript - react Apollo : Dynamically update GraphQL query from Component state

标签 javascript reactjs graphql react-apollo

我有一个组件,它使用 react-apollo 装饰器语法显示 GraphQL 查询的结果。查询接受一个参数,我想根据组件状态动态设置该参数。

考虑以下简化示例:

import * as React from ‘react’;
import { graphql } from ‘react-apollo’;
import gql from ‘graphql-tag’;

const myQuery = gql`
    query($active: boolean!) {
        items(active: $active) {

        }
    }
`;

@graphql(myQuery)
class MyQueryResultComponent extends React.Component {
    public render() {
        return <div>
            <input type=“checkbox” /* other attributes and bindings */ />
            {this.props.data.items}
        <div>;
    }
}

单击复选框时,我想重新提交查询,根据复选框的状态在 myQuery 中动态设置 active 属性。为简洁起见,我省略了复选框的处理程序和绑定(bind),但如何才能在状态更改时重新提交查询?

最佳答案

创建一个新组件,它将具有从父组件传递的 prop active

此过程在 react-apollo 文档的部分中有很好的解释: Computing from props

根据您的示例和您的要求,我制作了一个演示,该演示托管在 codesandbox 上并使用 GraphQL API。

演示:https://codesandbox.io/embed/PNnjBPmV2

Data.js

import React from 'react';
import PropTypes from 'prop-types';
import gql from 'graphql-tag';
import { graphql } from 'react-apollo';

const Data = ({ data }) =>
  <div>
    <h2>Data</h2>
    <pre style={{ textAlign: 'left' }}>
      {JSON.stringify(data, undefined, 2)}
    </pre>
  </div>;

Data.propTypes = {
  active: PropTypes.bool.isRequired,
};

const query = gql`
  query SearchAuthor($id: Int!) {
    author(id: $id) {
      id
      firstName
      lastName
    }
  }
`;

export default graphql(query, {
  options(ownProps) {
    return {
      variables: {
        // This is the place where you can 
        // access your component's props and provide
        // variables for your query
        id: ownProps.active ? 1 : 2,
      },
    };
  },
})(Data);

App.js

import React, { Component } from 'react';
import Data from './Data';

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      active: false,
    };

    this.handleChange = this.handleChange.bind(this);
  }

  handleChange() {
    this.setState(prevState => ({
      active: !prevState.active,
    }));
  }

  render() {
    const { active } = this.state;

    return (
      <div>
        <h1>App</h1>
        <div>
          <label>
            <input
              type="checkbox"
              checked={active}
              onChange={this.handleChange}
            />
            If selected, fetch author <strong>id: 1</strong>
          </label>
        </div>
        <Data active={active} />
      </div>
    );
  }
}

export default App;

关于javascript - react Apollo : Dynamically update GraphQL query from Component state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44808291/

相关文章:

javascript - 为什么我可以调用在组件下面声明的函数?

javascript - 无法从 ReactJS 的 render 方法中将类方法传递给 onClick ?

GraphQL 订阅字段无法访问解析器的上下文

vue.js - Vue Apollo : How can I query GraphQL using an object as Input argument?

javascript - 如何使用 javascript 以通用方式将 Html.Kendo().Grid 数据导出到 excel?

Javascript 'click' 按钮只能工作一次,必须刷新页面

javascript - youtube-dl 显示错误 : Command failed with exit code 1

javascript - 使用javascript匹配来提取字符串的一部分

javascript - ReactJS 如何实现显示更多按钮?

node.js - Mongodb graphQL和Feather js通过_id获取数据在某些情况下返回null