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

Add fallback_value option to Field #4069

Merged
merged 7 commits into from May 24, 2022
Merged

Conversation

danajackson2
Copy link
Contributor

Fixes #4020. This adds a parameter called :fallback_value to Field, which allows you to specify a value to be returned if the usual ways (calling the method on an object, or checking for the key in a hash) fail. It's useful if there is a query that returns objects that might or might not have a certain field.

The motivation for this is a scenario where we have a data model that has lots of entity types. All of them have an id property. Most of them have a name property, but some of them don't. We want all of them to implement an interface (Node) that has a required id property and an optional name property.

interface Node { id: ID! name: String }

The default for the :fallback_value param in the initializer is :not_given, which allows us to set nil as the fallback if we want. The most common use case is probably with interfaces, so that's where I wrote tests. Are there any other places this should be tested? I added documentation to guides/fields/introduction.md under Field Resolution. Does that seem sufficient? I added a blurb to the error message about supplying a fallback_value to implement the field.

@rmosolgo
Copy link
Owner

This looks great, thanks for making this improvement! I pushed a commit to test the hash_key: ... behavior, although changes upstream mean there are actually several hash-lookup-related branches in field resolution. I wouldn't be surprised if there are few resolution paths (@dig_keys comes to mind) where fallback_value isn't properly applied yet. But we can cross those bridges when we get to them. Thanks again!

@rmosolgo rmosolgo merged commit 5f8aa65 into rmosolgo:master May 24, 2022
@rmosolgo rmosolgo added this to the 2.0.8 milestone May 24, 2022
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.

Default/Fallback Implementation of Interface Fields
2 participants