Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breaking]: architectural change to omit draft model __typename creation #478

Merged
merged 12 commits into from
Feb 9, 2024

Conversation

jaikamat
Copy link
Contributor

@jaikamat jaikamat commented Feb 7, 2024

Summary

This PR makes sweeping changes to draft models, and introduces a proposed ADR we can agree on going forward.

The problem

Note that the changes in this PR only apply to draft models, not base models.

While __typename is technically part of the GraphQL schema for draft/input types, using it in practice has proven challenging. When we build draft GraphQL models and attempt to directly submit them as variables via the CoCo API, we are met with 400 errors, because we build __typename on many draft models internally.

This has led to our team having to shim omitDeep functions in our sample data creation resolvers to strip __typename, and casting to appropriate types, which is neither type safe nor ideal. I suspect that some consumers using draft models will have to do the same.

Proposed solution

@ByronDWall had done great work trying to solve this here, however that raised questions about the very purpose of draft models.

We learned that models were originally designed without drafts, but as testing necessitated mocking requests, drafts were split out to represent input types (and as many of you know, GraphQL drafts may deviate wildly from their base REST representations, so splitting them out proved very maintainable).

Therefore, we're setting the precedent here that draft GraphQL models should adhere to their preferred utilitarian shape (which excludes __typename), rather than their strict schema representation. After all, this is a test data repository, and our form should follow function.

There was one lingering problem with this: Reference, KeyReference, and LocalizedString did not have draft representations, and always included __typename when built for GraphQL. They essentially had a "dual use case". If we allowed them to be built as-is, our live API requests would still fail.

Therefore, they have been extended to include their respective *Draft representations (which exclude __typename), and all points of implementation in draft models have been edited to use them.

Further, we have removed __typename from those draft models that built it.

Tests and snapshots have been updated accordingly.

TL;DR

Draft GraphQL models no longer have __typename. Please indicate if this will have downstream complications for your team. We are considering this a breaking change

@jaikamat jaikamat added the refactor ⚒️ Refactors elements of the codebase and/or addresses technical debt label Feb 7, 2024
@jaikamat jaikamat self-assigned this Feb 7, 2024
@jaikamat jaikamat requested review from Sarah4VT, jmcreasman, kterry1 and valoriecarli and removed request for a team February 7, 2024 20:20
Copy link

changeset-bot bot commented Feb 7, 2024

🦋 Changeset detected

Latest commit: 483b152

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 34 packages
Name Type
@commercetools-test-data/product-selection Major
@commercetools-test-data/product-discount Major
@commercetools-test-data/standalone-price Major
@commercetools-test-data/inventory-entry Major
@commercetools-test-data/shipping-method Major
@commercetools-test-data/associate-role Major
@commercetools-test-data/business-unit Major
@commercetools-test-data/cart-discount Major
@commercetools-test-data/custom-object Major
@commercetools-test-data/discount-code Major
@commercetools-test-data/shopping-list Major
@commercetools-test-data/product-type Major
@commercetools-test-data/custom-view Major
@commercetools-test-data/category Major
@commercetools-test-data/customer Major
@commercetools-test-data/channel Major
@commercetools-test-data/commons Major
@commercetools-test-data/payment Major
@commercetools-test-data/product Major
@commercetools-test-data/order Major
@commercetools-test-data/state Major
@commercetools-test-data/store Major
@commercetools-test-data/cart Major
@commercetools-test-data/type Major
@commercetools-test-data/quote-request Major
@commercetools-test-data/quote Major
@commercetools-test-data/staged-quote Major
@commercetools-test-data/customer-group Major
@commercetools-test-data/review Major
@commercetools-test-data/tax-category Major
@commercetools-test-data/zone Major
@commercetools-test-data/core Major
@commercetools-test-data/graphql-types Major
@commercetools-test-data/utils Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jaikamat jaikamat requested a review from tdeekens February 7, 2024 20:22
@jaikamat jaikamat changed the title refactor(test-data): architectural change enforcing omitting draft model __typename creation refactor(test-data): architectural change to omit draft model __typename creation Feb 7, 2024
@jaikamat
Copy link
Contributor Author

jaikamat commented Feb 7, 2024

image
@commercetools/first-contact-team-fe No draft preset snapshot tests contain __typename

@@ -17,12 +17,8 @@ export type TDivisionDraftBuilder = TBuilder<TDivisionDraft>;
export type TCreateCompanyDraftBuilder = () => TCompanyDraftBuilder;
export type TCreateDivisionDraftBuilder = () => TDivisionDraftBuilder;
//BusinessUnitDraftGraphql is only scaffolding at this time
export type TCompanyDraftGraphql = TCompanyDraft & {
__typename: 'BusinessUnitDraft';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For most of the models in this PR (save for Reference, KeyReference, and LocalizedString), this was the relevant change. Remove __typename from types.ts, the generator, and ensure the type was safe all the way down to tests

@@ -0,0 +1,28 @@
# 2. Exclude `__typename` from Draft GraphQL Models
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Highlighting the ADR here for others to review 🔍

@jaikamat jaikamat marked this pull request as ready for review February 8, 2024 16:12
@jaikamat jaikamat requested review from a team as code owners February 8, 2024 16:12
Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

🏩 The changes look very sane. I value taking the time taken for this approach! Especially the ADR addition.

The main question I guess now: breaking or not. I would rather be safe and make it a breaking release. WDYT? Consumers can still add a typename to drafts by adding __typename('foo') I suppose?

@jaikamat
Copy link
Contributor Author

jaikamat commented Feb 8, 2024

🏩 The changes look very sane. I value taking the time taken for this approach! Especially the ADR addition.

The main question I guess now: breaking or not. I would rather be safe and make it a breaking release. WDYT? Consumers can still add a typename to drafts by adding __typename('foo') I suppose?

Definitely comfortable labeling this as a breaking release considering the number of presets and files it touches. How do I go about doing that?

To be honest consumers will likely have some difficulty tacking __typename on, especially for deeply-nested models, but that's the tradeoff. If teams are relying on __typename in draft GraphQL model tests, I'd be curious to see how widespread of an issue it is - but my suspicions tell me it's minimal, if at all.

Copy link
Member

@emmenko emmenko left a comment

Choose a reason for hiding this comment

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

Approving the approach, the documentation and the effort put into this! Many thanks ❤️

I won't be reviewing every single change though, I trust your abilities 😉

@jaikamat jaikamat changed the title refactor(test-data): architectural change to omit draft model __typename creation [Breaking]: architectural change to omit draft model __typename creation Feb 8, 2024
@jaikamat
Copy link
Contributor Author

jaikamat commented Feb 8, 2024

@tdeekens see cc0edf3 for the changeset 👍

Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

Super duper. So proud how far test-data came from a small idea to things falling into place.

Copy link
Contributor

@stephsprinkle stephsprinkle left a comment

Choose a reason for hiding this comment

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

This is a huge improvement 👏

Copy link
Contributor

@jaikumar-tj jaikumar-tj left a comment

Choose a reason for hiding this comment

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

👏 Thanks for such a massive work

Copy link
Contributor

@kterry1 kterry1 left a comment

Choose a reason for hiding this comment

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

Awesome job Jai!

@tdeekens
Copy link
Contributor

tdeekens commented Feb 9, 2024

:shipit:

@jaikamat jaikamat merged commit 71eb4f5 into main Feb 9, 2024
3 checks passed
@jaikamat jaikamat deleted the FCT-710 branch February 9, 2024 18:35
@ct-changesets ct-changesets bot mentioned this pull request Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor ⚒️ Refactors elements of the codebase and/or addresses technical debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants