1

I have started to look at using Graph QL for search using the endpoint that comes out the box with Sitecore 10.1. I've read that the results are based on an item but had lazy loading eg. You have indexed a field. How does this work in regards to computed fields, how do I see this as part of my results set?

1 Answer 1

2

Step 1.

Add a new config file to your {webroot folder}/App_Config/Include area to include an /api/search GraphQL endpoint:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore>
    <api>
        <!--
    Define the app's Sitecore GraphQL API endpoint
    Note: this can be removed if you are not using GraphQL.
    Note: the endpoint must be defined both for integrated and connected type GraphQL queries.
  -->
        <GraphQL>
            <endpoints>
                <searchGraphQLEndpoint url="/api/search" type="Sitecore.Services.GraphQL.Hosting.DatabaseAwareGraphQLEndpoint, Sitecore.Services.GraphQL.NetFxHost" resolve="true">
                    <url>$(url)</url>

                    <enabled>true</enabled>
                    <enableSubscriptions>true</enableSubscriptions>

                    <!-- lock down the endpoint when deployed to content delivery -->
                    <graphiql role:require="ContentDelivery">false</graphiql>
                    <enableSchemaExport role:require="ContentDelivery">false</enableSchemaExport>
                    <enableStats role:require="ContentDelivery">false</enableStats>
                    <enableCacheStats role:require="ContentDelivery">false</enableCacheStats>
                    <disableIntrospection role:require="ContentDelivery">true</disableIntrospection>

                    <schema hint="list:AddSchemaProvider">
                        <content type="Sitecore.Services.GraphQL.Content.ContentSchemaProvider, Sitecore.Services.GraphQL.Content">
                            <!-- scope typed template generation to just this app's templates -->
                            <templates type="Sitecore.Services.GraphQL.Content.TemplateGeneration.Filters.StandardTemplatePredicate, Sitecore.Services.GraphQL.Content">
                                <database>context</database>
                                <paths hint="list:AddIncludedPath">
                                    <templates>/sitecore/templates/Project/search</templates>
                                </paths>
                                <fieldFilter type="Sitecore.Services.GraphQL.Content.TemplateGeneration.Filters.StandardFieldFilter, Sitecore.Services.GraphQL.Content">
                                    <exclusions hint="raw:AddFilter">
                                        <!--
                                        Remove system fields from the API (e.g. __Layout) to keep the schema lean
                                        -->
                                        <exclude name="__*" />
                                    </exclusions>
                                </fieldFilter>
                            </templates>

                            <queries hint="raw:AddQuery">
                                <!-- enable querying on items via this API -->
                                <query name="item" type="Sitecore.Services.GraphQL.Content.Queries.ItemQuery, Sitecore.Services.GraphQL.Content" />
                                <query name="search" type="Sitecore.Services.GraphQL.Content.Queries.SearchQuery, Sitecore.Services.GraphQL.Content"/>
                            </queries>

                            <fieldTypeMapping ref="/sitecore/api/GraphQL/defaults/content/fieldTypeMappings/standardTypeMapping" />
                        </content>
                    </schema>

                    <!-- Enables the 'jss' graph nodes that are preformatted to use with JSS rendering components, and the datasource resolving queries for JSS -->
                    <extenders hint="list:AddExtender">
                        <layoutExtender type="Sitecore.JavaScriptServices.GraphQL.JssExtender, Sitecore.JavaScriptServices.GraphQL" resolve="true" />
                    </extenders>

                    <!-- Determines the security of the service. 'publicService' is open to anonymous access, but requires an SSC API key. -->
                    <security ref="/sitecore/api/GraphQL/defaults/security/publicService" />

                    <!-- Determines how performance is logged for the service. Defaults are defined in Sitecore.Services.GraphQL.config -->
                    <performance ref="/sitecore/api/GraphQL/defaults/performance/standard" />
                </searchGraphQLEndpoint>
            </endpoints>
        </GraphQL>
    </api>
</sitecore>

Step 2.

Open GraphQL Playground in a browser window and visit the new /api/search URL https://{your-sitecore-host}/api/search/ui?sc_apikey={your-api-key}

Step 3.

Insert this query on the left that contains the requested computed index field name you want to retrieve (in my case it was searchcontent_txm). Please note, that this field is requested outside the item{} block, as inside you can only request for Sitecore item fields.

    query Search($rootItem: String!, $keyword: String!, $pageSize: Int!) {
  search(
    rootItem: $rootItem
    fieldsEqual:[
        { name: "searchcontent_txm", value: $keyword}
      ]
    first: $pageSize
    latestVersion: false
  ) {
    results {
      items{
        item {
            id
            name
          path
          url
          navigationTitle: field (name:"navigationTitle") {
            value
          }
          description: field (name:"description") {
            value
          }
        }
        searchcontent: field(name:"searchcontent_txm")
        score
      }
      totalCount
        pageInfo {
        endCursor
          hasPreviousPage
          hasNextPage
        }
    }
  }
}

Step 4. Give your query some input parameters, e.g.:

{
  "rootItem": "/sitecore/content/your-website/home",
  "keyword": "cinnamon",
  "pageSize": 5
}

Execute your query and see your results :)

1
  • I'm curious if anyone has had luck implementing this against the XM Cloud GraphQL Search Query as the results don't allow for items Commented Sep 13, 2023 at 22:08

Your Answer

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

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