redwoodjs / redwood

The App Framework for Startups
https://redwoodjs.com
MIT License
17.17k stars 982 forks source link

[Feature Request] Improve SDL and Services #3446

Open HarunKilic opened 3 years ago

HarunKilic commented 3 years ago

The SDL and service generators are awesome! But they do not include nested relation creating/updating/deleting and etc.

Prisma provides manipulating nested relations with create, createMany, upsert, connect and etc. It would be nice if the generators could identify nested relations and generate those inputs.

Typegraphql-prisma is a good example

thedavidprice commented 2 years ago

This is definitely a good idea and we'd be interested in a POC or even pseudo code / example. However, this is unlikely to be a priority before v1.

Note: There might be an opportunity here to leverage the generator templates.

aggmoulik commented 2 years ago

Hey @thedavidprice, I would love to work on this, seems to be an interesting issue but need some help for the POC. Please tag the interested persons for this issue.

thedavidprice commented 2 years ago

Thanks for the interest @aggmoulik This would be a great feature to have! Just know it's not high priority (yet), which may mean the core team can't give you all the support needed to get across the finish line in the near term. But that doesn't mean I want to discourage you from taking a next step and trying a POC — it just means that we might have to put it on pause once we see what the implementation might require.

@cannikin has the best understanding of how the generators work with Prisma. Any gut reactions about what it might take to get something like this working, Rob?

cannikin commented 2 years ago

Ahh the nested data structure problem. This is one that scares me. How do you decide where to stop? For example:

Let's say I want to create the SDL & service for the User model. But User has many Orders. And Orders have many Line Items. And a Line Item has a Product, and a Product has Inventory...

How do you create the User SDL/Service, and include the nested types for create/update/etc., without also generating the SDL/services for all of those other relationships? You basically have to create them for the entire schema, otherwise you won't have the GraphQL types available to make them available for nested creates, upserts, etc.

We do a somewhat limited version of this now, where if there are relationships on a model we add them as types, but I believe if you try to request that sub-type without defining its own SDL, you'd get a GraphQL error:

Here is one where a User has many Addresses:

  type User {
    id: Int!
    name: String
    email: String!
    avatar: String
    addresses: [Address]!
    theme: String!
    createdAt: DateTime!
    updatedAt: DateTime!
  }

And in the User service we have the query to get addresses as well:

export const User = {
  addresses: (_obj, { root }) =>
    db.user.findUnique({ where: { id: root.id } }).addresses(),
}

But that's just for selecting in queries. Being able to create/update/connect/disconnect in mutations seems like it would be infinitely more complex. typegraphql-prisma looks like it creates everything for your whole schema, which I think we would need to do as well to get this to work.

But maybe I'm missing something...I'm no GraphQL expert so maybe it's composability allows for this kind of thing without all of the complexity I'm worried about above?