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

Default value for fields #2783

Closed
christophweegen opened this issue Feb 27, 2020 · 8 comments
Closed

Default value for fields #2783

christophweegen opened this issue Feb 27, 2020 · 8 comments

Comments

@christophweegen
Copy link

Hello,

is it possible to assign default values to fields?

For example:

field :my_field, String, null: false, default_value: "My default value"

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!

@rmosolgo
Copy link
Owner

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!

@christophweegen
Copy link
Author

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 graphql-ruby. If you're interested, I can keep you in the loop if I have something substantial. 🚀

@rmosolgo
Copy link
Owner

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!

@christophweegen
Copy link
Author

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!

@ltickett
Copy link

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

@rmosolgo
Copy link
Owner

This was added as fallback_value: back in #4069, although it's not well documented yet -- hopefully that helps!

@ltickett
Copy link

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.

@rmosolgo
Copy link
Owner

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!

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

No branches or pull requests

3 participants