Skip to content

CreateOrUpdateHookParams typing needs improvement #1447

@thekevinbrown

Description

@thekevinbrown

Describe the bug
When trying to create hooks the CreateOrUpdateHookParams<G> type is frustrating to work with.

  1. It has an entities property, but that property is not actually there at runtime, the correct one to be using is args.items.
  2. args.items is of type Partial<G>[], which is...kind of correct, but nested entities are not typed correctly as GQL inputs, for example if this entity has a many to one relationship with another, and you want to set the foreign key, you'd do this: params.args.items?.forEach((entity) => entity.relationshipProperty = { id: '5' }, which is perfectly valid, but the types tell you that you need to fill out all properties on this entity, which is not true.
  3. params.context is not typed well at all, and it's difficult to pull values out of the user context to reference in your hook.

To Reproduce
Steps to reproduce the behaviour:

Build a hook that sets the current user as createdBy on every entity of that type which is created.

Expected behavior
Expected types to correctly define the shape of the context, or at least provide a generic param for it. Also expected types to be aware of foreign key reference assignments.

Actual behavior
What actually happened.

Point 2:

Type '{ id: string; }' is missing the following properties from type 'OtherEntity': [all required fields](2740);

Point 3:

Property 'user' does not exist on type 'object'.ts(2339)

Metadata

Metadata

Assignees

No one assigned

    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