Deal with custom field resolvers
ziopod opened this issue · 1 comments
Hi,
I have some custom field resolvers in my gridsome.server.js
. I can figure how can get my custom field with flexSearch.
Do i use the righ way?
Custom field resolver example:
// Custom field
api.loadSource(({ addSchemaResolvers }) => {
addSchemaResolvers({
Post: {
reversedTitle: {
type: 'String',
resolve (obj) {
return obj.title.split('').reverse().join('')
}
}
}
})
})
Gridsome plugin Flexsearch configuration (gridsome.config.js
):
{
use: 'gridsome-plugin-flexsearch',
options: {
searchFields: ['title'],
collections: [
{
typeName: 'Post',
indexName: 'Post',
fields: [
'id',
'title',
'reversedTitle',
]
},
]
}
}
Excpected result:
{
"id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzQ3OTcxMzA5OTc4NjA=",
"path": "/post/hello",
"title": "Hello",
"reversedTitle": "olleH"
}
Obtained :
{
"id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzQ3OTcxMzA5OTc4NjA=",
"path": "/post/hello",
"title": "Hello"
}
If someone has a clue,
Best regards,
— Steve
Hey @ziopod,
This plugin pulls data directly from the store, so it will never hit any graphql resolvers.
You could either transform this data before it goes into the store with onCreateNode
, or you could use the GraphQL instructions here https://github.com/thetre97/gridsome-plugin-flexsearch#graphql-source which will get data using the Gridsome graphql
function, so it will then hit those resolvers.
Hope that helps.