0

I couldn't find a way to pass query variables to my graphql query, I've made a query which I think is how I'd be able to do mutation

mutation UpdateItem($datasource: String!, $language: String!, $fields: String!) {
  updateItem(
    path: $datasource
    language: $language
    fields: $fields
 ){
 ... on SampleItem {
     title {
      value
     }
    }
   }
 }

but I couldn't figure out how to pass my query variables.

I found this in the stackexchange How to pass a variable to search context? but I couldn't find the GraphQLData library

Any enlightenment on this matter would be greatly appreciated, Thanks in advance!

2

1 Answer 1

1

I assume you are trying it with GraphQL GUI and my understanding is that the question is how to pass a query variable dynamically to the query or mutation.

Here I'm trying to update the Title field value of Home item with following code.

mutation UpdateItem($datasource: String!,$language: String!, $fields: [FieldValueInput!]) {
  updateItem(
        path: $datasource,
      language: $language, 
        fields: $fields){
    ... on SampleItem {
      title {
        value
      }
    }
  }
}

Query variable:

{
  "datasource":"/sitecore/content/home",
  "language":"en",  
  "fields": [{ 
        "name": "title",
        "value": "'Example Item with changed title'"
      }]
}

It will look like this in GraphQL Playgrond. enter image description here

Also you can check the schema by clicking Docs button at the right side. The API documentation tab is one of the most exciting features of GraphQl Playground. It enables you to preview GraphQL queries, GraphQL type details, and a single field of a given schema.

enter image description here

Hope this helps!

1
  • Thanks for your response, sorry I thought this question was hidden by the admin/bots due to duplicate question. My intended question was passing query variables from code to sitecore graphql, I have found a way via fetch api though I wanted to implement it the same way it is structured in the styleguide graphql integrated sample docs, nevertheless your answer is valid so I will mark it as answered, thanks! Dec 9, 2022 at 16:33

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.