rmosolgo / graphql-ruby

Ruby implementation of GraphQL
http://graphql-ruby.org
MIT License
5.38k stars 1.39k forks source link

Allow to pass a anonymous block to arguments without a type definition #696

Closed gottfrois closed 7 years ago

gottfrois commented 7 years ago

Would be nice to be able to define an argument object type without explicitely having to create a type for it.

Consider the following example:

Types::PostType = GraphQL::ObjectType.define do
  field :title, types.String
  field :published, types.Boolean
end

Types::PostFilterInputType = GraphQL::InputObjectType.define do
  argument :published, types.Boolean
end

field :posts, Types::PostType do
  argument :filter, Types::PostFilterInputType
end

Which makes the following query possible:

query {
  posts(filter: { published: true }) {
    title
    published
  }
}

Now, I run into some usecases where the filter definition is coupled to the field. For example I could allow to filter against published in this case but not in another case.

I'm thinking it would be easier to manage those filters if we could pass an anonymous block to the argument:

field :posts, Types::PostType do
  argument :filter, GraphQL::InputObjectType do
    argument :published, types.Boolean
  end
end

What do you think?

pabloh commented 7 years ago

@gottfrois is an interesting suggestion but I think It would need to break the current API, since argument already can take a block: https://github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/argument.rb#L98.

gottfrois commented 7 years ago

@pabloh thanks for the quick response, that's a shame, what's the purpose of this block exactly?

pabloh commented 7 years ago

It's part of graphql-ruby's definitions DSL to be able to define properties using a block instead of plain ruby method arguments. Anyway, you could probably handle this use case by defining an inline InputObjectType for your :filter argument's type.

gottfrois commented 7 years ago

@pabloh sounds promising, can you paste some code sample please? not sure how that would look like

pabloh commented 7 years ago
Types::PostType = GraphQL::ObjectType.define do
  field :posts, Types::PostType do
    argument :filter, GraphQL::InputObjectType.define {
      argument :published, types.Boolea
    }
  end
end
pabloh commented 7 years ago

@gottfrois, I think you should close this there's already a lot of other issues opened...