@@ -62,23 +62,45 @@ def after_query(query)
62
62
end
63
63
end
64
64
65
+ # This is how you might add queries from a persisted query backend
66
+
67
+ class QueryStringInstrumenter
68
+ def before_query ( query )
69
+ if query . context [ :extra_query_string ] && query . query_string . nil?
70
+ query . query_string = query . context [ :extra_query_string ]
71
+ end
72
+ end
73
+
74
+ def after_query ( query )
75
+ end
76
+ end
77
+
65
78
let ( :query_type ) {
66
- GraphQL ::ObjectType . define do
67
- name "Query"
68
- field :int , types . Int do
69
- argument :value , types . Int
70
- resolve -> ( obj , args , ctx ) { args . value }
79
+ Class . new ( GraphQL ::Schema ::Object ) do
80
+ graphql_name "Query"
81
+ field :int , Integer , null : true do
82
+ argument :value , Integer , required : false
83
+ end
84
+
85
+ def int ( value :)
86
+ value
71
87
end
72
88
end
73
89
}
74
90
75
91
let ( :schema ) {
76
92
spec = self
77
- GraphQL ::Schema . define do
93
+ Class . new ( GraphQL ::Schema ) do
78
94
query ( spec . query_type )
79
95
instrument ( :query , FirstInstrumenter . new )
80
96
instrument ( :query , SecondInstrumenter . new )
81
97
instrument ( :query , ExecutionErrorInstrumenter . new )
98
+ instrument ( :query , QueryStringInstrumenter . new )
99
+
100
+ if TESTING_INTERPRETER
101
+ use GraphQL ::Analysis ::AST
102
+ use GraphQL ::Execution ::Interpreter
103
+ end
82
104
end
83
105
}
84
106
@@ -114,6 +136,12 @@ def after_query(query)
114
136
assert_equal "Raised from instrumenter before_query" , res [ "errors" ] . first [ "message" ]
115
137
refute res . key? ( "data" ) , "The query doesn't run"
116
138
end
139
+
140
+ it "can assign a query string there" do
141
+ context = { extra_query_string : "{ __typename }" }
142
+ res = schema . execute ( nil , context : context )
143
+ assert_equal "Query" , res [ "data" ] [ "__typename" ]
144
+ end
117
145
end
118
146
119
147
describe "within a multiplex" do
0 commit comments