Skip to content

Commit 0bf9b8e

Browse files
committed
Set up reference resolvers
1 parent 5f3c9bf commit 0bf9b8e

16 files changed

Lines changed: 123 additions & 9 deletions

app/graphql/kitsu_schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class KitsuSchema < GraphQL::Schema
44
include ApolloFederation::Schema
55
federation version: '2.0'
6+
import_directives %w[tag key shareable]
67

78
default_max_page_size 2000
89

app/graphql/types/account.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
# frozen_string_literal: true
2+
13
class Types::Account < Types::BaseObject
24
implements Types::Interface::WithTimestamps
35

46
description 'A user account on Kitsu'
57

8+
key fields: %w[id]
9+
10+
def self.resolve_reference(reference, context)
11+
# Only resolve the user if the ID matches the current user
12+
return nil if context[:user].nil? || context[:user].id.to_s != reference[:id]
13+
14+
context[:user]
15+
end
16+
617
field :id, ID, null: false
718

819
field :email, [String],

app/graphql/types/anime.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
# frozen_string_literal: true
2+
13
class Types::Anime < Types::BaseObject
24
implements Types::Interface::Media
35
implements Types::Interface::Episodic
46
implements Types::Interface::WithTimestamps
57

8+
key fields: %w[id]
9+
10+
def self.resolve_reference(reference, _context)
11+
Loaders::UnscopedRecordLoader.for(::Anime).load(reference[:id])
12+
end
13+
614
field :subtype, Types::Enum::AnimeSubtype,
715
null: false,
816
description: 'A secondary type for categorizing Anime.'

app/graphql/types/chapter.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
# frozen_string_literal: true
2+
13
class Types::Chapter < Types::BaseObject
24
implements Types::Interface::Unit
35
implements Types::Interface::WithTimestamps
46

57
description 'A single chapter of a manga'
68

9+
key fields: %w[id]
10+
11+
def self.resolve_reference(reference, _context)
12+
Loaders::UnscopedRecordLoader.for(::Chapter).load(reference[:id])
13+
end
14+
715
field :released_at, GraphQL::Types::ISO8601Date,
816
method: :published,
917
null: true,

app/graphql/types/character.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ class Types::Character < Types::BaseObject
55

66
description 'Information about a Character in the Kitsu database'
77

8+
key fields: %w[id]
9+
10+
def self.resolve_reference(reference, _context)
11+
Loaders::UnscopedRecordLoader.for(::Character).load(reference[:id])
12+
end
13+
814
field :id, ID, null: false
915

1016
field :slug, String,

app/graphql/types/episode.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
# frozen_string_literal: true
2+
13
class Types::Episode < Types::BaseObject
24
implements Types::Interface::Unit
35
implements Types::Interface::WithTimestamps
46

57
description 'An Episode of a Media'
68

9+
key fields: %w[id]
10+
11+
def self.resolve_reference(reference, _context)
12+
Loaders::UnscopedRecordLoader.for(::Episode).load(reference[:id])
13+
end
14+
715
field :length, Integer,
816
null: true,
917
description: 'The length of the episode in seconds'

app/graphql/types/franchise.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
# frozen_string_literal: true
2+
13
class Types::Franchise < Types::BaseObject
24
implements Types::Interface::WithTimestamps
35

46
description 'Related media grouped together'
57

8+
key fields: %w[id]
9+
10+
def self.resolve_reference(reference, _context)
11+
Loaders::UnscopedRecordLoader.for(::Franchise).load(reference[:id])
12+
end
13+
614
field :id, ID, null: false
715

816
field :titles, Types::TitlesList,
@@ -26,7 +34,7 @@ def titles
2634
def installments(sort: [{ on: :release_order, direction: :asc }])
2735
Loaders::InstallmentsLoader.connection_for({
2836
find_by: :franchise_id,
29-
sort: sort
37+
sort:
3038
}, object.id)
3139
end
3240
end

app/graphql/types/manga.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ class Types::Manga < Types::BaseObject
44
implements Types::Interface::Media
55
implements Types::Interface::WithTimestamps
66

7+
key fields: %w[id]
8+
9+
def self.resolve_reference(reference, _context)
10+
Loaders::UnscopedRecordLoader.for(::Manga).load(reference[:id])
11+
end
12+
713
field :subtype, Types::Enum::MangaSubtype,
814
null: false,
915
description: 'A secondary type for categorizing Manga.'

app/graphql/types/media_reaction.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ class Types::MediaReaction < Types::BaseObject
66

77
description 'A simple review that is 140 characters long expressing how you felt about a media'
88

9+
key fields: %w[id]
10+
11+
def self.resolve_reference(reference, context)
12+
Loaders::RecordLoader.for(::MediaReaction, token: context[:token]).load(reference[:id])
13+
end
14+
915
field :id, ID, null: false
1016

1117
field :author, Types::Profile,

app/graphql/types/person.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class Types::Person < Types::BaseObject
88
the creation and localization of media
99
DESCRIPTION
1010

11+
key fields: %w[id]
12+
13+
def self.resolve_reference(reference, _context)
14+
Loaders::UnscopedRecordLoader.for(::Person).load(reference[:id])
15+
end
16+
1117
field :id, ID, null: false
1218

1319
field :name, String,

0 commit comments

Comments
 (0)