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

[1.8] Default value not provided for omitted optional input object with arguments with defaults #1678

Closed
Envek opened this issue Jul 17, 2018 · 5 comments

Comments

@Envek
Copy link
Contributor

Envek commented Jul 17, 2018

Summary

If query argument is optional complex input object with arguments with default values then these default values are not provided in this input object is omitted from the query arguments

Example

Given the following query definition:

class Types::QueryType < GraphQL::Schema::Object
  field :products, Types::Product.connection_type, null: false do
    argument :filters, Types::Product::Filter, required: false, default_value: {}
  end
  def products(filters:)
    # apply filters
  end
end
class Types::Product::Filter < GraphQL::Schema::InputObject
  graphql_name "ProductFilter"
  argument :archived,            Boolean, required: false, default_value: false
  argument :status,              String,  required: false
end

Generated docs says that filters have default values:

products(
  first: Int
  after: String
  last: Int
  before: String
  filters: ProductFilter = {archived: false}
): ProductConnection!

If executing a query without filters parameter then in method empty hash is passed (not as expected):

{
  products(first: 2) {
    edges { node { id } }
  }
}
def products(filter:) # filter: {} - plain hash as provided in default_value

If executing a query with empty filters parameter then in method passed a hash with default parameter set (as expected):

{
  products(first: 2, filters: {}) {
    edges { node { id } }
  }
}
def products(filter:) # filter: {:archived=>false} - instance of Types::Product::Filter

Environment

gem graphql 1.8.5
gem graphql-batch 0.3.9
ruby 2.5.1
rails 5.2.0

Notes

I'm naturally don't want to duplicate default values from Types::Product::Filter in the default_value option in filters argument definition.

@rmosolgo
Copy link
Owner

🤔 Hmm, thanks for the detailed report. How misleading that the generated schema suggests one thing while the runtime behavior shows something else!

I wonder what the spec has to say on this, which one is right...

@N0xFF
Copy link
Contributor

N0xFF commented Sep 6, 2019

It actual problem? I can't reproduce in 1.9.9 with Interpreter and AST.

Executing without filter argument returns a filter object as expected.

@Envek
Copy link
Contributor Author

Envek commented Sep 10, 2019

Yes, with Interpreter enabled it works like it should. ✔️ @N0xFF, thank you for pointing it out!
With disabled interpreter this issue is still there, though.

The only weird case left is that you still should specify any default value for filters argument (like empty hash) or Ruby will throw exception about not provided keyword argument.

@cantino
Copy link

cantino commented Oct 2, 2019

We just ran into this as well in our app that doesn't yet use Interpreter. Defaults are not applied for the sub-object when the field using it has a default_value of {} (or when it has no default_value at all). Is there some way we could set the parent field's default_value to something like Types::Product::Filter.defaults? I.e.g, is there some method to generate the default output of an object?

@rmosolgo
Copy link
Owner

I'm sorry, I don't really have the bandwidth to support the old runtime these days. If you're a GraphQL-Pro customer, shoot me an email and I'll keep digging, or if anyone wants to submit a PR, I'd be happy to review and help as I can.

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

4 participants