Подтвердить что ты не робот

Мутация GraphQL без подзаголовка

Я хочу отправить запрос мутации graphql без подкатегории

mutation _ {
    updateCurrentUser(fullName: "Syava", email: "[email protected]")
}

и я получаю

{
  "errors": [
    {
      "message": "Field \"updateCurrentUser\" of type \"User\" must have a sub selection.",
      ... 
    }
  ]
}

добавить {id}, чтобы запрос выполнялся нормально, но я не хочу

Также код схемы

const userType = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    id: { type: new GraphQLNonNull(GraphQLString) },
    fullName: { type: GraphQLString },
    email: { type: GraphQLString },
  }),
});

type: userType,
  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);

    ...

    return user;
  },
4b9b3361

Ответ 1

Вы определяете тип поля как UserType. Несмотря на то, что это мутация, она по-прежнему следует тем же правилам и поведению, что и запрос. Поскольку UserType является типом объекта, для него требуются вложенные поля.

mutation _ {
  updateCurrentUser(fullName: "Syava", email: "[email protected]") {
    fullName
    email
  }
}
// would respond with { fullName: 'Syava', email: '[email protected]' }

Если вы не хотите, чтобы мутация возвращала пользователя, вы можете объявить его тип GraphQLBoolean, например, - скаляр и не имеет вложенных полей.

{
  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
  }
}