mswjs / data

Data modeling and relation library for testing JavaScript applications.

Home Page:https://npm.im/@mswjs/data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: manyOf can optionally return empty array instead of undefined

tinleym opened this issue ยท comments

Use Case

GraphQL schemas may require an array type, in which case returning undefined will throw an error. The current workaround is to explicitly extract all fields from an Entity and append an or fallback to all manyOf fields. It would be nice if this wasn't the case.

API Change

Change the manyOf(entityName: string) declaration to manyOf(entityName: string, emptyArrayFallback?: boolean)

Happy to submit a PR if this fits the goals of the library!

Hey, @tinleym. Thanks for suggesting this!

I think this is a good idea. Listing methods should always return a list which may or may not be filled with entities that match the criteria.

Sharing some of the similar APIs below for inspiration:

document.querySelectorAll('#noop')
// NodeList []
prisma.users.findMany({ ... })
// []

See Prisma docs on findMany

Since we model our public API to resemble Prisma, that's a big argument to migrate to the return type you're proposing. One correction though: I would suggest we always return an empty array if there were no entities matching the criteria. No need to introduce additional flags like emptyArrayFallback. Let's keep the API predictable.

Please, go ahead and open a pull request with this change. I'd be glad to review and merge it. Thank you!

Even better!

Awesome! Looking forward to collaborating with you in the pull request.

Ugh. Turns out I have too much on my plate at the moment, and it's going to take some time for me to get around to this. Hopefully not too long, but probably best to open up the floor.

๐ŸŽ‰ This issue has been resolved in version 0.10.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€