-
-
Notifications
You must be signed in to change notification settings - Fork 676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for lists with nullable elements #119
Comments
Yes, this is by design. A thought about this for a while and I couldn't figure out a legit use case for returning array of e.g. integers mixed with empty
|
In my case I'm building a game and I have a fixed length array which filled with null values by default. As the game progress I need to mutate values at certain indexes. So I think one might need this feature where there is a fixed length of array filled with null values, index matters and array contains non-scalar type. Something like:
There are workarounds but this structure feels most natural. |
I think that your app should abstract your internal, array representation from the public API. In GraphQL there's no array, only lists ( So you should use the fieldResolver pattern to transform an array to list of entries: type GameArrayItem {
index: Int!
value: GameItemValue!
}
type GameItemValue {
number: Int!
color: String!
} So you query should return list of type Query {
items: [GameArrayItem!]!
item(index: Int!): GameArrayItem
}
query GetItems {
items {
index
value {
number
color
}
}
}
query GetItem1 {
item(index: 1) {
value {
number
color
}
} And mutation should operate on type Mutation {
gameProgress(index: Int!, value: GameProgressInput): Boolean
}
input GameProgressInput {
number: Int!
color: String!
}
mutation ProgressGame {
gameProgress(index: 1, value: {
number: 55
color: "blue"
})
} Not on the whole list replacing items by itself. |
You are right. This makes sense. Normally I would like to avoid such abstractions but I think this is how I should handle things in Graphql. |
@19majkel94 On a side note I still think |
@erencay You're right, I should keep 100% compatibility with GraphQL specs. So this one will be done but with the lowest priority. And I have to find a good API for this case 😉 |
Actually when working on one of the projects I needed such a feature. Let's imagine that your database has some holes, some references that does not point to any objects. For example in project for our client we had something like this (it's not real example but just illustration):
and we had to implement GraphQL endpoint for such a data. So we had schema: type Order {
id: ID!
users: [User]!
} PS. It's not a real example but I just wanted to present that this case might happen |
That's the point. I haven't found good enough real example too 😃 If you really need it - feel free to open a PR 😉
|
any progress on this issue? we have same problem here to generate "nullable element" array in graphql schema. |
@kenpusney |
@19majkel94 please review. #211 Thanks. |
Closing this via #211 🔒 |
I'm having this issue with resolver arguments:
Let's say I want to ask for a certain range of messages, but I could ask for something like: [null, new Date()] which means anything until now. I'm getting an Argument Validation Error, I also tried with [Int] and [String] so Date is not the problem. It's not a pressing issue, but do you know if GraphQL even allow this type of arguments? |
@aounleonardo |
So the {nullable: "itemsAndList"} doesn't have any effect on what class-validator does? My bad then.. If I turn validate to false in buildSchema it works 👍 |
AFAIK there is currently no support for lists with nullable elements, such as
[String]!
.Seems like this method forces all lists to have non-null elements.
Appopriate solutions would be either adding a null value to value array
@Field(type => [String, null])
Or providing a decorator option such as;
@Field(type => [String], { nullable: true, nullableElement: true })
@19majkel94 would you like me to make a pull request?
The text was updated successfully, but these errors were encountered: