graphql突变给出语法错误:预期名称
问题描述:
我想用变量实现突变。但我得到以下错误:graphql突变给出语法错误:预期名称
"Syntax Error GraphQL request (3:22) Expected Name, found $
2: mutation {
3: createProperty($property) {
^
4: id
"
我的模式绝对不说的名字什么,这就是为什么我认为这个错误就是这么奇怪。我也不会去想graphql的单证/阿波罗是很好。
从客户端调用突变:
const property = {
title: 'First house',
cost: 849,
bedrooms: 3,
bathrooms: 2,
car_spaces: 1,
house_size: 60,
};
const createPropertyQuery =
graphql(gql`
mutation {
createProperty($property) {
id
}
}
`, {
options: {
variables: {
property,
},
},
});
const { data } = await apolloClient.query({
query: createPropertyQuery,
});
模式:
type Property {
title: String!
cost: Float
user: User
bedrooms: Int!
bathrooms: Int!
car_spaces: Int!
house_size: Int!
}
input propertyInput {
title: String!
cost: Float
bedrooms: Int!
bathrooms: Int!
car_spaces: Int!
house_size: Int!
}
type RootMutation {
createProperty (
property: propertyInput
): Property
}
答
你首先应该提到的参数的名字!
mutation CreatePropertyMutatuin($property: propertyInput){
createProperty(property: $property) {
id
}
}
您需要修改您的查询并实际声明您在操作定义中发送的变量。请参阅[本答案](https://stackoverflow.com/a/45131876/6024220)以获取详细解释。 –
[GraphQL - Syntax Error GraphQL request(5:15)Expected Name]可能重复(https://stackoverflow.com/questions/45130386/graphql-syntax-error-graphql-request-515-expected-name) –