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”.