# Generate Meal Plan

Use the `generateMealPlan` mutation to create a new meal plan week for the user.&#x20;

By default, the meal plan is generated based on the user's [goals](https://docs.suggestic.com/graphql/query/mutations/user-profile/update-users-goals), but you have the ability to overwrite the default settings by using the [profileMealPlanSettings](https://docs.suggestic.com/graphql/query/mutations/user-profile/profile-meal-plan-settings) mutation.

## How to Use

### First Request

The first time a user requests `generateMealPlan` the request will immediately return  `success = true` and `mealPlan` will be ready to be queried.

### Generating a New Meal Plan

#### If Settings Haven't Changed

If the user's meal plan settings or goals have **not changed** since the previous meal plan was created then it will **not be possible** to generate a new meal plan until the last date in the current one has passed. This means that a user cannot generate a new meal plan if the last one is still running.

The mutation will return `success=false` when the user still has days left in their current meal plan.

You can force the generation of a new meal plan while the last day in the current one is still running passing the parameter `addDays = true` to the mutation. This is only available for the last day of the current meal plan, and it will append the new week to start the next day.

#### If Settings Did Change

If the user's meal plan settings (or goals) **have changed** since the previous meal plan was created then the process will resemble a first-time generation.

Please note that a meal plan is not automatically generated when the user's meal plan settings or goals change.

**Deleting and generating a meal plan again**

Delete a meal plan **only** if it hasn't started yet.

If you delete and generate a meal plan again without changing the user's goals or settings, the recipes displayed will remain the same. This occurs because the process will always look for the ideal initial combination according to the given user's goals.

#### Repeating a meal plan

The mutation accepts a `repeat` datetime parameter to repeat a past meal plan. The mutation will select the first 7 days in the user's history with a date greater than or equal to `repeat`. These days will be duplicated and set as the new meal plan.&#x20;

Because the selected days are sorted and selected only by their date, they may have been generated from meal plans with different settings or have gaps in their original dates.

The mutation will return `success=false` when the user doesn't have at least 7 days with a date greater than or equal to `repeat`.

**Defining the variety of recipes**

