travis-r6s / gridsome-plugin-flexsearch

Add lightning fast search to Gridsome with FlexSearch
24 stars 7 forks source link

Deal with custom field resolvers #56

Closed ziopod closed 3 years ago

ziopod commented 3 years ago

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

travis-r6s commented 3 years ago

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.