4

GraphQL queries are straight forward to define if you search for record e.g. by its id or exact value (in my case date).

My query should be returning not one ActiveRecord object but ActiveRecord Relation. How to define it to be consumable by GraphQL?

 field :file_data, !FileDataType, " Returns records based on given date: format 'yyyy-mm-dd hh:mm:ss'" do argument :created_on, !types.String, "format 'yyyy-mm-dd hh:mm:ss'" resolve -> (obj, args, context) do FileData.where("created_on > ?", args["created_on"]) end end 

It works for one object: if I add to the relation 'last':

 FileData.where("created_on > ?", args["created_on"]).last 

For many results I get an error in rails console:

NoMethodError (undefined method `attribute_name' [any defined & requested] for FileData::ActiveRecord_Relation:..>

I would appreciate any suggestion.

1 Answer 1

5

Use a list type for returning multiple objects:

field :file_data, types[FileDataType] do ... end 

types[...] defines a list type.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! I was considering additional gem (graphql-ruby includes built-in connection support for Array, ActiveRecord::Relations, and Sequel::Datasets) but solution turned to be so simple!
I had this same problem. My resolver returned all the records in the table, but my field definition didn't have [] around it. Much appreciated @rmosolgo
i need to retrieve single record from the database could anyone please help me with syntax without using block. i am using this syntax field :method_name, [Types::ObjectType],etc

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.