Extending SDL’s & Services generated in Redwood
TL;RD: Should we extend the generated service.js
and sdl.js
to achieve more complex GraphQL schemas?
Having worked with Nexus one thing I found great about the coupling to Prisma was the expressive SDL. We can in very few lines specify that we allow ordering, filtering
etc. With a simple schema.prisma
such as the following:
model Post {
id String @default(cuid()) @id
title String
body String
createdAt DateTime @default(now())
comments Comment[]
}
model Comment {
id String @default(cuid()) @id
content String
createdAt DateTime @default(now())
Post Post? @relation(fields: [postId], references: [id])
postId String?
}
I want to have generated post.sdl.js
and comment.sdl.js
that allows filtering, cursor, ordering any much more. I believe that we can do this with quite easily using the DMMF.Schema
definition of Prisma
.
My short PoC can lead to the following comment.sdl.js
:
...
type Query {
findManyPost(
where: PostWhereInput
orderBy: PostOrderByInput
skip: Int
after: PostWhereUniqueInput
before: PostWhereUniqueInput
first: Int
last: Int
): [Post]!
aggregatePost: AggregatePost!
findOnePost(where: PostWhereUniqueInput!): Post
findManyComment(
where: CommentWhereInput
orderBy: CommentOrderByInput
skip: Int
after: CommentWhereUniqueInput
before: CommentWhereUniqueInput
first: Int
last: Int
): [Comment]!
findOneComment(where: CommentWhereUniqueInput!): Comment
}
input PostWhereUniqueInput {
id: String
}
...
And many more properties Full SDL Pastebin. I believe that this addition to the SDL is beneficial for more complex queries that one may want to create. I am not aware of any issues with this, but maybe some one has other opinions.
The main reason for this post is to understand:
- I see that some one has gone through quite to trouble to make the generated SDL more readable, this will probably need to be adapted to cater for that
- Is this something that we would like to add and in that case how to ensure this? Do we write a package ourselves or can we use the types defined by Prisma
- What happens if I want fields in my SDL that are not mapped in my database?
In addition, to the sdl.template
one has to change the generated service.js
. I did not write any code for that yet, but manually changed the input arguments and it seems straight forward.
Let me know your thoughts. I am of course happy to share my progress so far if that’s of any help.
PS: Redwood was my first ever OSS contribution and loving the progress so far!