Filter query based on two combined fields (first name + last name)

I have a table with firstName and lastName. I want to have a GraphQL query to filter by these two fields individually (only first or last name, or first and last name combined).

This is the query I have so far:

  query MyQuery($value: String!) {
    personsList(
      filter: {
        OR: [
          { firstName: { contains: $value } }
          { lastName: { contains: $value } }
        ]
      }
    ) {
      items {
        id
        firstName
        lastName
      }
    }
  }

When passing "John" or "Smith" as the value, the query returns the record, but passing "John Smith" obviously doesn’t work because neither firstName or lastName contain the whole name. Is there a filter that’s the opposite of contains? Instead of having the data in the field contain the value, I need the value to contain the data in the field.

I couldn’t find anything in the docs for filtering, so I’d appreciate some help if I’m missing something obvious, or would love to know if that’s a known limitation of filtering.

The only solution I can think of right now is to split the value based on spaces in the string and use the in filter, but that doesn’t seem ideal.

Hi,

We don’t have this functionality right now. I’ve added it to a list of tasks.

I suppose you can try to do following. Not exactly what you asking but it will allow you to work with Full Name:

  • create new field
  • choose TEXT
  • call it fullName
  • In the right panel click on “Advanced Settings” on the bottom
  • Choose Calculated and then Virtual
  • set the value to something like CONCAT_WS(" ", firstName, lastName)

This will create virtual field with full name for you. I hope this helps until we implement additional filters.

2 Likes

Good idea – I’ll do that while waiting for a new filter that helps with this situation. Thanks!

@evgeny.semushin approach is the best here.

Another approach would to be to pass the names as a list and use the in predicate. Though I don’t think it would accomplish the partial matching that I believe you’re shooting for.

Input

{
  "names": [
    "john",
    "smith"
  ] 
}

Query

query($names: [String!]) {
  usersList(filter: {
    OR: [
       {
        firstName: {
          in: $names
        }
      },
      {
        lastName: {
          in: $names   
        }
      }
    ]
  }) {
    items {
      firstName
      lastName
      email
    }
  }
}