Wp-graphql: Search against multiple types of data

Created on 18 Oct 2017  路  10Comments  路  Source: wp-graphql/wp-graphql

Please consider adding search functionality to WPGraphQL schema to search multiple types of data (i.e retrieve all Books and Authors whose post_title contains "cat" and also return individual fields per object-type).
I'm envisaging the query would look something like below:

query{
  search(title: "cat") {
    __typename
    ... on Books {
      title
      isbn
      publisher
      link
    }
    ... on Authors {
      title
      gender
      hometown
      link
    }    
  }
}
Actionable Architecture Needs Discussion Needs Info high high

Most helpful comment

@cr101 Search is definitely something I've been thinking about. . .but definitely needs a LOT more discussion for how to include in the core WPGraphQL plugin.

I'm thinking it would be good to see some patterns emerge from plugins to see what good Schema design might look like for search and for how to best handle connecting the Schema to WordPress internals.

At the moment, you _can_ search individual Types, like:

{
  posts(where: {search: "Keyword"}) {
    nodes {
      id
      title
    }
  }
}

But of course that's limited to just the post type.

I think there's a ton of value in being able to search cross-types, but there's a LOT of technical stuff to think about and discuss to see how it should best be implemented.

Adding search across PostObjects would be fairly straight forward as it's still one table in the DB, but adding cross-type search, like being able to search across Users, Terms, Posts, Comments, etc. . .would be pretty interesting to accomplish.

Definitely should keep this conversation going and see what we can come up with though!

All 10 comments

@cr101 Search is definitely something I've been thinking about. . .but definitely needs a LOT more discussion for how to include in the core WPGraphQL plugin.

I'm thinking it would be good to see some patterns emerge from plugins to see what good Schema design might look like for search and for how to best handle connecting the Schema to WordPress internals.

At the moment, you _can_ search individual Types, like:

{
  posts(where: {search: "Keyword"}) {
    nodes {
      id
      title
    }
  }
}

But of course that's limited to just the post type.

I think there's a ton of value in being able to search cross-types, but there's a LOT of technical stuff to think about and discuss to see how it should best be implemented.

Adding search across PostObjects would be fairly straight forward as it's still one table in the DB, but adding cross-type search, like being able to search across Users, Terms, Posts, Comments, etc. . .would be pretty interesting to accomplish.

Definitely should keep this conversation going and see what we can come up with though!

Yeah, this is an interesting one. Mostly because in theory you should be able to search across different post_types, but not all types, but in GQL each post_type is it's own type 馃

I think there are a few ways we could go about this... One of which would be to add a new Search query like:

{
    search(where: {types:["POST", "PAGE"], keyword:"Test"}) {
        nodes {
             id
             title
        }
}

where the types arg is an enum type of searchable post types.

My first thought is that it would just be too weird/difficult to implement this on the regular posts query by shoving it in to the args somehow.

Also curious if this is something the REST API can do. To my knowledge there's no instance where searching across multiple types is used in WordPress core. Only custom implementations, so that could drop the priority on this I think.

Similar to @cr101 initial request, we'd need to return the results as a union, as they could be of any type, like so:

{
    search(where: {types:["POST", "PAGE"], keyword:"Test"}) {
        nodes {
             ...on Post {
                ...postFields
             }
             ...on Page {
                ...pageFields
             }
        }
}

@CodeProKid I believe the core REST API only allows you to search per-type, like /posts/?search="your search term" or /pages/?search="your search term"

See: https://developer.wordpress.org/rest-api/reference/posts/#arguments

To my knowledge there's no instance where searching across multiple types is used in WordPress core. Only custom implementations, so that could drop the priority on this I think.

Well, the searchform allows for searching across types, doesn't it?

Do any of the Twenty-* themes make use of that?

@jasonbahl you're right, it will search across multiple types for default search: https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/class-wp-query.php#L1791-L1797

@jasonbahl @CodeProKid
It would be a great feature to search on multiple post types, as now to achieve the similar result you need to perform multiple queries with multiple paginations if you wish to display all results at once. Not ideal and it adds way more code. In the same time joining results is not possible as you're not able to paginate after that. (where: {first: 10, after: there is no reference here... })

Has any progress been made here, or has anyone created their own custom solution in the meantime that they are willing to share? At a loss currently on how to implement a "Search All Posts" functionality with WP-GraphQL.

For reference, for those on this thread, this was addressed and documented here: https://github.com/wp-graphql/wp-graphql/pull/1112

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CalebBarnes picture CalebBarnes  路  4Comments

szvest picture szvest  路  4Comments

reallyfabdigital picture reallyfabdigital  路  3Comments

cr101 picture cr101  路  4Comments

TylerBarnes picture TylerBarnes  路  4Comments