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
Default value for fields #2783
Comments
No, that configuration isn't supported. (Argument default values are defined in the GraphQL spec, eg http://spec.graphql.org/June2018/#sec-Coercing-Field-Arguments.) Of course, you can implement something like that with field :my_field, String, null: false
def my_field
object.my_field || "Default value"
end I hope that helps! |
Thanks a lot! This will help! I'm writing a proof of concept for a framework to quickly build schemas in a standardized way. Not sure if my ideas will work out in the end, but either way it's a lot of fun and I learn much about |
Cool, at a framework level, you could probably implement it with a Field Extension (https://graphql-ruby.org/type_definitions/field_extensions.html), eg class DefaultValueExtension < GraphQL::Schema::FieldExtension
def after_resolve(value:, **_rest)
if value.nil?
options[:default_value]
else
value
end
end
end Like class Types::BaseField < GraphQL::Schema::Field
def initialize(*args, default_value: nil, **kwargs, &block)
super(*args, **kwargs, &block)
if !default_value.nil?
extension(DefaultValueExtension, default_value: default_value)
end
end
end Then: field :some_string, String, null: false, default_value: "🎉" Hope that helps! |
Hey thanks a lot. I will look into that. Looks exactly like what I had in mind. I guess I will publish a first draft in the following weeks. Looking forward to hear what you think about it. Have a nice weekend! |
That extension solution looks great, but I wonder if there is a reason we might not want to make it out of the box functionality? Thanks |
This was added as |
Amazing- thank you @rmosolgo I can see the docs were updated in that PR, so i'm disappointed I missed this! Is there anywhere else in the docs you feel it makes sense to add a note? I would be happy to raise a PR if it helps. |
Yeah, I think it could be called out more in the documentation -- at least a little example following the mention on https://graphql-ruby.org/fields/introduction#field-resolution. Please do add something if you think it'd be helpful! |
Hello,
is it possible to assign default values to fields?
For example:
The guides talk about default value in arguments https://graphql-ruby.org/fields/arguments.html but I didn't find anything about default values for fields.
Thanks!
The text was updated successfully, but these errors were encountered: