Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions lib/rails/graphql/request/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ def resolve_data_for(field, args)
return unless args.size.zero?

if field.try(:dynamic_resolver?)
prepared = prepared_data_for(field)
args << Event.trigger(:resolve, field, self, prepared_data: prepared, &field.resolver)
extra = prepared_data_for(field, with_null: true)
extra = extra === PreparedData::NULL ? EMPTY_HASH : { prepared: extra }
args << Event.trigger(:resolve, field, self, **extra, &field.resolver)
elsif field.prepared_data?
args << prepared_data_for(field)
else
Expand Down Expand Up @@ -206,11 +207,14 @@ def safe_store_data(field, value = nil)

# Get the prepared data for the given +field+, getting ready for
# resolve, while ensuring to check prepared data on request
def prepared_data_for(field)
return @data_pool[field] unless field.prepared_data?

prepared = request.prepared_data_for(field).next
prepared unless prepared === PreparedData::NULL
def prepared_data_for(field, with_null: false)
if field.prepared_data?
request.prepared_data_for(field).next
elsif @data_pool.key?(field)
@data_pool[field]
elsif with_null
PreparedData::NULL
end
end

# Simply run the organize step for compilation
Expand Down
6 changes: 6 additions & 0 deletions lib/rails/graphql/source/active_record_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ def build_association_scope(association)
# Once the records are pre-loaded due to +preload_association+, use the
# parent value and the preloader result to get the records
def parent_owned_records(collection_result = false)
# The absence of the prepared data key means we got to a point that we
# don't know the result of the association, so we simply call it
unless event.data.key?(:prepared_data)
return current_value.public_send(field.method_name)
end

data = event.data[:prepared_data]
return collection_result ? [] : nil unless data

Expand Down
2 changes: 1 addition & 1 deletion lib/rails/graphql/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.version
module VERSION
MAJOR = 1
MINOR = 0
TINY = 5
TINY = 6
PRE = nil

STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
Expand Down
6 changes: 6 additions & 0 deletions test/assets/sqlite.gql
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ type LiteShip {
name: String
}

type Sample {
faction: LiteFaction!
}

type _Mutation {
createLiteBase(liteBase: LiteBaseInput!): LiteBase!

Expand Down Expand Up @@ -262,6 +266,8 @@ type _Query {
liteShip(id: ID!): LiteShip!

liteShips: [LiteShip!]!

sample: Sample!
}

"""
Expand Down
2 changes: 1 addition & 1 deletion test/integration/schemas/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MySQLRecord < ActiveRecord::Base
establish_connection(
name: 'mysql',
adapter: 'mysql2',
host: ENV.fetch('GQL_MYSQL_HOST', 'localhost'),
host: ENV.fetch('GQL_MYSQL_HOST', '127.0.0.1'),
database: ENV.fetch('GQL_MYSQL_DATABASE', 'starwars'),
username: ENV.fetch('GQL_MYSQL_USERNAME', 'root'),
password: ENV['GQL_MYSQL_PASSWORD'],
Expand Down
12 changes: 12 additions & 0 deletions test/integration/schemas/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,16 @@ def greeting
end

source LiteShip

object 'Sample' do
field :faction, 'LiteFaction', null: false
end

query_fields do
field :sample, 'Sample', null: false
end

def sample
{ faction: LiteFaction.last }
end
end
9 changes: 9 additions & 0 deletions test/integration/sqlite/star_wars_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ def test_query_methods_precedence
query EmpireFleet { liteFaction(id: "2") { greeting } }
GQL
end

def test_nested_non_prepared_source
bases = named_list('Death Star', 'Shield Generator', 'Headquarters')
sample = { sample: { faction: { name: 'Galactic Empire', bases: bases } } }

assert_result({ data: sample }, <<~GQL)
query SampleFaction { sample { faction { name bases { name } } } }
GQL
end
end