TransWikia.com

Using a GraphQL mutation to append a block to a Neo field

Craft CMS Asked by Jon W on January 18, 2021

I know how to update simple fields on an entry using a mutation:

mutation updateRecipe($recipeId: ID, $slug: String) {
  save_recipes_recipe_Entry(id: $recipeId, slug: $slug) {
    id
    title
    slug
  }
}
Variables:
{
  "recipeId": 8614,
  "slug":"new-slug"
}

Is there a way to append a block to a Neo field using mutations? I can retrieve the field data in a GraphQL query but have not found a way to update a Neo field with a mutation.

Does anybody have any experience implementing this?

Thanks …

One Answer

So the work around was to convert the Neo field to a Matrix. That worked for this instance since we're not storing anything complex but I'd sure like to figure out if/how Neo supports mutations.

mutation addRecipeReview($recipeId: ID, $blocks: [reviews_MatrixBlockContainerInput] = {review: {authorEmail: "", authorName: "", authorReview: "", reviewDate: "", reviewRating: ""}}, $sortOrder: [QueryArgument] = "") {
  save_recipes_recipe_Entry(id: $recipeId, reviews: {blocks: $blocks, sortOrder: $sortOrder}) {
    id
    reviews {
      ... on reviews_review_BlockType {
        id
        authorEmail
        authorName
        authorReview
        reviewRating
        reviewDate
      }
    }
  }
}

Variables:
{
  id: "1234",
  sortOrder: ["4321", "4322", "new:1"],
  blocks: [
    {
      review: {
        authorName: "Author Name",
        authorEmail: "Author Email",
        authorReview: "Review",
        reviewRating: "Rating",
        reviewDate: "Date Submitted"
      }
    }
  ]
}

The odd/silly part about the above is that you have to pass in an array of all the existing block IDs to sortOrder, and use the idiom "new:1" for your new block. If you skip the "new:1" it won't add the new block, and if you only pass in "new:1" but omit the existing block IDs it deletes all the existing blocks. You can pass an array of new blocks (the code above only has one) in the 'blocks' array, but you'll have to add "new:1", "new:2" ... up to how many blocks you have.

Answered by Jon W on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP