Filtering

The generated query for serverless search includes a filter argument that contains the input types for the indexed fields.

There are different types of logical operators depending on the field type used — gt, gte, lt, lte, eq, neq, in, notIn and isNull. These operators also work with lists and nullable fields.

Consider the following schema:

type Post @model @search {
  title: String!
  slug: String! @unique
  likes: Int @default(value: 0)
}

You can use the filter argument to fetch Posts that have a likes value greater than or equal to 10 and less than or equal to 100:

query {
  postSearch(first: 100, filter: { likes: { gte: 10, lte: 100 } }) {
    edges {
      node {
        likes
      }
    }
  }
}

Grafbase automatically generates input types for each field indexed that is specific to the scalar, nullability and list configuration.

The following arguments can be used to filter based on the equality of values:

  • eq — Equal to
  • neq — Not equal to
  • in — In the list
  • notIn — Not in the list

The following are examples using the eq operator:

{
  userSearch(first: 100, filter: { name: { eq: "Lee" } }) {
    edges {
      node {
        id
      }
    }
  }
}

The following are examples using the neq operator:

{
  userSearch(first: 100, filter: { name: { neq: "Lee" } }) {
    edges {
      node {
        id
      }
    }
  }
}

The following are examples using the in operator:

{
  userSearch(first: 100, filter: { name: { in: ["Lee"] } }) {
    edges {
      node {
        id
      }
    }
  }
}

The following are examples using the notIn operator:

{
  userSearch(first: 100, filter: { name: { notIn: ["Lee"] } }) {
    edges {
      node {
        id
      }
    }
  }
}

The following arguments can be used to filter based on the range of values:

  • gt — Greater than
  • gte — Greater than or equal to
  • lt — Less than
  • lte — Less than or equal to

The following are examples using the gt operator:

{
  userSearch(first: 100, filter: { name: { gt: "F" } }) {
    edges {
      node {
        id
      }
    }
  }
}

The following are examples using the gte operator:

{
  userSearch(first: 100, filter: { name: { gte: "a" } }) {
    edges {
      node {
        id
      }
    }
  }
}

The following are examples using the lt operator:

{
  userSearch(first: 100, filter: { name: { lt: "z" } }) {
    edges {
      node {
        id
      }
    }
  }
}

The following are examples using the lte operator:

{
  userSearch(first: 100, filter: { name: { lte: "z" } }) {
    edges {
      node {
        id
      }
    }
  }
}

Nullable fields generate their own input type in the format of [Type]OrNull[model]FilterInput.

The following arguments can be used to filter based on the presence of null values:

  • isNull — Is null
{
  userSearch(first: 100, filter: { name: { isNull: true } }) {
    edges {
      node {
        id
      }
    }
  }
}
  • ALL — Fetch entries that match all filters.
  • ANY — Fetch entries that match any filter.
  • NONE — Fetch entries where no filters match.
  • NOT — Fetch entries that don't exactly match.
type User @model @search {
  name: String!
  age: Int
}
Was this page helpful?