Написание вложенных GraphQL мутаций - PullRequest
0 голосов
/ 06 ноября 2018

Я ищу примеры написания вложенных мутаций. Я делаю мутацию для объекта рецепта, и схема выглядит следующим образом:

const RecipeType = new GraphQLObjectType({
  name: "Recipe",
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    dateCreated: { type: GraphQLString },
    authorID: { type: GraphQLID },
    prepTime: { type: PrepTimeType },
    cookTime: { type: CookTimeType },
    ingredients: { type: new GraphQLList(IngredientType) },
    steps: { type: new GraphQLList(StepType) }
  })
});

const PrepTimeType = new GraphQLObjectType({
  name: "PrepTime",
  fields: () => ({
    quantity: { type: GraphQLFloat },
    unit: { type: GraphQLString }
  })
});

const CookTimeType = new GraphQLObjectType({
  name: "CookTime",
  fields: () => ({
    quantity: { type: GraphQLFloat },
    unit: { type: GraphQLString }
  })
});

const IngredientType = new GraphQLObjectType({
  name: "Ingredients",
  fields: () => ({
    name: { type: GraphQLString },
    quantity: { type: GraphQLFloat },
    unit: { type: GraphQLString }
  })
});

const StepType = new GraphQLObjectType({
  name: "Ingredients",
  fields: () => ({
    details: { type: GraphQLString },
    estimatedTime: { type: GraphQLFloat },
    unit: { type: GraphQLString }
  })
});

Я ищу написать мутацию, которая создает целый объект для этого элемента. Мутация выглядит следующим образом:

createRecipe: {
  type: RecipeType,
  args: {
    // Required Args
    name: { type: new GraphQLNonNull(GraphQLString) },
    authorID: { type: new GraphQLNonNull(GraphQLID) },
    ingredients: { type: new GraphQLList(IngredientType) },
    steps: { type: new GraphQLList(StepType) },
    // Not required args
    prepTime: { type: PrepTimeType },
    cookTime: { type: CookTimeType },
  },
  resolve(parent, args) {
    let recipe = new Recipe({
      name: args.name,
      dateCreated: new Date().getTime(),
      authorID: args.authorID,
      ingredients: args.ingredients,
      steps: args.steps
    });

    // Check for optional args and set to recipe if they exist
    args.prepTime ? recipe.prepTime = args.prepTime : recipe.prepTime = null;
    args.cookTime ? recipe.cookTime = args.cookTime : recipe.cookTime = null;

    return recipe.save();
  }
}

Я не уверен, как создать одну мутацию, которая создает весь объект. и тогда обновление будет еще одной проблемой. У кого-нибудь есть примеры или ссылки на документы, которые поддерживают это? Из того, что я могу сказать, GraphQL не раскрыл это полезным способом.

В настоящее время я получаю следующие ошибки:

{
  "errors": [
    {
      "message": "The type of Mutation.createRecipe(ingredients:) must be Input Type but got: [Ingredients]."
    },
    {
      "message": "The type of Mutation.createRecipe(steps:) must be Input Type but got: [Steps]."
    },
    {
      "message": "The type of Mutation.createRecipe(prepTime:) must be Input Type but got: PrepTime."
    },
    {
      "message": "The type of Mutation.createRecipe(cookTime:) must be Input Type but got: CookTime."
    }
  ]
}

Любая поддержка будет принята с благодарностью.

Приветствия

1 Ответ

0 голосов
/ 06 ноября 2018

Я понял это. Мне нужно было создать типы ввода для каждого вложенного документа. У меня уже были типы объектов, но для мутаций мне пришлось добавить новые. Оттуда я поместил его в мутацию как таковую.

createRecipe: {
  type: RecipeType,
  args: {
    // Required Args
    name: { type: new GraphQLNonNull(GraphQLString) },
    authorID: { type: new GraphQLNonNull(GraphQLID) },
    ingredients: { type: new GraphQLList(IngredientInputType) },
    steps: { type: new GraphQLList(StepInputType) },
    // Not required args
    prepTime: { type: PrepTimeInputType },
    cookTime: { type: CookTimeInputType },
  },
  resolve(parent, args) {
    let recipe = new Recipe({
      name: args.name,
      dateCreated: new Date().getTime(),
      authorID: args.authorID,
      ingredients: args.ingredients,
      steps: args.steps
    });

    // Check for optional args and set to recipe if they exist
    args.prepTime ? recipe.prepTime = args.prepTime : recipe.prepTime = null ;
    args.cookTime ? recipe.cookTime = args.cookTime : recipe.cookTime = null ;

    return recipe.save();
  }
},
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...