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 all commits
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
14 changes: 12 additions & 2 deletions app/graphql/analysis/max_node_limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def max_possible_nodes

# We are checking if a field is a connection or custom type
def allowed_type?(node, field_definition)
return true if field_definition.connection?
return true if connection?(field_definition)
return false if field_definition.name == 'nodes'
# I am not sure if there is a better way to identify these.
# but this will get the custom type
Expand All @@ -96,8 +96,18 @@ def allowed_type?(node, field_definition)
false
end

def connection?(field_definition)
return true if field_definition.connection?

field_definition.extensions.any? do |extension|
# HACK: I am not sure how to handle custom connections
# that set connection: false and just use extensions.
extension.class.name.downcase.include?('connection')
end
end

def node_type(node, visitor)
prefix = visitor.field_definition.connection? ? 'Connection' : 'Custom'
prefix = connection?(visitor.field_definition) ? 'Connection' : 'Custom'

"Analysis::MaxNodeLimit::#{prefix}ScopeType".safe_constantize.new(
visitor.query,
Expand Down
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
47 changes: 47 additions & 0 deletions app/graphql/connections/feed_item_union_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 = nodes.count == first
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)
item&.id
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: 'The feed for an anime'

def feed
AnimeFeed.new(object.id)
end
end
16 changes: 16 additions & 0 deletions app/graphql/types/feed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Types::Feed < Types::BaseObject
field :activities, Types::Union::FeedItem.connection_type,
null: true,
connection: false,
extensions: [FeedConnectionExtension],
description: 'Feed activities based on the type supplied.'

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

possible_types Types::Post, Types::Profile
end