question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma resolves only the last element of a nested `include` query

See original GitHub issue

Bug description

Not sure if I’m missing something or there is a bug.

I have a basic localization design for a Project entity (including only relevant parts of the schema):

Prisma schema
model Partner {
  revisionId          BigInt               @id @default(autoincrement()) @map(name: "revision_id")
  id                  Int                  @default(autoincrement())
  createdAt           DateTime             @default(now()) @map(name: "time") @db.Timestamptz(6)
  updatedAt           DateTime             @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(6)
  name                String
  logo                String?
  projectPartnerTypes ProjectPartnerType[]

  @@map(name: "audit_organization")
}

model Project {
  id                     BigInt                         @id @default(autoincrement())
  createdAt              DateTime                       @default(now()) @map(name: "created_at") @db.Timestamptz(6)
  updatedAt              DateTime                       @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(6)
  published              Boolean                        @default(false)
  translations           ProjectTranslation[]

  @@map(name: "projects")
}

model ProjectPartnerType {
  id                   BigInt              @id @default(autoincrement())
  createdAt            DateTime            @default(now()) @map(name: "created_at") @db.Timestamptz(6)
  updatedAt            DateTime            @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(6)
  priority             Int                 @default(1)
  name                 String
  projectTranslationId BigInt              @map("project_translation_id")
  projectTranslation   ProjectTranslation? @relation(fields: [projectTranslationId], references: [id])
  partners             Partner[]

  @@map(name: "project_partner_types")
}

model ProjectTranslation {
  id                  BigInt                      @id @default(autoincrement())
  projectId           BigInt                      @map(name: "project_id")
  locale              Locale
  name                String
  contactEmail        String?                     @map(name: "contact_email")
  partnerTypes        ProjectPartnerType[]
  project             Project?                    @relation(fields: [projectId], references: [id])

  @@unique([projectId, locale])
  @@map(name: "project_translations")
}

I have a resolver for the Project entity:

GraphQL resolver
project: async (
      _root: any,
      { id, published, locale }: QueryProjectArgs
    ): Promise<Project | null> => 
      prisma.project.findFirst({
        where: {
          AND: [
            { id },
            published === true
              ? {
                  published: true,
                }
              : {},
          ],
        },
        include: {
          translations: {
            where: locale ? { locale } : {},
            include: {
              partnerTypes: {
                include: {
                  partners: true,
                },
              },
            },
          },
        },
      }),

Application has 2 locales: sk-SK and cs-CZ. And if you don’t pass the locale param to this resolver, it should return project with all translations. Which it does, except it doesn’t resolve nested partners inclusion for all translations, it does so only for the last element of the resulted project.translations array. Below are sample queries with their results:

1. When locale is specified

query {
  project(id: 20, locale: sk) {
    translations {
      id
      partnerTypes {
        id
        partners {
          id
          name
        }
      }
    }
  }
}
Result
{
  "data": {
    "project": {
      "translations": [
        {
          "id": 39,
          "partnerTypes": [
            {
              "id": 108,
              "partners": [
                {
                  "id": 5,
                  "name": "Johns and Sons"
                },
                {
                  "id": 12,
                  "name": "Pollich - Zieme"
                },
                {
                  "id": 14,
                  "name": "Grant, Cole and Waelchi"
                }
              ]
            },
            {
              "id": 109,
              "partners": [
                {
                  "id": 1,
                  "name": "Willms - Renner"
                },
                {
                  "id": 4,
                  "name": "Kiehn, Mante and Greenholt"
                },
                {
                  "id": 6,
                  "name": "Miller, Kohler and Doyle"
                },
                {
                  "id": 9,
                  "name": "Goodwin - Stokes"
                },
                {
                  "id": 10,
                  "name": "Mayert - Morissette"
                },
                {
                  "id": 13,
                  "name": "King - Bergnaum"
                },
                {
                  "id": 17,
                  "name": "Morissette - Grimes"
                }
              ]
            },
            {
              "id": 110,
              "partners": [
                {
                  "id": 7,
                  "name": "Schaden and Sons"
                },
                {
                  "id": 16,
                  "name": "Medhurst Group"
                },
                {
                  "id": 18,
                  "name": "Huels - Lemke"
                }
              ]
            }
          ]
        }
      ]
    }
  }
}

Partner type ids: 108, 109, 110.

2. When locale is not specified

Same query without locale param:

query {
  project(id: 20) {
    translations {
      id
      partnerTypes {
        id
        partners {
          id,
          name
        }
      }
    }
  }
}
Result
{
  "data": {
    "project": {
      "translations": [
        {
          "id": 39,
          "partnerTypes": [
            {
              "id": 108,
              "partners": []
            },
            {
              "id": 109,
              "partners": []
            },
            {
              "id": 110,
              "partners": []
            }
          ]
        },
        {
          "id": 40,
          "partnerTypes": [
            {
              "id": 111,
              "partners": []
            },
            {
              "id": 112,
              "partners": [
                {
                  "id": 1,
                  "name": "Willms - Renner"
                },
                {
                  "id": 4,
                  "name": "Kiehn, Mante and Greenholt"
                },
                {
                  "id": 5,
                  "name": "Johns and Sons"
                },
                {
                  "id": 6,
                  "name": "Miller, Kohler and Doyle"
                },
                {
                  "id": 8,
                  "name": "Reynolds, Vandervort and Abbott"
                },
                {
                  "id": 9,
                  "name": "Goodwin - Stokes"
                },
                {
                  "id": 10,
                  "name": "Mayert - Morissette"
                },
                {
                  "id": 11,
                  "name": "Schinner - Spencer"
                },
                {
                  "id": 12,
                  "name": "Pollich - Zieme"
                },
                {
                  "id": 13,
                  "name": "King - Bergnaum"
                },
                {
                  "id": 14,
                  "name": "Grant, Cole and Waelchi"
                },
                {
                  "id": 15,
                  "name": "Tillman, Fritsch and Raynor"
                },
                {
                  "id": 17,
                  "name": "Morissette - Grimes"
                }
              ]
            },
            {
              "id": 113,
              "partners": [
                {
                  "id": 7,
                  "name": "Schaden and Sons"
                },
                {
                  "id": 16,
                  "name": "Medhurst Group"
                },
                {
                  "id": 18,
                  "name": "Huels - Lemke"
                }
              ]
            }
          ]
        }
      ]
    }
  }
}

I feel like I’m missing something obvious here…

How to reproduce

  1. Create a schema as described above.
  2. Write a resolver with a nested include.
  3. Run resolver.
  4. Receive incomplete data.

Expected behavior

To get all data, including deeply nested fields.

Prisma information

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version: 14.15.4

Prisma Version

v3.3.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
nizadincommented, Jan 4, 2022

Resolved in 3.7.0

0reactions
nizadincommented, Nov 19, 2021

Issue still persists on version 3.5.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relation queries (Concepts) - Prisma
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Read more >
How to resolve a nested item in a graphql query - Stack Overflow
It only resolves the scalar values, id and quantity . The item field is a CartItem type: type CartItem { id: ID! quantity:...
Read more >
Prisma silently misses nested elements on MariaDB when it ...
I stumbled upon an issue when trying to perform a findMany query that included nested relations. What I tried to achieve is to...
Read more >
Resolvers - Apollo GraphQL Docs
The resolver map has top-level fields that correspond to your schema's types (such as Query above). Each resolver function belongs to whichever type...
Read more >
How can I call around in my api? (solved)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found