Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for oneOf directive #180

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

Merged

Conversation

thomasobrien99
Copy link
Contributor

This PR adds support for the 'oneOf' directive on input objects.

The generated mocking function is a little different than most others:

  • It only accepts a non-Partial type as an override
  • It picks an arbitrary fieldName/member from the input object and creates a default mock for that that field

In order to install @graphql-tools/utils, I needed to upgrade Typescript

Once I upgraded typescript, there were type errors for the 'indefinite' package, so I added the @types/indefinite package

...sharedGenerateMockOpts,
});

mockFieldsString = ` ...(override ? override : {${field.name.value} : ${value}}),`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this change to the mockFieldsString is the most significant change.

@@ -468,6 +462,10 @@ const getMockString = (
const typename = addTypename ? `\n __typename: '${typeName}',` : '';
const typenameReturnType = addTypename ? `{ __typename: '${typeName}' } & ` : '';

const overridesArgumentString = !hasOneOfDirective
? `overrides?: Partial<${casedNameWithPrefix}>`
: `override?: ${casedNameWithPrefix}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because a Partial<T> could be completely empty, it won't work for an the override which, if supplied, needs to replace the entire mock rather than merging to satisfy typescript that we're not putting multiple fields together.

@davidr64
Copy link

davidr64 commented Jan 29, 2025

@thomasobrien99 thank you very much for putting this PR together. Do you think it would be possible to instead generate a function for each of the possible configurations for the oneof object? Honestly I think it might be hard to fit into the current system, making it generate multiple functions at once instead of a single might be too much but thought I would throw it out there.

@thomasobrien99
Copy link
Contributor Author

@davidr64 I tinkered with that but it is a bit unwieldy. One of the oneOf types I'd like to mock has 20+ fields, so that'd generate a mocking function for each one, and the usage wouldn't be much (if at all) slicker than just calling the function generated here with whatever option you'd want overriding the default one.

@thomasobrien99
Copy link
Contributor Author

@ardeois do you think this is something we can get merged, and/or would you like to see changes/a different approach here?

Copy link
Owner

@ardeois ardeois left a comment

Choose a reason for hiding this comment

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

Looks good, thank you for the contribution !
I've got a few comments before we can go ahead

@thomasobrien99
Copy link
Contributor Author

@ardeois thanks for the review! I made the requested edits, ready for your 👀 when you have time.

@ardeois ardeois self-assigned this Feb 6, 2025
@ardeois ardeois added the minor Increment the minor version when merged label Feb 6, 2025
Copy link
Owner

@ardeois ardeois left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for the fixes!

@ardeois ardeois merged commit 71c9e0f into ardeois:main Feb 6, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Increment the minor version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants