1

我在http://localhost:8080/graphiql的 GraphIQL 中有这个突变正常工作:

询问

mutation($fromID: String!, $toID: String!, $msgText: String!){
  createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
    fromID
    toID
    msgText
  }
}

...和查询变量

{
  "fromID": "1",
  "toID": "2",
  "msgText": "Test from GraphIQL #3. It's working!!!"
}

现在我需要在代码中实现它。

客户代码

sendInstantMsg(){
    const {textToSend} = this.refs;
    const {toID} = this.props;

    const fromID = Meteor.userId();
    const msgText = trimInput(textToSend.getValue());

    client.query({
        query: gql`
            query mutation($fromID: String!, $toID: String!, $msgText: String!){
                createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
                    fromID
                    toID
                    msgText
                }
            }
        `,
        variables: {
            fromID: fromID,
            toID: toID,
            msgText: msgText
        },
        forceFetch: false,
    }).then(({ data }) => {
        console.log('got data', data);
    }).catch((error) => {
        console.log('there was an error sending the query', error);
    });
}

查询变量(fromID、toID 和 msgText)按预期进入函数,但 Apollo 抛出错误:

message: "Network error: Unexpected token < in JSON at position 0"

我错过了什么?

4

1 回答 1

2

请改用这个.. 你应该使用 mutate 来进行突变而不是查询..

client.mutate({
            mutation: gql`
                mutation createIM($fromID: String!, $toID: String!, $msgText: String!){
                    createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
                        fromID
                        toID
                        msgText
                    }
                }
            `,
            variables: {
                fromID: fromID,
                toID: toID,
                msgText: msgText
            },
            forceFetch: false,
        }).then(({ data }) => {
于 2016-09-15T19:34:16.943 回答