3

I am trying to build a query that returns all items of a specific template, but can't seem to figure out if there's a "template" field available on the search query for GraphQL.

Where can I find a list of available fields for the GraphQL search query?

Here's what I tried:

{
    # 3BD4AD928374500C836811A4F049ED22 = ContentBlock template ID
    search(where: {name: "template", value: "3BD4AD928374500C836811A4F049ED22"}) {
        total
        results {
            id
            name
            path
            hasChildren
        }
    }
}

I also tried templatename, templateid, templates, __templates, and all the other options I could think of. They all return the following results:

{
  "data": {
    "search": {
      "total": 0,
      "results": []
    }
  }
}

Environment details

  • jss --version: 18.0.0
  • Using the edge GraphQL endpoint, which uses the edgeContent schema configuration

1 Answer 1

3

Based on my review of the decompiled Sitecore.Services.GraphQL.EdgeSchema.Services.SearchService class (which is the service used by the Sitecore.Services.GraphQL.EdgeSchema.Queries.SearchQuery query which responds to search queries in GraphQL), it appears that you can use any field that is present in your Solr index, but a handful of standard fields get some special treatment.

The Solution

If you look in the Solr index (e.g. sitecore_master_index) you will see that the field containing the templates list is called _templates. The below query returns all items with a passed-in template:

{
    # 3BD4AD928374500C836811A4F049ED22 = ContentBlock template ID
    search(where: {name: "_templates", value: "3BD4AD928374500C836811A4F049ED22", operator: EQ}) {
        total
        results {
            id
            name
            path
            hasChildren
        }
    }
}

More Info

Special field handling

There a handful of standard fields that support a short-hand syntax, allowing you to use a default search operator and not have to pass one in. Here's the list from the SearchService constructor:

private Dictionary<string, ItemSearchOperator> supportedFields;

public SearchService(IOperationHandlerFactory operationHandlerFactory, IPaginationService paginationService)
{
    supportedFields = new Dictionary<string, ItemSearchOperator>();
    supportedFields.Add("_name", ItemSearchOperator.EQ);
    supportedFields.Add("_language", ItemSearchOperator.EQ);
    supportedFields.Add("_path", ItemSearchOperator.CONTAINS);
    supportedFields.Add("_templates", ItemSearchOperator.CONTAINS);
    supportedFields.Add("_hasLayout", ItemSearchOperator.EQ);
    supportedFields.Add("_parent", ItemSearchOperator.EQ);
    _operationHandlerFactory = operationHandlerFactory;
    _paginationService = paginationService;
}

Therefore, you can simplify the above query by removing the operator: EQ section, if you wish.

Other fields

You can also use your own fields to search against, like so:

{
    # 3BD4AD928374500C836811A4F049ED22 = ContentBlock template ID
    search(
        where: {
            name: "_templates"
            value: "3BD4AD928374500C836811A4F049ED22"
            AND: { name: "heading", value: "", operator: NEQ }
        }
    ) {
        total
        results {
            id
            name
            path
            hasChildren
            fields {
                name
                value
            }
        }
    }
}

Or, you can search against any other standard field in the Solr index (just make sure you include the operator property):

{
    search(
        where: {
            name: "_displayname"
            value: "content"
            operator: CONTAINS
        }
    ) {
        total
        results {
            id
            name
            path
            hasChildren
            fields {
                name
                value
            }
        }
    }
}
1
  • Nice one, also submitting a doc request for this. Sep 16, 2021 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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