How do I make a field nullable? I mean that I have...
# prisma-client
r
How do I make a field nullable? I mean that I have a simple relation for a table that contains items related to an asset. If the table is empty I get an error "Cannot return null for non-nullable field". So I have a
Copy code
@ResolveField()
async items(@Parent() asset: Asset, @Args('first') first: number) {
    return this.prisma.asset.findUnique({ where: { id: asset.id } }).items({ take: first });
}
So the schema is just along the lines of
Copy code
model Asset {
    id Int @id
    ...
    items Item[]
}
I tried with Item?[] and Item[]? but that is not a correct syntax 🙂
The graphql definition is along the lines of
Copy code
type Asset {
    id: Int!
    items(first: Int): [Event!]!
}
So is the ! the magic here. Will it work if I remove those?
Apparently so 🙂
Thanks for everyone who helped!
o
In the graphql schema the ! means that the field is non nullable. In the case of arrays, there is a double meaning, as
[Event!]
means that list itself is nullable where as the elements are not, if you wanted to have both the list and elements non null it would be
[Event!]!
. As for the prisma schema, i encountered the same thing but have deduced that the arrays are nullable by default, meaning that in your case if items are null it will just return null.
Are you using nestjs? In the code first approach there when you define your object types you can specify
{nullable: 'items' | 'itemsAndList' | true