Using other GraphQL types

Recently I needed to expose a GraphQL query that accepted a list of Sitecore item IDs and returned the item data for those IDs. I wanted to use the Content Search API for efficiency reasons and because the items might not all be colocated in a folder.

Out of the box, the Sitecore GraphQL Search query does not allow you to do this, but a look into the GraphQL assembly revealed that the ListGraphType might do the job.

The resulting extensions to Sitecore.Services.GraphQL.Content.Queries.SearchQuery looked like this:

public class MySearchQuery : Sitecore.Services.GraphQL.Content.Queries.SearchQuery
    {
        public MySearchQuery()
        {
            Arguments.Add(new QueryArgument<ListGraphType<StringGraphType>>()
            {
                Name = "itemIds",
                Description = "Filter by Sitecore item IDs"
            });
        }

        //override the ContentSearchResults method here...
}

The GraphQL query was then able to pass in an array of item IDs which looked something like this:

query ExtendedSearch {
	searchItemsById(itemIds:["6a6f0d75-2a1d-4f70-b639-622d308d9e11","{EF1848D8-6C43-4E52-A04F-287A12E19D12}"]) {
      results {
      items {
        item {
          id
          field(name:"myItemField"){
            value
          }
        }
      }
    }
  }
}

One thing to note is that, although you are passing in a Guid like “6a6f0d75-2a1d-4f70-b639-622d308d9e11”, the Sitecore GraphQL content search results will come back with a value like “6A6F0D752A1D4F70B639622D308D9E11”.

GraphQL and Sitecore – video series Part 1

Today I released Part 1 of a video series on using GraphQL with Sitecore. In this first part, I cover getting up and running with GraphQL through:

  • installing Sitecore’s JavaScript Services Server components
  • setting up an endpoint configuration
  • creating and using an API key, and
  • running a query in the UI.

In later videos we’ll dig deeper into configuration, item and search queries, fragments, integrated GraphQL, and extending and customising GraphQL in the context of Sitecore.

Hope that people find it useful.