-
-
Notifications
You must be signed in to change notification settings - Fork 821
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
Allow to return null for mocks / fuzz testing #870
Comments
Hey! You're right. There's no way to do this easily right now. One thing you could do would be to use the customizable mocks ability to null out the appropriate fields randomly: https://www.apollographql.com/docs/graphql-tools/mocking.html#Customizing-mocks. I think we could extend the mocking abilities to include something like this though in the future. Maybe |
Hey @JakeDawkins, thanks for your reply. Yes, you could implement it by customizing mocks, however you'd have to implement this for every single mock resolver. That's why I'd prefer a flag, which can also potentially return I like your suggestion for calling it |
An even better implementation would be to not randomize null values, but generate a full response with all nullable types filled (as it is currently the case). And then creating the minimum, but complete set of variations of responses where nullable types are set to null. This way the test would also be deterministic and not depending on randomness 🙂 |
There is a related more serious issue: #930 without an obvious workaround. If you have a |
If you use partial mocking this will create havoc on your login functionality. I just managed to create a workaround:
If you use mocking with The workaround I implemented was explicitly returning null
If you don't return |
Yep, I think that's a bit a different story. As you mentioned |
This would be a great feature so am adding a very late +1 |
👍 +1 |
Mocking has been almost rewritten from scratch. |
I am still seeing this issue: #4792 would really appreciate any guidance |
I've noticed in my apps that null pointer exceptions seem to be the major cause of runtime errors. Ideally these would be prevented by a type system, however I currently don't use one and it would be too much effort to add types to the code bases at this point.
However, the amount of components that connect to API data aren't that many. I'm using the mock capabilities of
graphql-tools
to generate mock API data that I can feed into those components to test if they generally work. Currently graphql-tools always returns an actual value if a field is nullable.I was thinking if there was an option to allow nullable properties to return null, I could generate a bunch of sample data and see if my components are able to render with all of them. If the set is large enough, I think I could catch most issues where null checks are missing.
Would you be open to introducing such a flag to graphql-tools to allow nullable properties to actually be null? Something I'm not sure yet, is how this would work if you have explicitly defined mock resolvers for a given field. For my use case I'd like to also possibly skip them, without having to put a condition into all of them.
So something like this would be great:
Or:
The latter example might be helpful for other use cases, as the user can decide per field if it should be null.
What do you think?
The text was updated successfully, but these errors were encountered: