Given schema:
type Contact {
id: ID
name: String
}
This will generate the input type:
input ContactFilter {
id: [ID!]
}
But the ID is not really required. The following is a valid query:
{
queryContact(filter: {id: []}) {
id
name
}
}
And the expected result is return no nodes because no IDs were provided.
However, if I try to put this into a variable:
query ($ids: [ID]!) {
queryContact(filter: {id:$ids}) {
id
name
}
}
And that fails because the input type is mismatched and I get:
Variable type provided [ID]! is incompatible with expected type [ID!]
However, if I correct this, and make it:
query ($ids: [ID!]!) {
queryContact(filter: {id:$ids}) {
id
name
}
}
with variables:
{
"ids": []
}
Then the query runs successfully and returns an empty set as expected, but with no error. If the ID really was required as it said, then it should return an error that the input ids
was empty.
Solution, just change the generated input type to [ID]
instead of [ID!]
As I understand the specification:
ids: [ID]
means that the input does not have to exist, and if it is an array then it can be an empty array.ids: [ID]!
means that the input does have to exist (single item not in list gets converted to a list of itself), and it can be an empty arrayids: [ID!]
means that the input does not have to exist, but if it does then it cannot be an empty arrayids: [ID!]!
means that the input does have to exist, and it cannot be an empty array.
Update:
They can all be empty arrays, but [ID] can contain elements that are null whereas [ID!] can’t.
So the corrected understanding is:
-
ids: [ID]
means that the input does not have to exist, and if it is an array then it can be an empty array, and it can contain null values.Valid Inputs:
ids: undefined ids: null ids: [] ids: [null, "0x1", 2, ...]
-
ids: [ID]!
means that the input does have to exist (single item not in list gets converted to a list of itself), and it can be an empty array, and it can contain null values.Valid Inputs:
ids: [] ids: [null, "0x1", 2, ...]
Invalid Inputs:
ids: undefined ids: null
-
ids: [ID!]
means that the input does not have to exist, but if it does then it cannot contain null values, but it can be an empty arrayValid Inputs:
ids: undefined ids: null ids: [] ids: ["0x1", 2, ...]
Invalid Inputs:
ids: [null, ...]
-
ids: [ID!]!
means that the input does have to exist, and it cannot contain null values, but it can be an empty array.Valid Inputs:
ids: [] ids: ["0x1", 2, ...]
Invalid Inputs:
ids: undefined ids: null ids: [null, ...]