Query with Relations

prisma

#1

Hi all,

I am having an issue with relations and querying data.
I have the following datamodel:

type Asset {
  id: ID! @id
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
  version: Int! @default(value: 1)
  title: String!
  description: String
  amount: Float

  user: User!
  assetType: AssetType!
  quote: [Quote!]!
  policy: [Policy!]!
  assetMedia: [AssetMedia!]
}

There are 5 non-scalar data types on there, does this mean that to get the full “row” from the db I need to do:

const asset = await prisma.asset({ id: id });
const user = await prisma.asset({ id: id }).user();
const policies = await prisma.asset({ id: id }).policy();
const quotes = await prisma.asset({ id: id }).quote();
const assetMedia = await prisma.asset({ id: id }).assetMedia();
const assetType = await prisma.asset({ id: id }).assetType();

const assetRow = {
  ...asset,
  user: user.id,
  assetType: assetType.id,
  quote: quotes.map(quote => quote.id),
  policy: policies.map(policy => policy.id),
  assetMedia: assetMedia.map(assetMedia => assetMedia.id)
}

am I missing something?


#2

The other way I thought of doing this is using $fragments:

const asset = await prisma.asset({ id: id }).$fragment(`
  fragment WithAllAssetDetails on asset { 
  id
  createdAt
  updatedAt
  version
  title
  description
  amount

  user {id}
  assetType {id}
  quote {id}
  policy {id}
  assetMedia {id}
`);

Then I’d need to create my own typescript type , somethng like:

import {  Asset } from '../generated/prisma-client';

export interface AssetWithStubs extends Asset {
  user: { id: string };
  assetType: { id: string };
  quote: string[];
  policy: string[];
  assetMedia: string[];
}

I’m not too keen on this at all as if the dataModel changes then there needs to be the mental check to ensure that all of the fragments and types are updated accordingly.
Multiply this by a couple more tables with a couple more non-scalars and it seems like it will easily become an admin nightmare…