无分段的GraphQL突变

无分段的GraphQL突变,graphql,graphql-js,Graphql,Graphql Js,我想发送没有子节的graphql变异请求 突变{ updateCurrentUser(全名:“Syava”,电子邮件:fake@gmail.com") } 我越来越 { “错误”:[ { “消息”:“用户”类型的“字段”updateCurrentUser“必须具有子选择。”, ... } ] } 将{id}添加到请求可以正常工作,但我不希望 还有模式代码 将字段的类型定义为UserType。即使它是一个变异,它仍然遵循与查询相同的规则和行为。因为UserType是对象类型,所以它需要嵌套

我想发送没有子节的graphql变异请求

突变{
updateCurrentUser(全名:“Syava”,电子邮件:fake@gmail.com")
}
我越来越

{
“错误”:[
{
“消息”:“用户”类型的“字段”updateCurrentUser“必须具有子选择。”,
... 
}
]
}
将{id}添加到请求可以正常工作,但我不希望

还有模式代码


将字段的类型定义为UserType。即使它是一个变异,它仍然遵循与查询相同的规则和行为。因为UserType是对象类型,所以它需要嵌套字段

mutation _ {
  updateCurrentUser(fullName: "Syava", email: "fake@gmail.com") {
    fullName
    email
  }
}
// would respond with { fullName: 'Syava', email: 'fake@gmail.com' }
{
  type: GraphQLBoolean,
  args: {
    fullName: { type: GraphQLString },
    email: { type: new GraphQLNonNull(emailType) },
    password: { type: GraphQLString },
  },
  resolve: async (root, { fullName, email, password }, { rootValue }) => {
    const user = await User.findById(rootValue.req.user.id);
    user.fullName = fullName;
    user.password = password; // or hashed to not store plain text passwords
    return user.save(); // assuming save returns boolean; depends on the library you use
  }
}
如果不想让变异返回用户,可以将其类型声明为GraphQLBoolean,例如,它是标量,没有任何嵌套字段

mutation _ {
  updateCurrentUser(fullName: "Syava", email: "fake@gmail.com") {
    fullName
    email
  }
}
// would respond with { fullName: 'Syava', email: 'fake@gmail.com' }
{
  type: GraphQLBoolean,
  args: {
    fullName: { type: GraphQLString },
    email: { type: new GraphQLNonNull(emailType) },
    password: { type: GraphQLString },
  },
  resolve: async (root, { fullName, email, password }, { rootValue }) => {
    const user = await User.findById(rootValue.req.user.id);
    user.fullName = fullName;
    user.password = password; // or hashed to not store plain text passwords
    return user.save(); // assuming save returns boolean; depends on the library you use
  }
}

通过添加graphql模式示例或graphql js类型定义,您能更好地理解这个答案吗?如果从代码的多个部分调用此变异,可能有时候响应应该是“userType”类型,有时候不需要响应。是否有一种方法可以获得可选的“userType”响应,并根据客户端约束子选择数据??使用
type:userType
,服务器表示它返回一个可能为空的用户(使用
type:GraphQLNonNull(userType)
,用户将被保证在场)。无论如何,服务器需要指定如何获取用户。如果在某些情况下它可以是
null
,则客户端在访问其属性之前应确保该属性根据客户端约束,客户端无法约束返回类型。您可以做的是请求
updateCurrentUser(…){id}
并忽略返回的内容。但是你不能要求没有属性的
updateCurrentUser(…)