duanbai1370 2018-09-10 15:25
浏览 17
已采纳

突变参数作为对象

I'm using the Go implemenatation of GraphQL.

How would you configure a mutation so that it can receive arguments with more than 1 level? For exemple, here is the list of arguments I would like to pass to a mutation CreateUser:

mutation createUser($user: CreateUser!) {
  createUser(input: $user)
}

{
  "user": {
    "name": {
      "first": "John",
      "last": "Doe"
    },
    "email": "john@doe.com"
  }
}

(Notice that I dont want to use firstname and lastname but a name object instead)

And this is my (unsuccessful) attempt so far:

var CreateUserInput = graphql.FieldConfigArgument{
    "input": &graphql.ArgumentConfig{
        Description: "Input for creating a new user",
        Type: graphql.NewNonNull(graphql.NewInputObject(graphql.InputObjectConfig{
            Name: "CreateUser",
            Fields: graphql.InputObjectConfigFieldMap{
                "name": &graphql.InputObjectFieldConfig{
                    Type: graphql.NewNonNull(graphql.NewInputObject(graphql.InputObjectConfig{
                        Fields: graphql.InputObjectConfigFieldMap{
                            "first": &graphql.InputObjectFieldConfig{
                                Type: graphql.NewNonNull(graphql.String),
                            },
                            "last": &graphql.InputObjectFieldConfig{
                                Type: graphql.NewNonNull(graphql.String),
                            },
                        },
                    })),
                },
                "email": &graphql.InputObjectFieldConfig{
                    Type: graphql.NewNonNull(graphql.String),
                },
            },
        })),
    },
}

Apparently the subfields first and last are not recognized as this is what I get when I run this mutation:

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$user\" got invalid value {\"email\":\"john@doe.com\",\"name\":{\"first\":\"john\",\"last\":\"doe\"}}.
In field \"name\": In field \"first\": Unknown field.
In field \"name\": In field \"last\": Unknown field.",
      "locations": [
        {
          "line": 1,
          "column": 21
        }
      ]
    }
  ]
}

Is this even possible?

EDIT: See comments in the accepted answer for the solution.

  • 写回答

1条回答 默认 最新

  • du9826 2018-09-10 20:21
    关注

    This are my first ever lines of Go but I will try to convey what I think the problem is.

    First lets talk about the structure you want to be going for. I will use SDL here:

    type Mutation {
      createUser(user: CreateUser!): Boolean! # Maybe return user type here?
    }
    
    input CreateUser {
      name: CreateUserName!
      email: String!
    }
    
    input CreateUserName {
      first: String!
      last: String!
    }
    

    Okay now that we know that we need two input types lets get started!

    var CreateUserName = graphql.NewInputObject(graphql.InputObjectConfig{
        Name: "CreateUserName",
        Fields: graphql.InputObjectConfigFieldMap{
            "first": &graphql.InputObjectFieldConfig{
                Type: graphql.NewNonNull(graphql.String),
            },
            "last": &graphql.InputObjectFieldConfig{
                Type: graphql.NewNonNull(graphql.String),
            },
        },
    })
    
    var CreateUser = graphql.NewInputObject(graphql.InputObjectConfig{
        Name: "CreateUser",
        Fields: graphql.InputObjectConfigFieldMap{
            "name": &graphql.InputObjectFieldConfig{
                Type: graphql.NewNonNull(CreateUserName),
            },
            "email": &graphql.InputObjectFieldConfig{
                Type: graphql.NewNonNull(graphql.String),
            },
        },
    })
    

    Now all that should be left is adding the mutation field to your mutation object type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。