Skip to content

Commit

Permalink
add unique global ids for relay. update current admin, player-app and…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
kevinhughes27 committed Jul 28, 2018
1 parent d9cad66 commit 9eb4c4c
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 20 deletions.
9 changes: 6 additions & 3 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ def params_to_input(strong_params, params = {}, id_key = nil)
end

def result_to_attributes(result, key, except: nil)
result[key].deep_transform_keys { |key| key.to_s.underscore }.except(except)
attributes = result[key].deep_transform_keys { |key| key.to_s.underscore }.except(except)
attributes['id'] = database_id(attributes['id'])
attributes
end

def result_to_errors(result)
result['userErrors'] && result['userErrors'].map{ |e| e['field'].capitalize + ' ' + e['message'] }
def database_id(global_id)
_, id = GraphQL::Schema::UniqueWithinType.decode(global_id)
return id
end

def render_admin_404
Expand Down
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
6 changes: 3 additions & 3 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type Division {
bracket: Bracket
bracketTree: String
games: [Game!]
id: ID
id: ID!
isSeeded: Boolean
name: String
needsSeed: Boolean
Expand All @@ -129,7 +129,7 @@ type Error {
# A Field
type Field {
geoJson: String
id: ID
id: ID!
lat: Float
long: Float
name: String
Expand Down Expand Up @@ -282,7 +282,7 @@ type Team {
division: Division
divisionId: Int
email: String
id: ID
id: ID!
name: String
phone: String
seed: Int
Expand Down
9 changes: 9 additions & 0 deletions app/graphql/schema.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class Schema < GraphQL::Schema
query Types::Query
mutation Types::Mutation

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
2 changes: 1 addition & 1 deletion app/graphql/types/division.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Types::Division < Types::BaseObject
graphql_name "Division"
description "A Division"

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

field :id, ID, null: true
global_id_field :id
field :name, String, null: true
field :lat, Float, null: true
field :long, Float, null: true
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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: true
field :pool, String, null: true
field :bracketUid, String, null: true
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/score_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Types::ScoreReport < Types::BaseObject
graphql_name "ScoreReport"
description "A ScoreReport"

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

field :id, ID, null: true
global_id_field :id
field :name, String, null: true
field :email, String, auth: :required, null: true
field :phone, String, auth: :required, null: true
Expand Down
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 9eb4c4c

Please sign in to comment.