tb / northwind-graphql-ruby

Northwind graphql-ruby
http://northwind-graphql-ruby.herokuapp.com/
24 stars 4 forks source link

Filter can't handle boolean arguments #35

Closed gopeter closed 6 years ago

gopeter commented 6 years ago

Thanks for this great demo app! Helped me a lot to understand some of the graphql-ruby's methods :)

I tried to adapt some concepts of the demo to my own GraphQL API, but found a problem with boolean arguments.

Example:

Types::ProjectFilterType = GraphQL::InputObjectType.define do
  name 'ProjectFilter'

  argument :archived, types.Boolean
end

and

query {
  projects(filter: { archived: true }) {
    count,
    edges {
      node {
        name
      }
    }
  }
}

returns:

{
  "status": 500,
  "error": "Internal Server Error",
  "exception": "#<NoMethodError: undefined method `archived' for #<Project::ActiveRecord_Relation:0x007fdbde6bc530>>",
[...]

If I use Rails' native .where method, boolean arguments are working:

module Services
  module Filter
    def self.call(records, filters)
      filters&.each do |key, value|
        records = records.where(key => value)
      end
      records
    end
  end
end

But I'm not sure if this could lead to some other unknown drawbacks. Performance should be not an issue since Rails builds the entire where clause:

query {
  projects(filter: { archived: false, category: "B" }) {
    count,
    edges {
      node {
        name
      }
    }
  }
}

leads to

Project Load (0.9ms)  SELECT "projects".* FROM "projects" WHERE "projects"."archived" = $1 AND "projects"."category" = $2  [["archived", "f"], ["category", "B"]]

So do you have any thoughts on this? :)

tb commented 6 years ago

"exception": "#<NoMethodError: undefined method `archived' for #<Project::ActiveRecord_Relation:0x007fdbde6bc530>>", means that you are missing scope method named archived in your Project model - check https://github.com/tb/northwind-graphql-ruby/blob/master/app/models/supplier.rb#L11

gopeter commented 6 years ago

🤦‍♂️ Sorry, work's as expected now!