GraphQLerror:语法错误:预期名称,发现的“ $”

发布于 2025-02-11 14:04:23 字数 1356 浏览 2 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2025-02-18 14:04:23

这对我有用。我在错误的位置宣布变量。需要通过为突变命名,然后在里面声明它们来声明它们。
另外,需要在突变后删除额外的{'{'}。

client_id="7b8c903a-3402";  
text= "Some Text";

const preppedQuery = gql`
        mutation CreateMaps(
          $client_id: String!
          $text: String!
        ) {
          createMaps(
            input: [{ client_id: $client_id,  text: $text }]
          ) {
            info {
              nodesCreated
              relationshipsCreated
            }
            maps {
              uid
            }
          }
        }
      `;
      const gqlMutation = {
        mutation: gql`
          ${preppedQuery}
        `,
        variables: {
          client_id,
          text,
        },
      };
      const res = await client.mutate(gqlMutation);

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.

client_id="7b8c903a-3402";  
text= "Some Text";

const preppedQuery = gql`
        mutation CreateMaps(
          $client_id: String!
          $text: String!
        ) {
          createMaps(
            input: [{ client_id: $client_id,  text: $text }]
          ) {
            info {
              nodesCreated
              relationshipsCreated
            }
            maps {
              uid
            }
          }
        }
      `;
      const gqlMutation = {
        mutation: gql`
          ${preppedQuery}
        `,
        variables: {
          client_id,
          text,
        },
      };
      const res = await client.mutate(gqlMutation);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文