-
|
create a list like so: export const Post = list({
access: allowAll,
fields: {
title: text({ validation: { isRequired: true } }),
name: text({ validation: { isRequired: true } }),
label: text({ validation: { isRequired: true } }),
created_at: timestamp({ defaultValue: { kind: 'now' } }),
updated_at: timestamp({ defaultValue: { kind: 'now' }, db: { updatedAt: true } }),
},
})If I go to http://localhost:3000/api/graphql, I would expect to find this in the graphql schema: type Post {
id: ID!
title: String!
name: String!
label: String!
created_at: DateTime
updated_at: DateTime
}
input PostCreateInput {
title: String!
name: String!
label: String!
created_at: DateTime
updated_at: DateTime
}
# etc.What I am actually getting is this: type Post {
id: ID!
title: String
name: String
label: String
created_at: DateTime
updated_at: DateTime
}
input PostCreateInput {
title: String
name: String
label: String
created_at: DateTime
updated_at: DateTime
}
# etc.There are no exclamation marks 😞 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Hey there. This is because, even though fields are required, you might restrict access to them and they will return null. So you schema should look like: export const Post = list({
access: allowAll,
fields: {
title: text({ validation: { isRequired: true }, graphql: { read: { isNonNull: true } } }),
name: text({ validation: { isRequired: true }, graphql: { read: { isNonNull: true } } }),
label: text({ validation: { isRequired: true }, graphql: { read: { isNonNull: true } } }),
created_at: timestamp({ defaultValue: { kind: 'now' }, graphql: { read: { isNonNull: true } } }),
updated_at: timestamp({ defaultValue: { kind: 'now' }, db: { updatedAt: true }, graphql: { read: { isNonNull: true } } }),
},
})EDIT: As for create input type, there is also equivalent create setting, but the output type will be something like: title: String! = ""As far as I know there is no way to force non-null value without default value at the moment (at least for autogenerated stuff - in custom query/mutation you can do whatever you want). If required field is not provided, it will fail in runtime during validation. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your speedy reply! 🙏 I get why the ability to return null would affect the export const Post = list({
access: allowAll,
fields: {
title: text({ validation: { isRequired: true }, graphql: { create: { isNonNull: true } } }),
name: text({ validation: { isRequired: true }, graphql: { create: { isNonNull: true } } }),
label: text({ validation: { isRequired: true }, graphql: { create: { isNonNull: true } } }),
created_at: timestamp({ defaultValue: { kind: 'now' } }),
updated_at: timestamp({ defaultValue: { kind: 'now' }, db: { updatedAt: true } }),
},
})solves my immediate problem (typescript/codegen). 🥳 However, it's A LOT of code for something I think should be the default behaviour for fields with I don't really get what is going on in #8283, will this issue be solved there? |
Beta Was this translation helpful? Give feedback.
-
|
Dupe of #7736? |
Beta Was this translation helpful? Give feedback.
Hey there.
This is because, even though fields are required, you might restrict access to them and they will return null.
What you are looking for is described here (note though that not all fields support this).
So you schema should look like: