prisma / prisma1

💾 Database Tools incl. ORM, Migrations and Admin UI (Postgres, MySQL & MongoDB) [deprecated]
https://v1.prisma.io/docs/
Apache License 2.0
16.55k stars 863 forks source link

Add computed fields for complex data types #5069

Closed friebetill closed 4 years ago

friebetill commented 4 years ago

Problem

It is not possible to create a computed field with complex data. With native data types it is no problem, but with complex data types, Prisma says that there must be a relation.

type User {
  id: ID! @unique
  stories: [Story]! @relation(name: "stories")
  latestStory: Story
}

type Story {
  id: ID! @unique
  text: String!
  author: User! @relation(name: "stories", link: INLINE)
  createdAt: DateTime! @createdAt
}

In this example, Prisma complains that latestStory is not possible because the relation is missing. This is problematic for me, because at the beginning of my project, each User could only have one Story, but I later changed that so that one User can have multiple Stories. But now I still have clients that expect one Story. So for backwards compatibility I have to keep it.

To avoid duplication in the database schema, I would not like to add another relation between User and Story.

Solution

A solution could be to have computed fields with complex data types. Then one can define in the resolver how the complex data type should be computed.

For example, it could look like this in Prisma:

type User {
  id: ID! @unique
  stories: [Story]! @relation(name: "stories")
  latestStory: Story @relation(link: COMPUTED)
}

or

type User {
  id: ID! @unique
  stories: [Story]! @relation(name: "stories")
  latestStory: Story
}
dodas commented 4 years ago

I am not sure I understand your issue but it seems that you want to have graphql field latestStory that does not exist in your datamodel / database.

You don't need to define latestStory in your prisma schema. Just add resolver to your graphql server that will use stories from your database.

I don't know how you are building your graphql schema (nexus maybe?), so I cannot help further.

friebetill commented 4 years ago

I am not sure I understand your issue but it seems that you want to have graphql field latestStory that does not exist in your datamodel / database.

Yes exactly, I want to have a GraphQL Field that does not exist in the database.

You don't need to define latestStory in your prisma schema. Just add resolver to your graphql server that will use stories from your database.

I tried your suggestion that I only define the resolver and have latestStory not in the schema, but unfortunately I get the following error message when trying to start the server:

[ERROR] 11:30:48 Error: User.latestStory defined in resolvers, but not in schema

I don't know how you are building your graphql schema (nexus maybe?), so I cannot help further.

To generate the schema I use prisma generate and graphqlgen with

Prisma CLI version: prisma/1.34.0 (darwin-x64) node-v13.8.0
graphqlgen 0.6.0-rc9

I guess the software is outdated by now. Unfortunately there are so many systems (Prisma, graphqlgen, prisma2, graphcool, nexus, ...) that I lost the overview and have no idea what I should use by now. I hope Prisma 2 will make things clearer.

If you can give me a suggestion which tools support this feature, I would be very happy to update my software.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 10 days if no further activity occurs. Thank you for your contributions.