发送突变时的 GraphQLError
GraphQLError when sending mutation
我在尝试使用 Apollo 客户端时收到以下语法错误
GraphQLError: Syntax Error: Expected Name, found $
我发送的查询是这样的
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
查看 apollo 文档有什么不对的地方
https://www.apollographql.com/docs/react/essentials/mutations.html#calling-mutations
我相信他们的示例与我的实现相匹配,或者我可能误解了用法
const ADD_TODO = gql`
mutation addTodo($type: String!) {
addTodo(type: $type) {
id
type
}
}
`;
我认为你在这里漏掉了一个逗号:mutation createAuthor($firstName: String $lastName: String) {
应该是mutation createAuthor($firstName: String, $lastName: String) {
您可能需要将变量发送到变异组件,例如:
<Mutation mutation ={CREATE_AUTHOR} variables={{"firstName": firstName, "lastName": lastName}}>
更新
虽然这不是您要找的,但这是我此时执行 Apollo 突变的方法。
一个属于React组件的函数class:
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>
我想通了——很简单!不要用括号括起你的突变——它应该看起来像这样:
const CREATE_AUTHOR = gql`
mutation createAuthor($firstName: String, $lastName: String) {
createAuthor(firstName: $firstName, lastName: $lastName) {
firstName
lastName
}
}
`;
我在尝试使用 Apollo 客户端时收到以下语法错误
GraphQLError: Syntax Error: Expected Name, found $
我发送的查询是这样的
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
查看 apollo 文档有什么不对的地方
https://www.apollographql.com/docs/react/essentials/mutations.html#calling-mutations
我相信他们的示例与我的实现相匹配,或者我可能误解了用法
const ADD_TODO = gql`
mutation addTodo($type: String!) {
addTodo(type: $type) {
id
type
}
}
`;
我认为你在这里漏掉了一个逗号:mutation createAuthor($firstName: String $lastName: String) {
应该是mutation createAuthor($firstName: String, $lastName: String) {
您可能需要将变量发送到变异组件,例如:
<Mutation mutation ={CREATE_AUTHOR} variables={{"firstName": firstName, "lastName": lastName}}>
更新
虽然这不是您要找的,但这是我此时执行 Apollo 突变的方法。
一个属于React组件的函数class:
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>
我想通了——很简单!不要用括号括起你的突变——它应该看起来像这样:
const CREATE_AUTHOR = gql`
mutation createAuthor($firstName: String, $lastName: String) {
createAuthor(firstName: $firstName, lastName: $lastName) {
firstName
lastName
}
}
`;