GraphQLerror:语法错误:预期名称,发现的“ $”
我是GraphQl的新手,在尝试运行React应用程序后,我会收到以下错误:
我会遇到错误:GraphQlerror:语法错误:预期名称,找到了“ $”。
这是我的GraphQl查询/突变。
这很好:
gqlMutation = {
mutation: gql`
mutation{
createMaps(input: [{
client_id: "7b8c903a-3402"
text: "Some Text"
}])
{
info{
nodesCreated
relationshipsCreated
}
Maps{
uid
}
}
}`
}
const res = await client.mutate(gqlMutation);
但是当我尝试通过变量时,它会引发错误
client_id="7b8c903a-3402";
text= "Some Text";
const preppedQuery =`
mutation{
createMaps(input: [{
$client_id: String!,
$text: String!
}])
{
info{
nodesCreated
relationshipsCreated
}
Maps{
uid
}
}
}`
}
const gqlMutation = {
mutation: gql`${preppedQuery}`,
variables:
{
client_id,
text
}
};
const res = await client.mutate(gqlMutation);
I am new to graphQL and after trying to run a React app, I get the following error:
I'm getting the error: GraphQLError: Syntax Error: Expected Name, found "$".
Here are my graphql queries/mutations.
This works fine:
gqlMutation = {
mutation: gql`
mutation{
createMaps(input: [{
client_id: "7b8c903a-3402"
text: "Some Text"
}])
{
info{
nodesCreated
relationshipsCreated
}
Maps{
uid
}
}
}`
}
const res = await client.mutate(gqlMutation);
But when I try to pass variables then it throws an error
client_id="7b8c903a-3402";
text= "Some Text";
const preppedQuery =`
mutation{
createMaps(input: [{
$client_id: String!,
$text: String!
}])
{
info{
nodesCreated
relationshipsCreated
}
Maps{
uid
}
}
}`
}
const gqlMutation = {
mutation: gql`${preppedQuery}`,
variables:
{
client_id,
text
}
};
const res = await client.mutate(gqlMutation);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用。我在错误的位置宣布变量。需要通过为突变命名,然后在里面声明它们来声明它们。
另外,需要在突变后删除额外的{'{'}。
This worked for me. I was declaring variables in the wrong place. Need to declare them by giving a name to Mutation and then declare them inside.
Also, need to remove extra '{' after mutation and '}' at the end.