Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,90 @@ updateUser: (parent, args, ctx: Context) => {
})
},
```

## The effect of <inlinecode>null</inlinecode> and <inlinecode>undefined</inlinecode> on conditionals

There are some caveats to filtering with conditionals which might produce unexpected results. When filtering with conditionals you might expect one result but receive another given how Prisma treats nullable values.

The following table provides a high-level overview of how the different operators handle 0, 1 and `n` filters.


|Operator | 0 filters | 1 filter | n filters |
|-----------|------------------------|------------------------|----------------------|
|`OR` | return empty list | validate single filter | validate all filters |
|`AND` | return all items | validate single filter | validate all filters |
|`NOT` | return all items | validate single filter | validate all filters |

This example shows how an `undefined` parameter impacts the results returned by a query that uses the [`OR`](../../../reference/api-reference/prisma-client-reference#or) <span class="api"></span> operator.

```ts
interface FormData {
name: string;
email?: string;
}

const formData: FormData = {
name: 'Emelie'
}

const users = await prisma.user.findMany({
where: {
OR: [
{
email: {
contains: formData.email
}
}
]
}
})

// returns: []
```

The query receives filters from a formData object, which includes an optional email property. In this instance, the value of the email property is `undefined`. When this query is run no data is returned.

This is in contrast to the [`AND`](../../../reference/api-reference/prisma-client-reference#and) <span class="api"></span> and [`NOT`](../../../reference/api-reference/prisma-client-reference#not) <span class="api"></span> operators, which will both return all the users
if you pass in an `undefined` value.

> This is because passing an `undefined` value to an `AND` or `NOT` operator is the same
as passing nothing at all, meaning the `findMany` query in the example will run without any filters and return all the users.

```ts
interface FormData {
name: string;
email?: string;
}

const formData: FormData = {
name: 'Emelie'
}

const users = await prisma.user.findMany({
where: {
AND: [
{
email: {
contains: formData.email
}
}
]
}
})

// returns: { id: 1, email: 'ems@boop.com', name: 'Emelie' }

const users = await prisma.user.findMany({
where: {
NOT: [
{
email: {
contains: formData.email
}
}
]
}
})

// returns: { id: 1, email: 'ems@boop.com', name: 'Emelie' }
```