From 56c4dbc2f47446addc1cd1748d8c995b17b07fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Wed, 12 Oct 2016 17:27:04 +0200 Subject: [PATCH] Add support for GraphQL pagination --- app/graph/types/commentable_interface.rb | 9 ++++++++- app/graph/types/debate_type.rb | 8 +++++++- app/graph/types/proposal_type.rb | 10 ++++++++-- app/graph/types/query_root.rb | 9 +++------ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/graph/types/commentable_interface.rb b/app/graph/types/commentable_interface.rb index 1f62b0d09..ad51a55eb 100644 --- a/app/graph/types/commentable_interface.rb +++ b/app/graph/types/commentable_interface.rb @@ -1,6 +1,8 @@ CommentableInterface = GraphQL::InterfaceType.define do name "Commentable" + # Expose fields associated with Commentable models + field :id, !types.ID, "ID of the commentable" field :title, types.String, "The title of this commentable" field :description, types.String, "The description of this commentable" @@ -8,7 +10,12 @@ CommentableInterface = GraphQL::InterfaceType.define do field :comments_count, types.Int, "Number of comments on this commentable" # Linked resources + field :author, UserType, "Author of this commentable" - field :comments, !types[!CommentType], "Comments in this commentable" + + connection :comments, CommentType.connection_type do + description "Comments in this commentable" + end + field :geozone, GeozoneType, "Geozone affected by this commentable" end diff --git a/app/graph/types/debate_type.rb b/app/graph/types/debate_type.rb index 1cae3a2a4..1c36f8b40 100644 --- a/app/graph/types/debate_type.rb +++ b/app/graph/types/debate_type.rb @@ -5,6 +5,7 @@ DebateType = GraphQL::ObjectType.define do interfaces([CommentableInterface]) # Expose fields associated with Debate model + field :id, !types.ID, "The id of this debate" field :title, types.String, "The title of this debate" field :description, types.String, "The description of this debate" @@ -19,7 +20,12 @@ DebateType = GraphQL::ObjectType.define do field :geozone_id, types.Int, "ID of the geozone affected by this debate" # Linked resources + field :author, UserType, "Author of this debate" - field :comments, !types[!CommentType], "Comments in this debate" + + connection :comments, CommentType.connection_type do + description "Comments in this debate" + end + field :geozone, GeozoneType, "Geozone affected by this debate" end diff --git a/app/graph/types/proposal_type.rb b/app/graph/types/proposal_type.rb index 007b5824c..c9d599b8d 100644 --- a/app/graph/types/proposal_type.rb +++ b/app/graph/types/proposal_type.rb @@ -3,8 +3,9 @@ ProposalType = GraphQL::ObjectType.define do description "A single proposal entry returns a proposal with author, total votes and comments" interfaces([CommentableInterface]) - + # Expose fields associated with Proposal model + field :id, !types.ID, "The id of this proposal" field :title, types.String, "The title of this proposal" field :description, types.String, "The description of this proposal" @@ -22,7 +23,12 @@ ProposalType = GraphQL::ObjectType.define do field :retired_explanation, types.String, "Explanation why this proposal was retired" # Linked resources + field :author, UserType, "Author of this proposal" - field :comments, !types[!CommentType], "Comments in this proposal" + + connection :comments, CommentType.connection_type do + description "Comments in this proposal" + end + field :geozone, GeozoneType, "Geozone affected by this proposal" end diff --git a/app/graph/types/query_root.rb b/app/graph/types/query_root.rb index 38f292bb9..007271932 100644 --- a/app/graph/types/query_root.rb +++ b/app/graph/types/query_root.rb @@ -11,8 +11,7 @@ QueryRoot = GraphQL::ObjectType.define do } end - field :proposals do - type !types[!ProposalType] + connection :proposals, ProposalType.connection_type do description "Find all Proposals" resolve -> (object, arguments, context) { Proposal.all @@ -28,8 +27,7 @@ QueryRoot = GraphQL::ObjectType.define do } end - field :debates do - type !types[!DebateType] + connection :debates, DebateType.connection_type do description "Find all Debates" resolve -> (object, arguments, context) { Debate.all @@ -45,8 +43,7 @@ QueryRoot = GraphQL::ObjectType.define do } end - field :comments do - type !types[!CommentType] + connection :comments, CommentType.connection_type do description "Find all Comments" resolve -> (object, arguments, context) { Comment.all