首页 文章

在GraphQL Mutate函数中将ID作为正确的变量类型传递

提问于
浏览
1

我'm trying to edit an item using GraphQL by passing in arguments using this.props.mutate. I' m得到以下错误... Error: GraphQL error: Variable $id of required type ID! was not provided. 因此,问题在于我将错误的ID类型传递给mutate函数 . 任何人都知道如何将ID类型作为正确的类型传递给mutate函数?或者我可以将ID类型从字符串转换为正确的类型以作为变量传递给mutate函数吗?谢谢

我正在使用本地组件状态来保存值以预先填写表单,以防您想知道我为什么使用本地状态

import UPDATE_CHAT_MUTATIONS from '../graphql/mutations/updateChat';

class EditChat extends React.Component {
  state = {
    text: '',
    id: ''
  }

  componentWillMount() {
    this._onEditLoad()
  }

  _onEditLoad = () => {
    const chat = this.props.navigation.state.params;
    this.setState({ text: chat.text, id: chat._id })
  }

  _onChangeText = text => this.setState({ text });

  _onEditPress = async () => {
    const { id, text } = this.state;
    await this.props.mutate({
      variables: {
        _id: id,
        text
      }
    });

    Keyboard.dismiss();
    this.props.navigation.goBack(null);
  }

1 回答

  • 3

    我设法让它工作!我在客户端的graphql突变上犯了一个错误 . 以下是有效的代码!!希望这将有助于那些面临同样问题的人 . 干杯

    import { gql } from 'react-apollo';
    
    export default gql`
      mutation updateChat($_id: ID!, $text: String!) {
        updateChat(_id: $_id, text: $text) {
          text
          _id
          updatedAt
        }
      }
    `;
    

相关问题