> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Entity Queries

> Run basic GraphQL entity queries against a Squid SDK GraphQL API.

## Introduction

OpenReader auto-generates queries from the `schema.graphql` file. All entities defined in the schema can be queried over the GraphQL endpoint.

## Exploring queries

Let's take a look at the different queries you can run using the GraphQL server. We'll use examples based on a typical channel/video schema.

### Simple entity queries

#### **Fetch list of entities**

Fetch a list of channels:

```graphql theme={"system"}
query {
  channels {
    id
    handle
  }
}
```

or, using a [newer](../overview#supported-queries) and [more advanced](./paginate-query-results) `{entityName}sConnection` query

```graphql theme={"system"}
query {
  channelsConnection(orderBy: id_ASC) {
    edges {
      node {
        id
        handle
      }
    }
  }
}
```

#### **Fetch an entity by ID**

OpenReader generates one single-row query per entity. It accepts an ID directly:

```graphql theme={"system"}
query {
  channelById(id: "1") {
    id
    handle
  }
}
```

To find a row by another `@unique` field, use the list or connection query with
a `where` filter. OpenReader does not generate `ByUniqueInput` root fields.

<h3 id="filter-query-results-search-queries">
  Filter query results / search queries
</h3>

#### **The `where` argument**

You can use the `where` argument in your queries to filter results based on some field's values. You can even use multiple filters in the same `where` clause using the `AND` or the `OR` operators.

For example, to fetch data for a channel named `Joy Channel`:

```graphql theme={"system"}
query {
  channels(where: { handle_eq: "Joy Channel" }) {
    id
    handle
  }
}
```

Note that `{entityName}sConnection` queries support exactly the same format of the `where` argument:

```graphql theme={"system"}
query {
  channelsConnection(orderBy: id_ASC, where: { handle_eq: "Joy Channel" }) {
    edges {
      node {
        id
        handle
      }
    }
  }
}
```

#### **Supported Scalar Types**

SQD supports the following scalar types:

* String
* Int
* Float
* BigInt
* Boolean
* Bytes
* DateTime
* BigDecimal
* JSON

#### **Equality Operators (`_eq`)**

`_eq` is supported by all the scalar types.

The following are examples of using this operator on different types:

* Fetch a list of videos where `title` is "Bitcoin"
* Fetch a list of videos where `isExplicit` is "true"
* Fetch a list of videos `publishedOn` is "2021-01-05"

```graphql theme={"system"}
query Query1 {
  videos(where: { title_eq: "Bitcoin" }) {
    id
    title
  }
}

query Query2 {
  videos(where: { isExplicit_eq: true }) {
    id
    title
  }
}

query Query3 {
  videos(where: { publishedOn_eq: "2021-01-05" }) {
    id
    title
  }
}
```

#### **Greater than or less than operators (`gt`, `lt`, `gte`, `lte`)**

The `_gt` (greater than), `_lt` (less than), `_gte` (greater than or equal to), `_lte` (less than or equal to) operators are available on `ID`, `String`, `Int`, `BigInt`, `BigDecimal`, `Float`, and `DateTime` fields.

The following are examples of using these operators on different types:

* Fetch a list of videos published before "2021-01-05"
* Fetch a list of channels before block "999"

```graphql theme={"system"}
query Query1 {
  videos(where: { publishedOn_gte: "2021-01-05" }) {
    id
    title
  }
}

query Query2 {
  channels(where: { block_lte: "999" }) {
    id
    handle
  }
}
```

#### **Text search or pattern matching operators (`_contains`, `_startsWith`, `_endsWith`)**

The `_contains`, `_startsWith`, `_endsWith` operators are used for pattern matching on string fields.

Example:

```graphql theme={"system"}
query Query1 {
  videos(where: { title_contains: "Bitcoin" }) {
    id
    title
  }
}

query Query2 {
  videos(where: { title_endsWith: "cryptocurrency" }) {
    id
    title
  }
}
```

## Operator reference

Every scalar field supports `_isNull`, `_eq`, and `_not_eq`. The comparable
types listed above also support `_in` and `_not_in`. `ID` and `String` add
`_contains`, `_not_contains`, `_containsInsensitive`,
`_not_containsInsensitive`, `_startsWith`, `_not_startsWith`, `_endsWith`, and
`_not_endsWith`.

Enum fields support `_isNull`, `_eq`, `_not_eq`, `_in`, and `_not_in`. Arrays
of scalars or enums support `_isNull`, `_containsAll`, `_containsAny`, and
`_containsNone`. JSON fields support `_jsonContains` and `_jsonHasKey`.

Relation fields can be filtered with a nested `where` object. Foreign-key
relations also expose filters on the generated `{fieldName}Id` value. Inverse
list relations support `_every`, `_some`, and `_none`.
