Extending Result Hits
Customise Hits via a Resolver
- Add to the schema and update the type that has been configured as the
hitTypeName
. Normally this would beResultHit
. Below is an example of adding an additional field in Hit in the schema
type ResultHit implements SKHit {
id: ID!
fields: ResultFields
exampleCustomField: String // example field addition
}
Then provide a resolver for the field.
resolvers: withSearchkitResolvers({
ResultHit: {
exampleCustomField: (parent) => `Example Return Value for ${parent.id}`
}
}),
then with a GQL query like below
query {
results {
hits {
items {
... on ResultHit {
id
exampleCustomField
}
}
}
}
}
will call the exampleCustomField resolver for each hit item. The parent object (passed as an argument) contains the HitFields values. The return value is what will be provided in the response.
{
"data": {
"results": {
"hits": {
"items": [
{
"id": "tt0111161",
"exampleCustomField": "Example Return Value for tt0111161"
}
]
}
}
}
}