发送变异时出现GraphQLError

时间:2018-06-25 19:17:38

标签: javascript graphql apollo react-apollo

尝试使用Apollo客户端时出现以下语法错误

  

GraphQLError:语法错误:预期名称,找到$

我发送的查询就像

const CREATE_AUTHOR = gql`
  {
    mutation createAuthor($firstName: String, $lastName: String) {
      createAuthor(firstName: $firstName, lastName: $lastName) {
        firstName
        lastName
      }
    }
  }
`;

我在服务器上的类型定义是这样定义的

//...
  type Mutation {
    createAuthor(firstName: String! lastName: String!): Author
    updateAuthor(_id: String firstName: String lastName: String): Author
    deleteAuthor(_id: String): Author
  }

//...

我的问题是我使用gql查看阿波罗文档是否正确

  

https://www.apollographql.com/docs/react/essentials/mutations.html#calling-mutations

他们的示例与我认为的实现相符,或者我可能会误解用法

const ADD_TODO = gql`
  mutation addTodo($type: String!) {
    addTodo(type: $type) {
      id
      type
    }
  }
`;

3 个答案:

答案 0 :(得分:2)

我想通了-这很简单!请勿在突变周围加上括号,而应改为:

const CREATE_AUTHOR = gql`
  mutation createAuthor($firstName: String, $lastName: String) {
    createAuthor(firstName: $firstName, lastName: $lastName) {
      firstName
      lastName
    }
  }
`;

答案 1 :(得分:0)

我认为您在这里缺少逗号:<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">

应该为mutation createAuthor($firstName: String $lastName: String) {

答案 2 :(得分:0)

您可能需要将变量发送到突变组件,例如:

<Mutation mutation ={CREATE_AUTHOR} variables={{"firstName": firstName, "lastName": lastName}}>

更新

尽管这并不是您要找的东西,但这是我目前做阿波罗突变的方法。

属于React组件类的函数:

sendInstantMsg(createIM) {
    const textToSendElem = document.getElementById("textToSend");
    const textToSend = textToSendElem.value;

    const {toID} = this.props;
    const fromID = Meteor.userId();
    const msgText = trimInput(textToSend);

    createIM({
        variables: {
            "fromID": fromID,
            "toID": toID,
            "msgText": msgText
        },
        optimisticResponse: {
            __typename: 'Mutation',
            createIM: {
                __typename: 'instant_message',
                id: -1, 
                fromID: fromID,
                toID: toID,
                msgText: msgText,
                createdAt: +new Date
            },
        },
        update: (cache, {data: {createIM}}) => {
            let cacheData = cache.readQuery({query: GETIMS_QUERY, variables: {"fromID": fromID, "toID": toID}});
            let instant_message = cacheData.instant_message;
            if (!isDuplicateObject(createIM, instant_message)) {
                instant_message.push(createIM);

                cache.writeQuery({
                    query: GETIMS_QUERY,
                    data: {instant_message},
                    variables: {"fromID": fromID, "toID": toID}
                });
            }
            textToSendElem.value = "";
            scrollToBottomOfTextMsgs();
        }
    })
}

在渲染功能中:

<Mutation
    mutation={CREATE_IM_MUTATION}
>
    {(createIM, {data}) => (
        <RaisedButton
            id="sendMsgButton"
            label="SEND"
            style={styles.makeApptButton}
            secondary={true}
            onClick={() => this.sendInstantMsg(createIM)}
        />
    )}
</Mutation>