GraphQL-语法错误GraphQL请求(5:15)预期名称 [英] GraphQL - Syntax Error GraphQL request (5:15) Expected Name

查看:73
本文介绍了GraphQL-语法错误GraphQL请求(5:15)预期名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个graphQL API,它与查询配合得很好,但是对于突变却效果不佳:

I am trying to implement a graphQL API, it went well with queries but it's going not that well with mutations:

这是我使用 apollo-client graphql-tag 的基本突变:

Here is my basic mutation using apollo-client and graphql-tag:

import gql from 'graphql-tag'
const addNewPlace = (place) => {
    return client.mutate({
        mutation: gql`
        mutation {
          addNewPlace(
          input: {
            $title: String!
          }
          ) {
            place { title }
          }
        }
        `,
        variables: {
          title: place.title
        }
      })
  }

我在这里尝试使用变量.当将突变更改为如下所示时,它进行得很顺利,但这并不是正确的id处理方法.

Here I was trying to use variables. When changing the mutation to look like that one below, it is going smoothly however, it's not the right way to do id.

const addNewPlace = (place) => {
    return client.mutate({
        mutation: gql`
        mutation {
          addNewPlace(
          input: {
            title: "${place.title}"
          }
          ) {
            place { title }
          }
        }
        `
    })
}

知道我在哪里弄错了吗?

Any idea where I made my mistake?

推荐答案

使用变量时,需要执行三个步骤:

When using variables, there's three steps you need to take:

  1. 将变量添加到正在发送的请求中.在Apollo中,这是通过在传递给 mutate 的对象内指定一个 variables 属性来完成的.您写道:

  1. Add the variables to the request being sent. In Apollo, it's done by specifying a variables property inside the object you pass to mutate. You wrote:

变量:{title:place.title}

variables: { title: place.title }

这很好.我们正在将带有任何值的一些变量(称为title)连同请求一起发送到我们的服务器.此时,GraphQL甚至都不知道变量.

This is fine. We are sending some variable, called title, with whatever value, along with the request to our server. At this point, GraphQL doesn't even know about the variables.

在操作中声明变量.您不必命名您的操作,但这是一个好习惯:

Declare your variables inside your operation. You don't have to name your operation, but it's good practice to do so:

变异 AddNewPlace($ title:String) {

在这里,我们要告诉GraphQL我们包含了一个名为title的变量.您可以将其命名为任何东西(例如foo),只要它与您传递给#1中的变量prop的内容匹配即可.此步骤很重要,因为A)GraphQL需要了解变量,并且B)必须知道您要传入的变量的哪种类型.

Here, we are telling GraphQL we've included a variable called title. You could call it anything (foo for example), as long as it matched what you passed to the variables prop in #1. This step is important because A) GraphQL needs to know about the variable and B) it needs to know what type of variable you are passing in.

最后,在您的突变中包含变量,如下所示:

Finally, include the variable in your mutation, like this:

addNewPlace(输入:{title:$ title}){

addNewPlace(input: { title: $title }) {

请注意不要将步骤2中的变量定义与步骤3中的输入定义混淆.另外,我假设您的typeDefs包含某种 input 类型,例如AddNewPlaceInput.您不仅可以传递标题,还可以传递这样的对象:

Be careful not to mix up your variable definition in step #2 with your input definition in step #3. Also, I'm assuming your typeDefs include some kind of input type like AddNewPlaceInput. Rather than passing in just title, you can pass in an object like this:

variables: { input: { title: place.title } }

然后您的突变如下所示:

Then your mutation looks like this:

mutation AddNewPlace($input: AddNewPlaceInput) {
  addNewPlace(input: $input) {
    # fields returned by the mutation

我会强烈建议启用Graph i QL端点,以便在客户端实现查询和变异之前可以轻松地对其进行测试.

I would highly recommend enabling a GraphiQL endpoint so you can easily test your queries and mutations before implementing them on the client side.

最后,您可能需要检查并确保突变中要查询的字段与您的类型定义匹配.我只是在猜测,但是如果您的突变解析为Place类型,则无需放置 place {title} ,只需放置 title ,除非您的Place类型实际上有一个 place 字段.

Lastly, you may want to check and make sure the fields you are asking for in the mutation match your type definition. I'm just guessing here, but if your mutation resolves to a Place type, you wouldn't need to put place { title }, just title, unless your Place type actually has a place field.

这篇关于GraphQL-语法错误GraphQL请求(5:15)预期名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