-
Notifications
You must be signed in to change notification settings - Fork 52
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
Add support for oneOf directive #180
Conversation
...sharedGenerateMockOpts, | ||
}); | ||
|
||
mockFieldsString = ` ...(override ? override : {${field.name.value} : ${value}}),`; |
There was a problem hiding this comment.
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}`; |
There was a problem hiding this comment.
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.
@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. |
@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. |
@ardeois do you think this is something we can get merged, and/or would you like to see changes/a different approach here? |
There was a problem hiding this 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
@ardeois thanks for the review! I made the requested edits, ready for your 👀 when you have time. |
There was a problem hiding this 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!
This PR adds support for the 'oneOf' directive on input objects.
The generated mocking function is a little different than most others:
In order to install
@graphql-tools/utils
, I needed to upgrade TypescriptOnce I upgraded typescript, there were type errors for the 'indefinite' package, so I added the
@types/indefinite
package