Skip to content

Commit

Permalink
use relay global ids
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhughes27 committed Nov 29, 2018
1 parent f4098b4 commit 0ad75c2
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 18 deletions.
5 changes: 5 additions & 0 deletions app/graphql/resolvers/base_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ class Resolvers::BaseResolver
def self.call(inputs, ctx)
self.new.call(inputs, ctx)
end

def database_id(global_id)
_, id = GraphQL::Schema::UniqueWithinType.decode(global_id)
return id
end
end
4 changes: 2 additions & 2 deletions app/graphql/resolvers/submit_score.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Resolvers::SubmitScore < Resolvers::BaseResolver
def call(inputs, ctx)
@tournament = ctx[:tournament]
@game = @tournament.games.find(inputs[:game_id])
@game = @tournament.games.find(database_id(inputs[:game_id]))
@report = build_report(inputs)

if !valid_submitter?
Expand All @@ -23,7 +23,7 @@ def build_report(inputs)
ScoreReport.new(
tournament_id: @tournament.id,
game_id: @game.id,
team_id: inputs[:team_id],
team_id: database_id(inputs[:team_id]),
submitter_fingerprint: inputs[:submitter_fingerprint],
home_score: inputs[:home_score],
away_score: inputs[:away_score],
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ type ScheduleGamePayload {

# A Score Dispute
type ScoreDispute {
gameId: Int!
gameId: ID!
id: ID!
}

Expand Down
9 changes: 9 additions & 0 deletions app/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ class Schema < GraphQL::Schema
subscription Types::Subscription

use GraphQL::Subscriptions::ActionCableSubscriptions

def self.id_from_object(object, type_definition, query_ctx)
GraphQL::Schema::UniqueWithinType.encode(object.class.name, object.id)
end

def self.object_from_id(id, query_ctx)
class_name, item_id = GraphQL::Schema::UniqueWithinType.decode(id)
Object.const_get(class_name).find(item_id)
end
end
3 changes: 2 additions & 1 deletion app/graphql/types/division.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Types::Division < Types::BaseObject
graphql_name "Division"
description "A Division"

field :id, ID, null: false
global_id_field :id

field :name, String, null: false
field :numTeams, Int, null: false
field :numDays, Int, null: false
Expand Down
3 changes: 2 additions & 1 deletion app/graphql/types/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Types::Field < Types::BaseObject
graphql_name "Field"
description "A Field"

field :id, ID, null: false
global_id_field :id

field :name, String, null: false
field :lat, Float, null: false
field :long, Float, null: false
Expand Down
3 changes: 2 additions & 1 deletion app/graphql/types/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Types::Game < Types::BaseObject
graphql_name "Game"
description "A Game"

field :id, ID, null: false
global_id_field :id

field :division, Types::Division, null: false
field :pool, String, null: true
field :bracketUid, String, null: true
Expand Down
4 changes: 2 additions & 2 deletions app/graphql/types/score_dispute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class Types::ScoreDispute < Types::BaseObject
graphql_name "ScoreDispute"
description "A Score Dispute"

field :id, ID, null: false
field :gameId, Int, null: false
global_id_field :id
field :gameId, ID, null: false
end
3 changes: 2 additions & 1 deletion app/graphql/types/score_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Types::ScoreReport < Types::BaseObject
graphql_name "ScoreReport"
description "A Score Report"

field :id, ID, null: false
global_id_field :id

field :submittedBy, String, null: false
field :submitterFingerprint, String, null: false
field :homeScore, Int, null: false
Expand Down
3 changes: 2 additions & 1 deletion app/graphql/types/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Types::Team < Types::BaseObject
graphql_name "Team"
description "A Team"

field :id, ID, null: false
global_id_field :id

field :name, String, null: false
field :email, String, auth: :required, null: true
field :phone, String, auth: :required, null: true
Expand Down
3 changes: 2 additions & 1 deletion app/graphql/types/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class Types::User < Types::BaseObject
graphql_name "User"
description "A User"

field :id, ID, null: false
global_id_field :id

field :name, String, null: true
field :email, String, null: false
end
8 changes: 6 additions & 2 deletions app/models/score_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def total
def build_confirm_link
params = {
teamName: other_team.name,
gameId: game_id,
gameId: game_global_id,
homeScore: home_score,
awayScore: away_score
}
Expand All @@ -67,9 +67,13 @@ def build_confirm_link
def build_dispute_link
params = {
teamName: other_team.name,
gameId: game_id
gameId: game_global_id
}

"#{tournament.domain}/submit?#{params.to_query}"
end

def game_global_id
GraphQL::Schema::UniqueWithinType.encode("Game", game_id)
end
end
14 changes: 9 additions & 5 deletions test/api/submit_score_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SubmitScoreTest < ApiTest
refute @game.reload.score_confirmed

second_input = input.merge(
team_id: @game.away.id,
team_id: build_global_id('Team', @game.away.id),
submitter_fingerprint: 'fingerprint2'
)

Expand All @@ -73,7 +73,7 @@ class SubmitScoreTest < ApiTest
refute @game.reload.score_confirmed

second_input = input.merge(
team_id: @game.away.id
team_id: build_global_id('Team', @game.away.id)
)

execute_graphql("submitScore", "SubmitScoreInput", second_input)
Expand All @@ -86,7 +86,7 @@ class SubmitScoreTest < ApiTest
refute @game.reload.score_confirmed

second_input = input.merge(
team_id: @game.away.id,
team_id: build_global_id('Team', @game.away.id),
submitter_fingerprint: 'fingerprint2',
home_score: 3,
away_score: 15
Expand Down Expand Up @@ -132,8 +132,8 @@ class SubmitScoreTest < ApiTest

def input
{
game_id: @game.id,
team_id: @game.home.id,
game_id: build_global_id('Game', @game.id),
team_id: build_global_id('Team', @game.home.id),
submitter_fingerprint: 'fingerprint',
home_score: 15,
away_score: 13,
Expand All @@ -145,4 +145,8 @@ def input
comments: 'comment'
}
end

def build_global_id(type, id)
GraphQL::Schema::UniqueWithinType.encode(type, id)
end
end

0 comments on commit 0ad75c2

Please sign in to comment.