J. Jackson
J. Jackson

Reputation: 3774

Strapi GraphQL search by multiple attributes

I've got a very simple Nuxt app with Strapi GraphQL backend that I'm trying to use and learn more about GraphQL in the process.

One of my last features is to implement a search feature where a user enters a search query, and Strapi/GraphQL performs that search based on attributes such as image name and tag names that are associated with that image. I've been reading the Strapi documentation and there's a segment about performing a search.

So in my schema.graphql, I've added this line:

type Query {
  ...other generated queries
  searchImages(searchQuery: String): [Image
}

Then in the /api/image/config/schema.graphql.js file, I've added this:

module.exports = {
  query: `
    searchImages(searchQuery: String): [Image]
  `,
  resolver: {
    Query: {
      searchImages: {
        resolverOf: 'Image.find',
        async resolver(_, { searchQuery }) {
          if (searchQuery) {
            const params = {
              name_contains: searchQuery,
              // tags_contains: searchQuery,
              // location_contains: searchQuery,
            }
            const searchResults = await strapi.services.image.search(params);
            console.log('searchResults: ', searchResults);
            return searchResults;
          }
        }
      }
    },
  },
};

At this point I'm just trying to return results in the GraphQL playground, however when I run something simple in the Playground like:

query($searchQuery: String!) {
  searchImages(searchQuery:$searchQuery) {
    id
    name
  }
}

I get the error: "TypeError: Cannot read property 'split' of undefined".

Any ideas what might be going on here?

UPDATE: For now, I'm using deep filtering instead of the search like so:

query($searchQuery: String) {
  images(
    where: {
      tags: { title_contains: $searchQuery }
      name_contains: $searchQuery
    }
  ) {
    id
    name
    slug
    src {
      url
      formats
    }
  }
}

This is not ideal because it's not an OR/WHERE operator, meaning it's not searching by tag title or image name. It seems to only hit the first where. Ideally I would like to use Strapi's search service.

Upvotes: 1

Views: 2695

Answers (1)

I actually ran into this problem not to recently and took a different solution. the where condition can be combined with using either _and or _or. as seen below.

  1. _or
    articles(where: { 
    _or: [
      { content_contains: $dataContains },
      { description_contains: $dataContains }
    ]})
  1. _and
    (where: {
      _and: [
        {slug_contains: $categoriesContains}
      ]})

Additionally, these operators can be combined given that where in this instance is an object.

For your solution I would presume you want an or condition in your where filter predicate like below

    images(where: {
      _or: [
        { title_contains: $searchQuery },
        { name_contains: $searchQuery }
    ]})

Lastly, you can perform a query that filters by a predicate by creating an event schema and adding the @search directive as seen here

Upvotes: 1

Related Questions