This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use GraphQL Batch for find by id fields
- Loading branch information
Showing
8 changed files
with
56 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Graph | ||
class << self | ||
def find_by_id_field(type, model) | ||
GraphQL::Field.define do | ||
type type | ||
argument :id, !types.ID | ||
resolve ->(_, args, _) do | ||
gid = GlobalID.parse(args[:id]) | ||
|
||
return unless gid.model_name == type.name | ||
|
||
Graph::FindLoader.for(model).load(gid.model_id.to_i) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Graph::FindLoader < GraphQL::Batch::Loader | ||
def initialize(model) | ||
@model = model | ||
end | ||
|
||
def perform(ids) | ||
records = @model.where(id: ids.uniq) | ||
records.each { |record| fulfill(record.id, record) } | ||
ids.each { |id| fulfill(id, nil) unless fulfilled?(id) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
development: | ||
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> | ||
secret_key_base: secret-for-development-not-so-secret | ||
|
||
test: | ||
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> | ||
secret_key_base: secret-for-test-not-so-secret | ||
|
||
production: | ||
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters