Apollo Graphql变异和重新获取返回空数据的查询

时间:2018-04-14 10:20:34

标签: reactjs graphql react-apollo apollostack

目前正在处理一个应用程序,其中我们有一个包含ID和列表名称的联系人列表。单击联系人列表名称将显示详细页面,其中列出了所述联系人列表名称中的所有联系人。

在此页面上,设置一个按钮以显示输入字段以添加更多联系人。然后使用突变添加这些接触。

完成变异后,联系人列表详细信息组件无法呈现并导致未定义错误(对于地图操作员),但刷新页面会清楚地显示联系人已成功添加。以下是代码段:

ManageContact组件,通过/ contacts / manage /:id route

访问
....

class ManageContact extends Component {
  state = {
    showAddForm: false
  };

  handleAddContactBtn = () => {
    this.setState({ showAddForm: !this.state.showAddForm });
  };

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

    return (
      <Query
        query={findContactList}
        variables={{ id: this.props.match.params.id }}
      >
        {({ loading, error, data:{findContactList} }) => {
          if (loading) return "loading";
          if (error) return "error";
          console.log(findContactList)
          const renderContacts = findContactList => {
            const { contacts } = findContactList;
            return contacts.map((contact, index) => {
              return (
                <Row key={contact._id}>
                  <Col s={3}>{contact.firstName}</Col>
                  <Col s={3}>{contact.lastName}</Col>
                  <Col s={3}>{contact.email}</Col>
                  <Col s={3}>
                    <Button>
                      <Icon>edit</Icon>
                    </Button>
                    <Button>
                      <Icon>delete_forever</Icon>
                    </Button>
                  </Col>
                </Row>
              );
            });
          };
          return (
            <Fragment>
              <h3>{findContactList.listName}</h3>
              <Row>
                <Button onClick={this.handleAddContactBtn} className="right">
                  <Icon>{showAddForm ? "close" : "person_add"}</Icon>
                </Button>
              </Row>
              {renderContacts(findContactList)}
              {showAddForm ? (
                <RecipientList contactListID={findContactList._id} onFinishCreateContact={() => this.handleAddContactBtn()}/>
              ) : null}
            </Fragment>
          );
        }}
      </Query>
    );
  }
}

export default ManageContact;

这里是RecipientList组件,其中执行添加联系人的变异:

...
import {
  createContactMutation,
  findContactList,
  userContactListQuery
} from "../../../graphql";

class RecipientList extends Component {
  state = {
    firstName: "",
    lastName: "",
    email: ""
  };

  createContact = async () => {
    const {
      createContactMutation,
      contactListID,
      onFinishCreateContact
    } = this.props;
    const { firstName, lastName, email } = this.state;
    onFinishCreateContact()
    try {
      await createContactMutation({
        variables: {
          contactListID,
          firstName,
          lastName,
          email
        },
        refetchQueries: [
          { query: userContactListQuery },
          { query: findContactList, variables:{ id: contactListID }}
        ]
      });


    } catch (err) {
      console.log(err);
    }

  };

  handleInputChange = e => {
    this.setState({ [e.target.name]: e.target.value });
  };

  renderRecipientAddForm = () => {
    const { firstName, lastName, email } = this.state;
    return (
      <Row>
        <Col s={3}>
          <Input
            label="First Name"
            name="firstName"
            value={`${firstName}`}
            onChange={this.handleInputChange}
          />
        </Col>
        <Col s={3}>
          <Input
            label="Last Name"
            name="lastName"
            value={`${lastName}`}
            onChange={this.handleInputChange}
          />
        </Col>
        <Col s={3}>
          <Input
            label="Email"
            name="email"
            value={`${email}`}
            onChange={this.handleInputChange}
          />
        </Col>
        <Col s={3}>
          <Button onClick={this.createContact}>+</Button>
        </Col>
      </Row>
    );
  };

  render() {
    return <span>{this.renderRecipientAddForm()}</span>;
  }
}

export default graphql(createContactMutation, {
  name: "createContactMutation"
})(RecipientList);

我一直在尝试使用refetchqueries,但它一直在失败。我知道当我在变异后控制日志时,我得到的数据是未定义的,这是ManageContact组件中地图操作员所需要的。

关于我做错了什么的想法?

0 个答案:

没有答案