title: get a nested email field that is part of another model in prisma?
body: <p>i have schema that looks like:</p>
  <h3>schema.prisma</h3>
  <pre><code>generator client {
    provider = &quot;prisma-client-js&quot;
  }
  
  datasource db {
    provider = &quot;sqlite&quot;
    url      = &quot;file:./dev.db&quot;
  }
  
  model User {
    id       String  @id @default(cuid())
    email    String? @unique
    stripeId String  @unique
  
    createdAt DateTime @default(now())
  
    product Product?
  
    @@map(&quot;users&quot;)
  }
  
  model Product {
    id       String @id @default(cuid())
    totalSum Int    @default(9700)
  
    user   User   @relation(fields: [userId], references: [id])
    userId String @unique
  
    licenses License[]
  
    @@map(&quot;products&quot;)
  }
  
  model License {
    id    String @id @default(cuid())
    name  String @unique
    /// no. of licenses generated
    total Int    @default(1)
    /// no. of licenses used
    used  Int    @default(0)
    /// stored in cents
  
    product   Product? @relation(fields: [productId], references: [id])
    productId String?
  
    createdAt DateTime @default(now())
  
    @@map(&quot;licenses&quot;)
  }
  </code></pre>
  <p>i want to access <code>email</code> field while doing <code>prisma.license.findMany()</code>. my <code>db</code> file looks like:</p>
  <h3>db.ts</h3>
  <pre><code>import { Prisma, License, User } from '@prisma/client'
  
  import { prisma } from './context'
  
  export const getLicenses = async (): Promise&lt;
    Array&lt;License &amp; Pick&lt;User, 'email'&gt;&gt; | null | undefined
  &gt; =&gt; {
    const userSelect = Prisma.validator&lt;Prisma.ProductSelect&gt;()({
      user: {
        select: {
          email: true,
        },
      },
    })
  
    const productSelect = Prisma.validator&lt;Prisma.LicenseSelect&gt;()({
      product: {
        include: userSelect,
      },
    })
  
    const licenses = await prisma.license.findMany({
      orderBy: {
        createdAt: 'desc',
      },
      include: productSelect,
    })
  
    const result = licenses.map((license) =&gt; {
      const email = license.product?.user.email
  
      if (email) {
        return {
          ...license,
          email,
        }
      }
    })
  
    return result
  }
  
  export const db = {
    getLicenses,
  }
  </code></pre>
  <p>the last line <code>return result</code> gives this typescript error:</p>
  <pre class="lang-bash prettyprint-override"><code>Type '({ email: string; id: string; name: string; total: number; used: number; productId: string | null; createdAt: Date; product: (Product &amp; { user: { email: string | null; }; }) | null; } | undefined)[]' is not assignable to type '(License &amp; Pick&lt;User, &quot;email&quot;&gt;)[]'.
    Type '{ email: string; id: string; name: string; total: number; used: number; productId: string | null; createdAt: Date; product: (Product &amp; { user: { email: string | null; }; }) | null; } | undefined' is not assignable to type 'License &amp; Pick&lt;User, &quot;email&quot;&gt;'.
      Type 'undefined' is not assignable to type 'License &amp; Pick&lt;User, &quot;email&quot;&gt;'.
        Type 'undefined' is not assignable to type 'License'.ts(2322)
  </code></pre>
  <p>my schema file looks like:</p>
  <h3>schema.ts</h3>
  <pre><code>import { db } from './db'
  
  const Query = objectType({
    name: 'Query',
    definition(t) {
      t.list.field('licenses', {
        type: 'License',
        resolve: async (_, __, ctx) =&gt; {
          if (!ctx.admin.isLoggedIn) return null
          const licenses = await db.getLicenses()
  
          if (licenses) return licenses
          return null
        },
      })
    },
  })
  </code></pre>
  <p>even this little query is causing me a lot of errors. it used to work when i wanted to query all of licenses using <code>prisma.license.findMany()</code> but it started throwing errors as soon as i wanted <code>email</code> field but in a flat file format so my output looks like:</p>
  <pre><code>{
      id: string;
      name: string;
      total: number;
      used: number;
      productId: string | null;
      createdAt: Date;
      email: string;
  }
  </code></pre>
  <p>i also don't want <code>productId</code> to be sent. how can i solve this?</p>
  <p>i've made a minimal repro → <a href="https://github.com/deadcoder0904/prisma-nested-query-email" rel="nofollow noreferrer">https://github.com/deadcoder0904/prisma-nested-query-email</a></p>