rmosolgo/graphql-ruby

Argument default value is cached?

maxrosecollins opened this issue · 1 comments

I have an argument on a field like this

field :comments, [Types::CommentType] do
  argument :created_at, String, required: false, default_value: Time.current.strftime('%Y-%m-%d')
end

But this default value is stuck on the last time the app was deployed.
If I redeploy the app and check this default value then it is updated to the current date.

Is this expected behaviour?

Yes, this is the expected behavior. It's "just" Ruby: .strftime returns a String, that String is passed as argument(..., default_value: ...). GraphQL-Ruby doesn't know that the string was produced from a Time.current... expression!

For the expression to be recalculated at runtime, you'd have to write it in some code that runs each time the field is called. For example:

def comments(created_at: Time.current.strftime("%Y-%m-%d")) 
  # resolution behavior here... 
end 

To document this, you could use the description: .... option, for example:

argument ..., description: "Defaults to the current date" 

I hope this helps! Let me know if you have any trouble with that approach.