graphback icon indicating copy to clipboard operation
graphback copied to clipboard

Default values and required inputs on create

Open lastmjs opened this issue 4 years ago • 5 comments

I am using alpha 7, I assume that the @default annotation should be working correctly? If not I have the following problem:

When creating an entity, a field that is marked with a default value annotation should not be required in the create input.

Example:

""" @model """
type User {
  id: ID!
  
  """ @default(value: 5) """
  numFriends: Int!
}

I should be able to write the following mutation:

mutation {
  createUser {
    id
  }

The default value should be provided behind-the-scenes, and I should not be required to put a value for numFriends in the input for the mutation.

lastmjs avatar Jun 25 '20 02:06 lastmjs

Hi @lastmjs thanks for reporting the issue.

When creating an entity, a field that is marked with a default value annotation should not be required in the create input.

Even though the user can still mark a field with default value as nullable, I think it makes sense to do so out of the box: Only for Postgres / relational database since that's the only DB where we support the @default annotation.

/cc @craicoverflow @wtrocki WDYT?

machi1990 avatar Jun 25 '20 07:06 machi1990

So it will not be required in the client side queries but will be always required to be returned from the database (which is obvious). If this is only client side issue then we can apply this quick fix. Although splitting between databases is hard here so we need to think about it in global scope.

wtrocki avatar Jun 25 '20 09:06 wtrocki

If this is only client side issue then we can apply this quick fix. Although splitting between databases is hard here so we need to think about it in global scope.

The default annotation only works with Postgres, so no need to think about how this would work for Mongo.

If there is a default annotation, make the input field nullable. If you have the default annotation on your model using Mongo that is a user misconfiguration.

craicoverflow avatar Jun 25 '20 09:06 craicoverflow

If this is only client side issue then we can apply this quick fix. Although splitting between databases is hard here so we need to think about it in global scope.

The default annotation only works with Postgres, so no need to think about how this would work for Mongo.

It got me thinking, if there was a way to have datasource e.g postgres to define a way to change the schema (think of plugin here), this could work transparently for Postgres and as for MongoDB this will always be non nullable if the user defines it that way.

machi1990 avatar Jun 25 '20 10:06 machi1990

Only for Postgres / relational database since that's the only DB where we support the @default annotation.

I think it is reasonable to do this for Mongo also. It would be a documented consequence of using the @default annotation. Users may also use their own data abstraction with Mongoose for example, which offers a way to use default values.

craicoverflow avatar Jun 30 '20 08:06 craicoverflow