Use the [`variety`](#arguments) argument to generate a meal plan with less variety of recipes; therefore, this allows recipes repeated in the meal plan.

There are 3 levels of variety:

* `LOW:` Generate three different meals in the week. For example, the meal plan generates three breakfasts, three lunches, three dinners, and three snacks distributed across the week.&#x20;
* `MEDIUM:` Generate 5 different meals in the week.
* `HIGH:` All generated meals are different. This is the current behavior of the meal plan.

This is not a required argument, so if it is not defined, the `high` variety is set as default.

**Get the information of the meal plan generated**

Instead of executing the [`mealPlan`](https://docs.suggestic.com/graphql/query/queries/meal-plan/meal-plan-1) query separately,  include the `mealPlan` field in the response of the `generateMealPlan` mutation to retrieve the information of the generated meal plan.&#x20;

Use this information to debug your meal plan or when the meal plan is generated quickly.

*This is not recommended to be used in the Apps because of a timeout of 50 seconds.*

### **Generate a meal plan based on a created template**

Generate custom meal plans by using a meal plan template previously created. In addition, if you have a running meal plan, you can add the days from the meal plan template by using the `addDays` argument as true. For more information, refer to [this example](#add-days-from-a-meal-plan-to-the-current-meal-plan).

First, the user creates the [meal plan template](https://docs.suggestic.com/graphql/query/mutations/meal-plan/create-meal-plan-template) according to what best fits him and then uses the [template ID](https://docs.suggestic.com/graphql/query/search/meal-plan-template-search) to pass as an argument in this mutation.

For more information, refer to [this example](#generate-a-meal-plan-based-on-a-meal-plan-template).

### Generating a meal plan with a predefined calorie distribution

You may use the distribution arguments to generate a meal plan with a predefined distribution of calories per meal. For example, you may want that breakfast doesn't exceed 30% of total daily calories and dinner doesn't exceed 25%. The request for this example would look as follows:

{% tabs %}
{% tab title="Mutation" %}

```graphql
mutation {
    generateMealPlan(
        breakfastDistribution: 0.3
        dinnerDistribution: 0.25
    ) {
        success
        message
    }
}
```

{% endtab %}
{% endtabs %}

It's important to notice that these percentages are per meal and not per meal tag, so if your meal plan's format contains 2 breakfasts, the generator will try to pick two breakfast recipes with calories equal to 30% each, this means that your 2 breakfasts will represent 60% of the daily caloric goal!

### Couldn't generate a meal plan?

If you get the `Couldn't generate a meal plan with the current settings` message, check the meal plan settings by executing the [`debugMealPlanVariables`](https://docs.suggestic.com/graphql/query/queries/meal-plan/meal-plan-config) query. For more information to know the causes and get the solution, see [this document.](https://docs.suggestic.com/graphql/knowledge-base/common-errors)

## Arguments

| **Argument Name**       | **Type**                                                                                                 | **Description**                                                                                                                                                                                                                                                                                                                                              |
| ----------------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `addDays`               | Boolean                                                                                                  | <p>True to allow generating a new meal plan if a user is on her last meal plan day. This field is ignored if <code>ignoreLock</code> is true. Default is false.<br>If the <code>fromTemplate</code> argument is used and the  <code>addDays</code> is true, the template days are added to the current meal plan, instead of replacing the running days.</p> |
| `isAppend`              | Boolean                                                                                                  | True to add an additional week worth of meal plan (7 days) to the end of the current meal plan each time is executed. (Default is false.)                                                                                                                                                                                                                    |
| `appendDays`            | Int                                                                                                      | After setting `isAppend` to *True* use `appendDays` to define the amount of days to append. (max 7)                                                                                                                                                                                                                                                          |
| `fromTemplate`          | ID                                                                                                       | Use the [meal plan template ID](https://docs.suggestic.com/graphql/query/search/meal-plan-template-search) from which the meal plan will be generated.                                                                                                                                                                                                       |
| `ignoreLock`            | Boolean                                                                                                  | True to allow generating a new meal plan any time, overwriting any pending days in the current plan. Default is false.                                                                                                                                                                                                                                       |
| `includeFavorites`      | Boolean                                                                                                  | <p>True to include 2 random recipes, extracted from the user's favorites, within the generated meal plan.</p><p>False no actions are executed and the number of generated days is still 7.</p>                                                                                                                                                               |
| `repeat`                | Datetime                                                                                                 | <p>Create a new meal plan repeating the 7 dates after <code>repeat</code> </p><p>in user's history. Default to null.</p>                                                                                                                                                                                                                                     |
| `kcalLimit`             | Float                                                                                                    | Sets an upper kcal limit per meal in the meal plan as a percentage of the daily goal. Default is 0.50.                                                                                                                                                                                                                                                       |
| `maxNumOfServings`      | Int                                                                                                      | A maximum number of servings per meal is allowed in the meal plan. Must be between 1 and 15. Default is 15.                                                                                                                                                                                                                                                  |
| `maxServingWeight`      | Int                                                                                                      | Maximum serving weight in grams per meal. Default is 600.                                                                                                                                                                                                                                                                                                    |
| `minServingWeight`      | Int                                                                                                      | Minimum serving weight in grams per meal. Default is 100.                                                                                                                                                                                                                                                                                                    |
| `breakfastDistribution` | Float                                                                                                    | Percentage of the daily calories goal that each breakfast covers.                                                                                                                                                                                                                                                                                            |
| `lunchDistribution`     | Float                                                                                                    | Percentage of the daily calories goal that each lunch covers.                                                                                                                                                                                                                                                                                                |
| `dinnerDistribution`    | Float                                                                                                    | Percentage of the daily calories goal that each dinner covers.                                                                                                                                                                                                                                                                                               |
| `snackDistribution`     | Float                                                                                                    | Percentage of the daily calories goal that each snack covers.                                                                                                                                                                                                                                                                                                |
| `maxTimeMinutes`        | [maxTimeMinutes](https://docs.suggestic.com/graphql/objects/meal-plan/maximum-preparation-time-per-meal) | <p>Object that returns the maximum values for each meal. Expressed in minutes.</p><p>For example, maximum preparation time for the following meals:</p><ul><li>breakfast: 15 </li><li>lunch: 30</li><li>dinner: 40</li><li>snack: 30</li></ul>                                                                                                               |
| `maxIngredientsCount`   | [maxIngredientsCount](https://docs.suggestic.com/graphql/objects/meal-plan/maximum-ingredient-count)     | <p></p><p>Object that returns the maximum values for each meal. Expressed in minutes.</p><p>For example, maximum ingredient count for the following meals:</p><ul><li>breakfast: 7 </li><li>lunch: 5</li><li>dinner: 5</li><li>snack: 6</li></ul>                                                                                                            |
| `variety`               | MPVariety                                                                                                | Define the variety of the recipes generated in the meal plan. Possible Values: `LOW`, `MEDIUM`, or `HIGH`                                                                                                                                                                                                                                                    |
| `startDate`             | Date                                                                                                     | Set starting date for the Meal Plan. (YYYY-MM-DD )                                                                                                                                                                                                                                                                                                           |

## Available fields

The following field will be part of the response.

| **Field name** | **Type**                                                                          | **Description**                                                                       |
| -------------- | --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| `success`      | Boolean                                                                           | **True** if the meal plan template has been created. Otherwise, it displays **False** |
| `message`      | String                                                                            | Description of the result                                                             |
| `mealPlan`     | [mealPlanDay](https://docs.suggestic.com/graphql/objects/meal-plan/meal-plan-day) | Returns the information of the meal plan generated.                                   |

## Examples

### Create a new meal plan week for the user:

{% tabs %}
{% tab title="Mutation" %}

```graphql
mutation {
  generateMealPlan {
    success
    message
  }
}
```

{% endtab %}

{% tab title="Response" %}

```javascript
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated."
    }
  }
}
```

{% endtab %}
{% endtabs %}

### Append a meal plan week to the current meal plan in progress:

{% tabs %}
{% tab title="Mutation" %}

```graphql
mutation{ generateMealPlan(isAppend:true  appendDays:7){
  success
  message
}}
```

{% endtab %}

{% tab title="Response" %}

```graphql
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated."
    }
  }
}
```

{% endtab %}
{% endtabs %}

### Add days to the meal plan

Create another week when the last day from the last meal plan is still running:

{% tabs %}
{% tab title="Mutation" %}

```graphql
mutation {
  generateMealPlan(addDays: true) {
    success
    message
  }
}
```

{% endtab %}

{% tab title="Response" %}

```javascript
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated."
    }
  }
}
```

{% endtab %}
{% endtabs %}

### Repeat a week

Repeat the week from 2020-05-20 to 2020-05-27. Remember that `repeat` has to be a DateTime in UTC timezone.

{% tabs %}
{% tab title="Mutation" %}

```graphql
mutation {
  generateMealPlan(repeat: "2020-05-20T10:25:14.120000Z") {
    success
    message
  }
}
```

{% endtab %}

{% tab title="Response" %}

```javascript
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated."
    }
  }
}
```

{% endtab %}
{% endtabs %}

### Set the maximum preparation times and the maximum ingredients&#x20;

Set the maximum preparation times and the maximum ingredients for the **breakfast**, **lunch**, **dinner**, and **snack** meals&#x20;

{% tabs %}
{% tab title="Mutation" %}

```graphql
mutation {
  generateMealPlan (
     maxTimeMinutes:{
      breakfast:10,
      dinner:5,
      snack:200,
      lunch:3
    }
    maxIngredientsCount:
    {
      breakfast:7,
      dinner:5,
      snack:5,
      lunch:3
    }
  ) 
    {
    success
    message
  }
}
```

{% endtab %}

{% tab title="Response" %}

```graphql
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated."
    }
  }
}
```

{% endtab %}
{% endtabs %}

### Define a low variety of recipes

{% tabs %}
{% tab title="Request" %}

```graphql
mutation {
  generateMealPlan(variety: LOW) {
    success
    message
  }
}
```

{% endtab %}

{% tab title="Response" %}

```graphql
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated."
    }
  }
}
```

{% endtab %}
{% endtabs %}

Execute the [`mealPlan`](https://docs.suggestic.com/graphql/query/queries/meal-plan/meal-plan-1) query to check the generated recipes.

### Generate a Meal Plan based on a Meal Plan Template&#x20;

The following example describes how to generate a meal plan based on a given template ID

{% tabs %}
{% tab title="Request" %}

```graphql
mutation {
  generateMealPlan(
    fromTemplate: "TWVhbFBsYW5UZW1wbGF0ZTphNjc5NGIwYy1lN2Y4LTQ5ZDYtOWIxZC04M2ExZTY0MDExZmE="
  ) {
    success
    message
    mealPlan {
      day
      date
    }
  }
}


```

{% endtab %}

{% tab title="Response" %}

```graphql
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated.",
      "mealPlan": [
        {
          "day": 2,
          "date": "2022-01-15 02:00:00+00:00"
        },
        {
          "day": 1,
          "date": "2022-01-14 02:00:00+00:00"
        }
      ]
    }
  }
}
```

{% endtab %}
{% endtabs %}

As follows find a step by step explanation of how to get the template ID and use it to generate a meal plan:

![](https://920729701-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwqSnBDpAb6mFZYLsuB%2Fuploads%2FI1brasAJ4hRxBsqeIoWD%2FGenerateMealPlan.gif?alt=media\&token=8a0850b3-d4c3-4953-8c40-d8dbf75982ad)

### Add days from a meal plan to the current meal plan

The following example adds the days created in a meal plan template to the meal plan currently active.

{% tabs %}
{% tab title="Request" %}

```graphql
mutation {
  generateMealPlan(
    fromTemplate: "TWVhbFBsYW5UZW1wbGF0ZToyYTVmY2Q3Zi0wNzJlLTRiMjYtOGE3ZC1iYTgxNDZhMmY3ZTQ="
    addDays: true
  ) {
    success
    message
  }
}

```

{% endtab %}

{% tab title="Response" %}

```graphql
{
  "data": {
    "generateMealPlan": {
      "success": true,
      "message": "Meal plan generated."
    }
  }
}
```

{% endtab %}
{% endtabs %}

In the following example, 5 days from the meal plan template is added to the current meal plan:

![](https://920729701-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwqSnBDpAb6mFZYLsuB%2Fuploads%2FcvyC5vkWQ09VQUZdL0tc%2FGenerateMPTemplateAddDays.gif?alt=media\&token=29afc406-c19a-425b-8ff7-9ca908e8b752)

#### UI/UX example

In the same way, we have code examples, this is an implementation example in which see results of the API execution in the Suggestic App.&#x20;

Login to the App, tap on the **Meal Plan** option, and the 7-day meal plan is listed. The information is separated per day and per meal type:

![](https://920729701-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwqSnBDpAb6mFZYLsuB%2Fuploads%2F1pO4S3DBuejfkbr66p6Z%2FGenerateMealPlan.gif?alt=media\&token=617afe3e-84f7-4e7f-9426-d8075db41700)
