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

GraphQL - anime custom feed connection #855

Open
wants to merge 3 commits into
base: the-future
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/graphql/connections/base_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Connections::BaseConnection < GraphQL::Pagination::Connection
end
50 changes: 50 additions & 0 deletions app/graphql/connections/feed_item_union_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Connections::FeedItemUnionConnection < Connections::BaseConnection
attr_reader :mark, :user

def initialize(items, **args)
super(items, args)

@mark = args[:mark]
@user = User.current
end

def nodes
@nodes ||= list.to_a
end

def has_next_page
toyhammered marked this conversation as resolved.
Show resolved Hide resolved
@has_next_page
end

# There is no backwards check for feeds.
def has_previous_page
toyhammered marked this conversation as resolved.
Show resolved Hide resolved
false
end

def cursor_for(item)
end

def page_info

end

private

def list
return @list if @list

list = items
list = list.per(first_value)
list = list.page(id_lt: after_value) if after_value
list = list.sfw if sfw_filter?
list = list.mark if mark

@list = list
end

def sfw_filter?
return true if user.blank?

user.sfw_filter?
end
end
12 changes: 12 additions & 0 deletions app/graphql/feed_connection_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class FeedConnectionExtension < GraphQL::Schema::Field::ConnectionExtension
def apply
field.argument :first, 'Int', 'Returns the specific amount of elements', required: true
field.argument :after, 'String', 'Returns the elements in the list that come after the specified cursor.', required: false
toyhammered marked this conversation as resolved.
Show resolved Hide resolved
end

def resolve(object:, arguments:, context:)
toyhammered marked this conversation as resolved.
Show resolved Hide resolved
next_args = arguments.dup

yield(object, next_args)
end
end
4 changes: 4 additions & 0 deletions app/graphql/kitsu_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class KitsuSchema < GraphQL::Schema
use GraphQL::Execution::Interpreter
use GraphQL::Analysis::AST
use GraphQL::Execution::Errors
use GraphQL::Pagination::Connections

default_max_page_size 100

Expand All @@ -15,6 +16,9 @@ class KitsuSchema < GraphQL::Schema

query_analyzer Analysis::MaxNodeLimit

# Hook up a custom wrappers
connections.add(Types::Union::FeedItem, Connections::FeedItemUnionConnection)

def self.resolve_type(_type, object, _context)
"Types::#{object.class.name}".safe_constantize
end
Expand Down
8 changes: 8 additions & 0 deletions app/graphql/types/anime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ class Types::Anime < Types::BaseObject
def streaming_links
AssociationLoader.for(object.class, :streaming_links).scope(object)
end

field :feed, Types::Feed,
null: false,
description: ''

def feed
AnimeFeed.new(object.id)
end
end
18 changes: 18 additions & 0 deletions app/graphql/types/feed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Types::Feed < Types::BaseObject
description ''

field :activities, Types::Union::FeedItem.connection_type,
null: true,
connection: false,
extensions: [FeedConnectionExtension],
description: ''

def activities(first:, after: nil)
Connections::FeedItemUnionConnection.new(
object.activities,
first: first,
after: after,
context: context
)
end
end
6 changes: 6 additions & 0 deletions app/graphql/types/union/feed_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Types::Union::FeedItem < Types::Union::Base
connection_type_class(Types::FeedConnection)
description 'Objects which are part of a Feed'

possible_types Types::Post, Types::Profile
end