1

I am trying to create a new entry in Craft CMS using GraphQL. The entry has a matrix field called "vehicles" that has a single block called "vehicle". This block has a single field called "vehicleValue".

enter image description here

In GraphiQL I am using the following query to save a new entry:

mutation SaveVehicle($vehicles: vehicles_MatrixInput) {
  save_applications_application_Entry(vehicles: $vehicles) {
    uid
    vehicles {
      ... on vehicles_vehicle_BlockType {
        vehicleValue
      }
    }
  }
}

Then I pass the following variables:

{
  "vehicles": {
    "blocks": [
      {
        "vehicle": {
          "vehicleValue": 111
        }
      }
    ]
  }
}

The query runs and a new entry is created but the matrix field is empty.

The schema is public so no auth is required.

I am new to using Craft and GraphQL so it is probably something simple. Any help would be greatly appreciated.

1 Answer 1

1

Try this:

mutation SaveVehicle($vehicles: [vehicles_MatrixBlockContainerInput], $sortOrder: [QueryArgument]) {
  save_applications_application_Entry(vehicles: {blocks: $vehicles, sortOrder: $sortOrder}) {
        uid
        vehicles {
            ...on vehicles_vehicle_BlockType {
                vehicleValue
            }
        }
    }
}

And your variables:

{
    "sortOrder":"new1",
    "vehicles": [
        {
            "vehicle": {
                "id": "new1", 
                "vehicleValue": "111"
            }
        }
    ]
}

You can find more info in the documentation.

1
  • Thank you very much, this is exactly what I needed! Works perfectly.
    – theboldben
    Feb 10 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.