2

While executing below GraphQL query using Sitecore Experience Graph Browser

{
  item(path:"/sitecore/content/home")
  {
    name
    fields {
      name
      value
    }
  }
}

the response is as follows:

{
  "data": {
    "item": {
      "name": "Home",
      "fields": [
        {
          "name": "Summary",
          "value": "This is summary"
        },
        {
          "name": "Title",
          "value": "This is title"
        }
      ]
    }
  }
}

Is there a way, we can get the below response by tweaking something in Graph query itself.

{
  "data": {
    "item": {
      "name": "Home",
      "fields": [
        {
          "Summary": "This is summary"
        },
        {
          "Title": "This is title"
        }
      ]
    }
  }
}
1

The short answer is no for OOTB. GraphQL is based on a schema.

Your GraphQL server uses a schema to describe the shape of your data graph. This schema defines a hierarchy of types with fields that are populated from your back-end data stores. The schema also specifies exactly which queries and mutations are available for clients to execute against your data graph.

So, even if you have a custom implementation of your GraphQL query, you will need to specify the mapping of your graph types. See example below:

{
  item(path:"/sitecore/content/home")
  {
    name
    fields {
      name
      value
    }
  }
}

The above query will get its values via backend code. Below is a pseudo code how the process works internally

Get Item name using Sitecore APIs and assign it to the field string graph type

Field<StringGraphType>("name", null, null, x => x.Source.ItemName);

For the fields, it will loops through all the fields available on the items and you can specify whether to include the standard fields. A list will be obtained and it will get populated as follows

Field<ListGraphType<ItemFieldInterfaceGraphType>>("fields", null, null, x => x.Source.FieldCollections);

Please note that the above is not how it is implemented in Sitecore but it gives an overview on how it looks. You can get more information on the implementation by looking at the Sitecore dll Sitecore.Services.GraphQL.Content.dll. The namespace for the item query is Sitecore.Services.GraphQL.Content.Queries.ItemQuery.

You can still perform your proposed scenario but you will need to create a new query and then have the graph type implementation as dynamic. So, the name property in the graph type will be dynamic. However, you may end up having lots of issues since you will need to know the schema so that the returned result is mapped to the schema.

For example, all developers knows the different properties that a Sitecore field has (name, value) but if you write down the code to be like "fieldNameHere": "fieldValueHere", it will cause some errors because you may end up specifying a field name which does not exist.

1
  • Thanks a lot Gishaam for your valuable information. I will try it out and let you know. – Arvind Gehlot Feb 10 '20 at 11:11
1

You cannot get your desired query result format OOTB, but you can get template fields in a strongly-typed way by using inline fragments that select fields from specific types, as described in the Sitecore GraphQL documentation page here.

This input query:

{
  item(path: "/sitecore/content/home") {
    name
    ...on SampleItem {
      text {
        value
      },
      title {
        value
      }
    }
  }
}

produces this query result:

{
  "data": {
    "item": {
      "name": "Home",
      "text": {
        "value": "<p style=\"line-height: 22px;\">From a single connected platform that also integrates with other customer-facing platforms, to a single view of the customer in a big data marketing repository, to completely eliminating much of the complexity that has previously held marketers back, the latest version of Sitecore makes customer experience highly achievable. Learn how the latest version of Sitecore gives marketers the complete data, integrated tools, and automation capabilities to engage customers throughout an iterative lifecycle &ndash; the technology foundation absolutely necessary to win customers for life.</p>\n<p>For further information, please go to the <a href=\"https://doc.sitecore.net/\" target=\"_blank\" title=\"Sitecore Documentation site\">Sitecore Documentation site</a></p>\r"
      },
      "title": {
        "value": "Sitecore Experience Platform"
      }
    }
  }
}

It requires that the client application knows the template of the item (SampleItem) and its field names (text,title).

1
  • Thanks a lot Alessandro for your valuable information. I will try it out and let you know. – Arvind Gehlot Feb 10 '20 at 11:11

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.