Skip to content

Fix create and update nested relational items marked as required instead of optional#25835

Merged
br41nslug merged 11 commits intodirectus:mainfrom
HPaulson:main
Sep 26, 2025
Merged

Fix create and update nested relational items marked as required instead of optional#25835
br41nslug merged 11 commits intodirectus:mainfrom
HPaulson:main

Conversation

@HPaulson
Copy link
Contributor

@HPaulson HPaulson commented Sep 15, 2025

Scope

Changes SDK createItem item type from item: Partial<UnpackList<Schema[Collection]>> to item: NestedPartial<UnpackList<Schema[Collection]>>, to address #21212 and bryantgillespie/directus-sdk-typegen#15.

Potential Risks / Drawbacks

N/A -- This just makes nested (relational) properties partial, instead of only the top-level item properties, so that items can be created using this structure on the docs.

Tested Scenarios

  • createItem on a collection with a One-To-Many, making sure that we can create an item in the related collection with no type error, as shown:
    const newCustomer = await directus.request(
      withToken(
       "",
        createItem('customer', {
          location: [{
            address: formData.address,
          }]
        })
      )
    );

I do not think any test or docs updates are required.

@HPaulson HPaulson requested a review from a team as a code owner September 15, 2025 16:06
br41nslug

This comment was marked as outdated.

@HPaulson
Copy link
Contributor Author

HPaulson commented Sep 16, 2025

Yes, of course! I updated the functions you mentioned. I also found these functions which were not on your list, but are POST and PATCH functions that take in an item with the Partial type very similar to those you had me update. Should these be updated also? I did not change them in my above commit assuming they were left out on your list intentionally. Perhaps you excluded these because they are system collections with no relations? Even in that case, I imagine NestedPartial might still be good for consistency and future-proofing in these functions. Wanted to ask to be sure.

  • createComment
  • createNotification
  • createPreset
  • createShare
  • createTranslation
  • createContentVersion
  • createWebhook
  • updateComment(s)
  • updateNotification
  • updatePreset(s)
  • updateSettings
  • updateShare(s)
  • updateSingleton
  • updateTranslation(s)
  • updateMe
  • updateContentVersion(s)
  • updateWebhook(s)
  • saveToContentVersion

@br41nslug
Copy link
Member

I indeed left a couple out because they have no real relations in them (some like notification do have a user relation but its not really expected to be creating the user while creating a notification)

@HPaulson
Copy link
Contributor Author

HPaulson commented Sep 16, 2025

I indeed left a couple out because they have no real relations in them (some like notification do have a user relation but its not really expected to be creating the user while creating a notification)

That’s understandable. My only concern is that this makes the SDK a bit opinionated about how developers “should” use Directus. I’d argue the SDK should support any usage the API itself allows, even if it’s uncommon.

This feels especially important because system collections, even those without default relations, can be extended with relations to custom collections. I’ve used Directus in some unusual setups myself, and it would be frustrating to switch to the SDK only to find that nested create/updates sometimes work, but not always. That’s essentially why I opened this PR, because the API supports a documented feature, but the SDK didn’t always allow it. Extending NestedPartial consistently across all collections where the API supports it would prevent similar issues from cropping up again.

Right now the changes in this PR feel inconsistent, since most POST and PATCH functions allow nested create/update, but some don’t. It’s the same situation we had before, where batch requests used NestedPartial but regular requests didn’t. I think this kind of inconsistency might cause confusion, and I don’t see much benefit unless the goal really is to keep the SDK opinionated.

Of course the decision is yours, I just wanted to share my perspective. Happy to update those extra functions if you think this reasoning holds, or leave them as is if not.

@ComfortablyCoding ComfortablyCoding changed the title Changes createItem type to NestedPartial. Fixes #21212 Fix create and update nested relational items marked as required instead of partial Sep 17, 2025
@ComfortablyCoding ComfortablyCoding changed the title Fix create and update nested relational items marked as required instead of partial Fix create and update nested relational items marked as required instead of optional Sep 17, 2025
@br41nslug

This comment was marked as outdated.

@HPaulson HPaulson requested a review from br41nslug September 19, 2025 15:52
Copy link
Member

@br41nslug br41nslug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have 1 final type change I'd want fixed and a nitpick on the changeset when those are resolved im happy to merge this! Thanks in advance for improving the consistency in the SDK types ❤️

@HPaulson HPaulson requested a review from br41nslug September 25, 2025 13:13
@br41nslug br41nslug merged commit 0fce562 into directus:main Sep 26, 2025
60 of 63 checks passed
@github-actions github-actions bot added this to the Next Release milestone Sep 26, 2025
u12206050 pushed a commit to u12206050/directus that referenced this pull request Oct 22, 2025
…ead of optional (directus#25835)

* Changes createItem type to NestedPartial; fixes directus#21212

* Updates changeset

* Adds `NestedPartial` to more POST/PATCH functions

* Update .changeset/beige-lamps-relate.md

Co-authored-by: daedalus <[email protected]>

* Update .changeset/beige-lamps-relate.md

* Fixes sdk item type on additional `POST` and `PATCH` functions

* Update .changeset/beige-lamps-relate.md

Co-authored-by: Brainslug <[email protected]>

* Removes unintended non-nullish type for `data`

* Update sdk/src/rest/commands/update/singleton.ts

---------

Co-authored-by: daedalus <[email protected]>
Co-authored-by: Brainslug <[email protected]>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants