Creating empty non-null array via graphql

I have the following schema

type Listing {
  address: String!
  images: [Image!]!
  zipCode: String!
}

type Query {
  allListings: [Listing!]!
}

When I run the following query:

mutation createListing {
  createListing(data: {
    images: [],
    zipCode: "90035"
    address: "1536 S. Sherbourne Dr."
  }) {
    address
  }
}

I get the following error: "Field 'images' is required and cannot be 'null'

From what I’ve read, an empty array is not considered null and should be valid for the [Image!]!. In short: why can’t I pass an empty array for the images property?

On a similar note, I noticed the graphql type for allListings is the following:

Why does data have a type of [Listing]! and not [Listing!]! since [Listing!]! is what I had specified in my schema?

Thank you for the help.

1 Like

Hi @bez,

having empty array if fine, but in your case you specify as:

 type Listing {
  address: String!
  images: [Image!]!
  zipCode: String!
}

in that case, an empty array violates the directive. The ! force to return at least one value/element and an empty array is not valid then.
You can try this way:

type Listing {
  address: String!
  images: [Image]
  zipCode: String!
}

to remove such constraint.
For your second question, this way

[Listing]!

allows you to query empty collection, since this

[Listing!]!

returns an error.

Hope this answers.

Luigi

Hi @bez,
Just checked internally, and even if the solution I provided actually works, also your should.
We already have a bug open for this, and we are working on fixing it.
Sorry for the inconvenient.

Luigi

Hi @Luigi_Servini,

Thanks for the reply. If I’m understanding correctly, this looks like a departure from the graphql spec.

Here is a quote from https://graphql.org/learn/schema/

[Episode!]! represents an array of Episode objects. Since it is also non-nullable , you can always expect an array (with zero or more items) when you query the appearsIn field. And since Episode! is also non-nullable , you can always expect every item of the array to be an Episode object.

In other words, [Image!]! does not mean that the array has to have at least one item. It just means that none of the items in the array will be null. Therefor an empty array should pass the graphql validation.

Am I understanding correctly? If so, is this a known deviation from the graphql spec?

Thanks again!

@Luigi_Servini I just saw your second comment. Thanks for the update. Is there anywhere where we can track the progress of issues? I guess something similar to GitHub issues?

At the moment, to my knowledge, we don’t have such a process in place. We do sometimes give out the ticket number to the user, so they can ask later on whether something has changed on this. However, I ticketed this fairly recently after receiving this user feedback (PROD-827) since I personally assume this is an inconsistency and assume that we can do better. That opinion is not yet confirmed by engineering and it’s not prioritized yet. That should give you an idea on whether you want to wait for it or adapt your queries. Given that there is a workaround I would personally not wait for it.