Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for filtering by relationship fields in the admin UI #352

Closed
Tracked by #2692
timleslie opened this issue Sep 20, 2018 · 9 comments
Closed
Tracked by #2692

Add support for filtering by relationship fields in the admin UI #352

timleslie opened this issue Sep 20, 2018 · 9 comments
Assignees

Comments

@timleslie
Copy link
Contributor

timleslie commented Sep 20, 2018

Let's say you have a list posts with a related field author (that links to the authors list). Currently, the "simple search" functionality (#319) available from the admin UI won't dereference this relationship. So if you filter by "Jane" in the admin UI, you'll only get posts with "Jane" in the name, not posts where the author has "Jane" in their name.

Ideally, Keystone would be able to apply these "simple search" filters to related collections and apply them to items in the list being viewed.

Note, this functionality is closely related to, and dependant on, the "Implement configurable search fields" (#343) but is a distinct piece of work.

@timleslie
Copy link
Contributor Author

As a first pass we should be able to filter by just the ID (using the label inside a select component).

@molomby
Copy link
Member

molomby commented Oct 3, 2018

As discussed, the first step is building UI to allow selection of a single item from a related list. Ie. a searchable select field.

The exact behaviour is going to depend on the number of items in the related list. If there aren't many (<50?) the interface could load them all then maybe filter down client-side as the user types (I believe this is the current behaviour?).

However, the related list could potentially contain millions of items. This requires a mode of operation where no items are loaded automatically. The user can type into the field and, when less than a certain number are matched (20?), they populated into the select field.

This UI/behaviour should also be used when setting a single related item in the new/edit form.

@jesstelford
Copy link
Contributor

jesstelford commented Oct 25, 2018

It would be easy enough to do the filtering by queries, it'd just be a matter of figuring out which things to filter by:

query searchPosts($search: String) {
  allPosts(where: {
    OR: [
      title_contains: $search,
      body_contains: $search,
      author: { name_contains: $search },
      comments: { comment_contains: $search }
    ]
  }) {
    id
    title
  }
}

It would be possible to recursively gather up all the possible title_contains, etc, filters for a list, something like:

function listFieldsAsSearchPaths(list) {
  return list.fieldsArray.map(field => {
    switch (field.prototype.constructor) {
      case Text:
        return `${field.path}_contains: $search`;
      case Relationship: {
        const paths = listFieldsAsSearchPaths(getListByKey(field.ref));
        if (!paths) {
          return null;
        }
        return `${field.path}: { ${paths} }`;
      }
      default:
        return null;
    }
  }).filter(filter => filter)
  .join(',\n');
}

const query = gql`
  query searchPosts($search: String) {
    allPosts(
      where: {
        OR: [
          ${filters}
        ]
      },
      first: 20
    ) {
      id
      title
    }
  }
`;

(Note: This example has the limit set to the first 20)

@jesstelford
Copy link
Contributor

The above could also act as an initial implementation for #319 & #343

@MadeByMike
Copy link
Contributor

From duplicate issue:

The queries for this kind of filtering already exist:

query {
  allPosts(where: { author: { name_contains: "Jane" } }) {
    id
    title
  }
}

@stale
Copy link

stale bot commented Nov 15, 2019

It looks like you haven't had a response in over 3 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contributions. :)

@gautamsi
Copy link
Member

what about adding adminConfig item for search

here $search is predefined variable we document that someone has to use

listConfig.adminConfig = { searchQuery: `query <name>($searchText: String!){all<Listname>s(where:{mycondition: $search}){<required fields can be filled by query builder based on view>}`}

or we just get the querybuilder based on variable name,

listConfig.adminConfig = { searchQuery: `{OR: [{field1: $search},{field2_contains: $search}]}` }

we will replace the where clause condition from this config value.

@stale stale bot removed the needs-review label May 22, 2020
@gautamsi
Copy link
Member

this way I can have it customised for each list.

@stale
Copy link

stale bot commented Sep 20, 2020

It looks like there hasn't been any activity here in over 6 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contribution. :)

@stale stale bot added the needs-review label Sep 20, 2020
@bladey bladey closed this as completed Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants