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

enhance(json-schema): handle discriminator mapping #5206

Merged
merged 3 commits into from
Apr 3, 2023

Conversation

tsirlucas
Copy link
Contributor

🚨 IMPORTANT: Please do not create a Pull Request without creating an issue first.

Any change needs to be discussed before proceeding. Failure to do so may result in the rejection of
the pull request.

Description

If discriminator mapping has a different name, getTypeResolverFromOutputTCs fails to resolve it to the correct schema. This PR adds a DiscriminatorMappingDirective and tags discriminated unions with its mappings if theyre existent.

Fixes # TODO

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as
    expected)
  • This change requires a documentation update

Screenshots/Sandbox (if appropriate/relevant):

Adding links to sandbox or providing screenshots can help us understand more about this PR and take
action on it as appropriate

How Has This Been Tested?

I've added a schema that should break if you try to query for DifferentDog. Id like to add a test case for getTypeResolverFromOutputTCs picking the values for directives but Im not sure how. Would gladly add it with some guidance.

  • Test A
  • Test B

Test Environment:

  • OS:
  • @graphql-mesh/...:
  • NodeJS:

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests and linter rules pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose
the solution you did and what alternatives you considered, etc...

@changeset-bot
Copy link

changeset-bot bot commented Mar 7, 2023

⚠️ No Changeset found

Latest commit: d1e793f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

if (subSchema.discriminator.mapping) {
schemaComposer.addDirective(DiscriminatorMappingDirective);
Object.keys(subSchema.discriminator.mapping).forEach(value => {
const mappedSchema = subSchema.discriminator.mapping[value].split('schemas/')[1];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please tell me if there is a better way to do so, I've done it this way due to lack of context

@@ -53242,7 +53292,7 @@ type PageBeanCustomFieldContextDefaultValue {
values: [CustomFieldContextDefaultValue]
}

union CustomFieldContextDefaultValue @discriminator(field: "type") = CustomFieldContextDefaultValueCascadingOption | CustomFieldContextDefaultValueMultipleOption | CustomFieldContextDefaultValueSingleOption | CustomFieldContextSingleUserPickerDefaults | CustomFieldContextDefaultValueMultiUserPicker | CustomFieldContextDefaultValueSingleGroupPicker | CustomFieldContextDefaultValueMultipleGroupPicker | CustomFieldContextDefaultValueDate | CustomFieldContextDefaultValueDateTime | CustomFieldContextDefaultValueURL | CustomFieldContextDefaultValueProject | CustomFieldContextDefaultValueFloat | CustomFieldContextDefaultValueLabels | CustomFieldContextDefaultValueTextField | CustomFieldContextDefaultValueTextArea | CustomFieldContextDefaultValueReadOnly | CustomFieldContextDefaultValueSingleVersionPicker | CustomFieldContextDefaultValueMultipleVersionPicker | CustomFieldContextDefaultValueForgeStringField | CustomFieldContextDefaultValueForgeMultiStringField | CustomFieldContextDefaultValueForgeObjectField | CustomFieldContextDefaultValueForgeDateTimeField | CustomFieldContextDefaultValueForgeGroupField | CustomFieldContextDefaultValueForgeMultiGroupField | CustomFieldContextDefaultValueForgeNumberField | CustomFieldContextDefaultValueForgeUserField | CustomFieldContextDefaultValueForgeMultiUserField
union CustomFieldContextDefaultValue @discriminator(field: "type") @discriminatorMapping(value: "option.cascading", schema: "CustomFieldContextDefaultValueCascadingOption") @discriminatorMapping(value: "option.multiple", schema: "CustomFieldContextDefaultValueMultipleOption") @discriminatorMapping(value: "option.single", schema: "CustomFieldContextDefaultValueSingleOption") @discriminatorMapping(value: "single.user.select", schema: "CustomFieldContextSingleUserPickerDefaults") @discriminatorMapping(value: "multi.user.select", schema: "CustomFieldContextDefaultValueMultiUserPicker") @discriminatorMapping(value: "grouppicker.single", schema: "CustomFieldContextDefaultValueSingleGroupPicker") @discriminatorMapping(value: "grouppicker.multiple", schema: "CustomFieldContextDefaultValueMultipleGroupPicker") @discriminatorMapping(value: "datepicker", schema: "CustomFieldContextDefaultValueDate") @discriminatorMapping(value: "datetimepicker", schema: "CustomFieldContextDefaultValueDateTime") @discriminatorMapping(value: "url", schema: "CustomFieldContextDefaultValueURL") @discriminatorMapping(value: "project", schema: "CustomFieldContextDefaultValueProject") @discriminatorMapping(value: "float", schema: "CustomFieldContextDefaultValueFloat") @discriminatorMapping(value: "labels", schema: "CustomFieldContextDefaultValueLabels") @discriminatorMapping(value: "textfield", schema: "CustomFieldContextDefaultValueTextField") @discriminatorMapping(value: "textarea", schema: "CustomFieldContextDefaultValueTextArea") @discriminatorMapping(value: "readonly", schema: "CustomFieldContextDefaultValueReadOnly") @discriminatorMapping(value: "version.single", schema: "CustomFieldContextDefaultValueSingleVersionPicker") @discriminatorMapping(value: "version.multiple", schema: "CustomFieldContextDefaultValueMultipleVersionPicker") @discriminatorMapping(value: "forge.string", schema: "CustomFieldContextDefaultValueForgeStringField") @discriminatorMapping(value: "forge.string.list", schema: "CustomFieldContextDefaultValueForgeMultiStringField") @discriminatorMapping(value: "forge.object", schema: "CustomFieldContextDefaultValueForgeObjectField") @discriminatorMapping(value: "forge.datetime", schema: "CustomFieldContextDefaultValueForgeDateTimeField") @discriminatorMapping(value: "forge.group", schema: "CustomFieldContextDefaultValueForgeGroupField") @discriminatorMapping(value: "forge.group.list", schema: "CustomFieldContextDefaultValueForgeMultiGroupField") @discriminatorMapping(value: "forge.number", schema: "CustomFieldContextDefaultValueForgeNumberField") @discriminatorMapping(value: "forge.user", schema: "CustomFieldContextDefaultValueForgeUserField") @discriminatorMapping(value: "forge.user.list", schema: "CustomFieldContextDefaultValueForgeMultiUserField") = CustomFieldContextDefaultValueCascadingOption | CustomFieldContextDefaultValueMultipleOption | CustomFieldContextDefaultValueSingleOption | CustomFieldContextSingleUserPickerDefaults | CustomFieldContextDefaultValueMultiUserPicker | CustomFieldContextDefaultValueSingleGroupPicker | CustomFieldContextDefaultValueMultipleGroupPicker | CustomFieldContextDefaultValueDate | CustomFieldContextDefaultValueDateTime | CustomFieldContextDefaultValueURL | CustomFieldContextDefaultValueProject | CustomFieldContextDefaultValueFloat | CustomFieldContextDefaultValueLabels | CustomFieldContextDefaultValueTextField | CustomFieldContextDefaultValueTextArea | CustomFieldContextDefaultValueReadOnly | CustomFieldContextDefaultValueSingleVersionPicker | CustomFieldContextDefaultValueMultipleVersionPicker | CustomFieldContextDefaultValueForgeStringField | CustomFieldContextDefaultValueForgeMultiStringField | CustomFieldContextDefaultValueForgeObjectField | CustomFieldContextDefaultValueForgeDateTimeField | CustomFieldContextDefaultValueForgeGroupField | CustomFieldContextDefaultValueForgeMultiGroupField | CustomFieldContextDefaultValueForgeNumberField | CustomFieldContextDefaultValueForgeUserField | CustomFieldContextDefaultValueForgeMultiUserField
Copy link
Contributor Author

@tsirlucas tsirlucas Mar 7, 2023

Choose a reason for hiding this comment

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

Hmm maybe we only add discriminator when the mapped value is different from schema title so we can avoid these huge code changes?

@tsirlucas
Copy link
Contributor Author

Kinda late for me here so I will go through the checklist tomorrow. Will leve it open so we can communicate on the subject here

@tsirlucas
Copy link
Contributor Author

tsirlucas commented Mar 8, 2023

Is the failing test related to pr changes? (im assuming it isnt as i can see similar error here as well)

@ardatan
Copy link
Owner

ardatan commented Apr 3, 2023

Thanks @tsirlucas !

@ardatan ardatan merged commit d417143 into ardatan:master Apr 3, 2023
@theguild-bot theguild-bot mentioned this pull request Apr 24, 2023
This was referenced Apr 30, 2024
This was referenced May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants