尝试使用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
}
}
`;
最佳答案
我想通了-这很简单!不要在突变周围加上括号,而应改为:
const CREATE_AUTHOR = gql`
mutation createAuthor($firstName: String, $lastName: String) {
createAuthor(firstName: $firstName, lastName: $lastName) {
firstName
lastName
}
}
`;
关于javascript - 发送变异时出现GraphQLError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51030494/