Seems setting defaultValue to null by default is not good practice. Null eventually is a value, so graphql validator which mustn't change behavior actually changes it: before validation application thought that if key doesn't exists in input then its ok, after validation the key appears with the null value. Then this null value goes to be written to DB by default.
Also, default value in graphql is probably harmful at all: consider if I need to make update request to mongoDB. For example I have next schema:
{
name: { type: String },
comment: { type: String, default: '' },
}
I send request to save data in that collection, after request is sent I have in my DB next document:
{
_id: ObjectId('someid'),
name: 'My name',
comment: 'Blabla',
}
Then I send update request through graphql mutation to update just name. But graphql will automatically add comment: '' to the request. So comment will be erased in DB. Database doesn't know whether that empty string was a default value or new value so that you want to erase your data.
@leebyron, @IvanGoncharov , let me know your thoughts.
@deser GraphQL is a query language for your API not for your database. It's up to you to decide how to use default values and how to map this semantic to the database of your choice.
Also please use other platforms to ask general questions about GraphQL and its best practices:
If you have a question on how to use GraphQL, please post it to Stack Overflow with the tag #graphql.
Please do not post general questions directly as GitHub issues. They may sit for weeks unanswered, or may be spontaneously closed without answer.
Yes, but specification is wrong. My API can't understand whether the value which is coming is default or not, so to really understand that I have to go to the DB\make API call, look whether the value is presented and if not - apply the default coming from graphql. Obviously normal developer wouldn't do that due to many reasons. As well as my API can't understand whether null value for key is coming from the mutation input or the key is absent in input and null shouldn't go further (to API\DB)
PIty that you don't understand.
@deser Same as #2155 if you think graphql-js behaves incorrectly please at least provide an example to reproduce this behavior and I will try my best to fix it ASAP.
If you want to discuss certain features of GraphQL and their usefulness please ask the community on one of 3rd-party platforms like StackOverflow.
Most helpful comment
@deser Same as #2155 if you think
graphql-jsbehaves incorrectly please at least provide an example to reproduce this behavior and I will try my best to fix it ASAP.If you want to discuss certain features of GraphQL and their usefulness please ask the community on one of 3rd-party platforms like StackOverflow.