Search code examples
prismaprisma-graphql

Define required or not for array fields in the prisma datamodel


What are the differences of followings. when to use a one over the other?

zones: [Zone]
zones: [Zone!]
zones: [Zone]!
zones: [Zone!]!

Solution

  • This can be summarised with this table of allowed values based on the definition:

    values         | [Zone] | [Zone!] | [Zone]! | [Zone!]! |
    --------------------------------------------------------
    null           |    ✔   |    ✔    |    X    |     X    |
    []             |    ✔   |    ✔    |    ✔    |     ✔    |
    [null]         |    ✔   |    X    |    ✔    |     X    |
    ["a","b"]      |    ✔   |    ✔    |    ✔    |     ✔    |
    ["a",null,"c"] |    ✔   |    X    |    ✔    |     X    |
    

    Most of the time, you will need to use [Zone!]! as it ensures that no null values will be found in your array.