Skip to content

Commit 5fcc6b0

Browse files
committed
feat(Context) provide access to context.ast_node
1 parent 1f813e5 commit 5fcc6b0

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

lib/graphql/query.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def validation_errors
5252
# Expose some query-specific info to field resolve functions.
5353
# It delegates `[]` to the hash that's passed to `GraphQL::Query#initialize`.
5454
class Context
55-
attr_accessor :execution_strategy
55+
attr_accessor :execution_strategy, :ast_node
5656
def initialize(values:)
5757
@values = values
5858
end

lib/graphql/query/base_execution.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
module GraphQL
55
class Query
66
class BaseExecution
7+
# This is the only required method for an Execution strategy.
8+
# You could create a custom execution strategy and configure your schema to
9+
# use that custom strategy instead.
10+
#
711
# @param ast_operation [GraphQL::Language::Nodes::OperationDefinition] The operation definition to run
812
# @param root_type [GraphQL::ObjectType] either the query type or the mutation type
913
# @param query_obj [GraphQL::Query] the query object for this execution

lib/graphql/query/serial_execution/field_resolution.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def get_finished_value(raw_value)
3131

3232

3333
def get_raw_value
34+
query.context.ast_node = ast_node
3435
value = field.resolve(target, arguments, query.context)
36+
query.context.ast_node = nil
3537

3638
if value == GraphQL::Query::DEFAULT_RESOLVE
3739
begin

readme.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ If you're building a backend for [Relay](http://facebook.github.io/relay/), you'
9898
- Raise if you try to configure an attribute which doesn't suit the type
9999
- ie, if you try to define `resolve` on an ObjectType, it should somehow raise
100100
- Big ideas:
101-
- Cook up some path other than "n+1s everywhere"
102-
- See Sangria's `project` approach ([in progress](https://github.com/rmosolgo/graphql-ruby/pull/15))
103-
- Try debounced approach?
104101
- Write Ruby bindings for [libgraphqlparser](https://github.com/graphql/libgraphqlparser) and use that instead of Parslet
105102
- Add instrumentation
106103
- Some way to expose what queries are run, what types & fields are accessed, how long things are taking, etc

spec/graphql/query_spec.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,33 @@
151151
argument :key, !types.String
152152
resolve -> (target, args, ctx) { ctx[args[:key]] }
153153
end
154+
field :contextAstNodeName, types.String do
155+
resolve -> (target, args, ctx) { ctx.ast_node.class.name }
156+
end
154157
}}
155158
let(:schema) { GraphQL::Schema.new(query: query_type, mutation: nil)}
156159
let(:query) { GraphQL::Query.new(schema, query_string, context: {"some_key" => "some value"})}
157-
let(:query_string) { %|
158-
query getCtx { context(key: "some_key") }
159-
|}
160160

161-
it 'passes context to fields' do
162-
expected = {"data" => {"context" => "some value"}}
163-
assert_equal(expected, query.result)
161+
describe "access to passed-in values" do
162+
let(:query_string) { %|
163+
query getCtx { context(key: "some_key") }
164+
|}
165+
166+
it 'passes context to fields' do
167+
expected = {"data" => {"context" => "some value"}}
168+
assert_equal(expected, query.result)
169+
end
170+
end
171+
172+
describe "access to the AST node" do
173+
let(:query_string) { %|
174+
query getCtx { contextAstNodeName }
175+
|}
176+
177+
it 'provides access to the AST node' do
178+
expected = {"data" => {"contextAstNodeName" => "GraphQL::Language::Nodes::Field"}}
179+
assert_equal(expected, query.result)
180+
end
164181
end
165182
end
166183
end

0 commit comments

Comments
 (0)