Apollo/GraphQL变异可以在GraphIQL中工作,但不能在客户端代码中工作?

Apollo/GraphQL变异可以在GraphIQL中工作,但不能在客户端代码中工作?,graphql,apollostack,Graphql,Apollostack,我已经在GraphIQL中正确地运行了这个突变: 查询 mutation($fromID: String!, $toID: String!, $msgText: String!){ createIM(fromID: $fromID, toID: $toID, msgText: $msgText){ fromID toID msgText } } …和查询变量 { "fromID": "1", "toID": "2", "msgText": "Test

我已经在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抛出错误:

消息:“网络错误:意外令牌<在JSON中的位置0”

我遗漏了什么?

请试试这个。。 您应该使用mutate for mutations而不是query

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 }) => {

查看chrome网络选项卡-你能看到其中的请求吗?它命中的URL是什么,返回值是什么?请求URL:。推荐人:。请求有效负载:{“查询”:“查询变异($fromID:String!,$toID:String!,$msgText:String!){\n createIM(fromID:$fromID,toID:$toID,msgText:$msgText:$msgText){\n fromID\n toID\n msgText\n}\n}\n,“变量”{“fromID:“S9fBtrxQCPacjnon2”,“toID:“572BDDAC4ECBB0FFE37FDF”,“msgText:“Testing 123”},“operationName:“变异”}。在哪里可以找到返回值?在包含引用代码的文件中,我通过从“apollo客户端”导入apollo客户端来访问apollo客户端;const client=新客户端()。这是否足以访问先前定义的解析程序?