Skip to content

hooks doesn't provide many relationship's data in item arg #8710

@wysher

Description

@wysher

all hooks have access to item and for most cases it works fine, but for many relation fields it doesn't include any data for them (or even a key). No matter if hook is part of field config or list config.

Here's example schema for Post:

{
  title: text(),
  author: relationship({ ref: 'Author.posts', many: false }),
  tags: relationship({ ref: 'Tag.posts', many: true })
}

when using hook, let's say validateInput, item arg looks like this:

{
  id: 'clkcqc10f0001923p6b00ufp9',
  title: 'First post',
  authorId: 'clkcq9fqq0000923prkji3bi6'
}

but there's no tags here.

Under the hood, item comes from async prisma.findFirst method. By design this method returns only ids for single relationship fields (like authorId above).

As a developer I would like to get all the information for item, but to get them I need to call another prisma.findFirst within validateInput hook. So there's two async calls for the "same" data.

I change some keystone code to have those data, which additionally passes to prisma.findFirst

include: {
  [key]: {
      select: { id: true }
    }
}

for missing many relationships and map them, so the result is:

{
  id: 'clkcqc10f0001923p6b00ufp9',
  title: 'First post',
  authorId: 'clkcq9fqq0000923prkji3bi6',
  tagsIds: [ 'clkcqgwtc000092nau201v3vy' ]
}

Here's the code:
https://github.com/wysher/keystone/pull/1/files#diff-cafe5f9fd16c4aff83f4b1bc269237512a4e3ea5ef17e6f48e97b49e3537b3aaR33

I'm not sure if you deliberately doesn't include relationship data here for performance reasons.

If it's the case, my proposal here will be to refactor item a little bit, and call prisma.findFirst on demand. item may be a function, or to not have breaking changes we can use object getter.

Let me know what do you think of change I made. If it's ok with you I'll make proper pull request.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions