1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
# frozen_string_literal: true
require "spec_helper"
describe GraphQL::Schema::Mutation do
let(:mutation) { Jazz::AddInstrument }
after do
Jazz::Models.reset
end
it "Doesn't override !" do
assert_equal false, !mutation
end
describe "definition" do
it "passes along description" do
assert_equal "Register a new musical instrument in the database", Jazz::Mutation.get_field("addInstrument").description
assert_equal "Autogenerated return type of AddInstrument.", mutation.payload_type.description
end
end
describe "argument prepare" do
it "calls methods on the mutation, uses `as:`" do
query_str = "mutation { prepareInput(input: 4) }"
res = Jazz::Schema.execute(query_str)
assert_equal 16, res["data"]["prepareInput"], "It's squared by the prepare method"
end
end
describe "a derived field" do
it "has a reference to the mutation" do
test_setup = self
t = Class.new(GraphQL::Schema::Object) do
field :x, mutation: test_setup.mutation
end
f = t.get_field("x")
assert_equal mutation, f.mutation
# Make sure it's also present in the schema
f2 = Jazz::Schema.find("Mutation.addInstrument")
assert_equal mutation, f2.mutation
end
end
describe ".payload_type" do
it "has a reference to the mutation" do
assert_equal mutation, mutation.payload_type.mutation
end
end
describe ".object_class" do
it "can override & inherit the parent class" do
obj_class = Class.new(GraphQL::Schema::Object)
mutation_class = Class.new(GraphQL::Schema::Mutation) do
object_class(obj_class)
end
mutation_subclass = Class.new(mutation_class)
assert_equal(GraphQL::Schema::Object, GraphQL::Schema::Mutation.object_class)
assert_equal(obj_class, mutation_class.object_class)
assert_equal(obj_class, mutation_subclass.object_class)
end
end
describe ".argument_class" do
it "can override & inherit the parent class" do
arg_class = Class.new(GraphQL::Schema::Argument)
mutation_class = Class.new(GraphQL::Schema::Mutation) do
argument_class(arg_class)
end
mutation_subclass = Class.new(mutation_class)
assert_equal(GraphQL::Schema::Argument, GraphQL::Schema::Mutation.argument_class)
assert_equal(arg_class, mutation_class.argument_class)
assert_equal(arg_class, mutation_subclass.argument_class)
end
end
describe "evaluation" do
it "runs mutations" do
query_str = <<-GRAPHQL
mutation {
addInstrument(name: "trombone", family: BRASS) {
instrument {
name
family
}
entries {
name
}
ee
}
}
GRAPHQL
response = Jazz::Schema.execute(query_str)
assert_equal "Trombone", response["data"]["addInstrument"]["instrument"]["name"]
assert_equal "BRASS", response["data"]["addInstrument"]["instrument"]["family"]
errors_class = "GraphQL::Execution::Interpreter::ExecutionErrors"
assert_equal errors_class, response["data"]["addInstrument"]["ee"]
assert_equal 7, response["data"]["addInstrument"]["entries"].size
end
it "accepts a list of errors as a valid result" do
query_str = "mutation { returnsMultipleErrors { dummyField { name } } }"
response = Jazz::Schema.execute(query_str)
assert_equal 2, response["errors"].length, "It should return two errors"
end
it "raises a mutation-specific invalid null error" do
query_str = "mutation { returnInvalidNull { int } }"
response = Jazz::Schema.execute(query_str)
assert_equal ["Cannot return null for non-nullable field ReturnInvalidNullPayload.int"], response["errors"].map { |e| e["message"] }
end
end
describe ".null" do
it "overrides whether or not the field can be null" do
non_nullable_mutation_class = Class.new(GraphQL::Schema::Mutation) do
graphql_name "Thing1"
null(false)
end
nullable_mutation_class = Class.new(GraphQL::Schema::Mutation) do
graphql_name "Thing2"
null(true)
end
default_mutation_class = Class.new(GraphQL::Schema::Mutation) do
graphql_name "Thing3"
end
example_mutation_type = Class.new(GraphQL::Schema::Object) do
field :non_nullable_mutation, mutation: non_nullable_mutation_class
field :nullable_mutation, mutation: nullable_mutation_class
field :default_mutation, mutation: default_mutation_class
end
refute example_mutation_type.get_field("defaultMutation").type.non_null?
refute example_mutation_type.get_field("nullableMutation").type.non_null?
assert example_mutation_type.get_field("nonNullableMutation").type.non_null?
end
it "should inherit and override in subclasses" do
base_mutation = Class.new(GraphQL::Schema::Mutation) do
null(false)
end
inheriting_mutation = Class.new(base_mutation) do
graphql_name "Thing"
end
override_mutation = Class.new(base_mutation) do
graphql_name "Thing2"
null(true)
end
f1 = GraphQL::Schema::Field.new(name: "f1", resolver_class: inheriting_mutation)
assert_equal true, f1.type.non_null?
f2 = GraphQL::Schema::Field.new(name: "f2", resolver_class: override_mutation)
assert_equal false, f2.type.non_null?
end
end
it "warns once for possible conflict methods" do
expected_warning = "X's `field :module` conflicts with a built-in method, use `hash_key:` or `method:` to pick a different resolve behavior for this field (for example, `hash_key: :module_value`, and modify the return hash). Or use `method_conflict_warning: false` to suppress this warning.\n"
assert_output "", expected_warning do
# This should warn:
mutation = Class.new(GraphQL::Schema::Mutation) do
graphql_name "X"
field :module, String
end
# This should not warn again, when generating the payload type with the same fields:
mutation.payload_type
end
assert_output "", "" do
mutation = Class.new(GraphQL::Schema::Mutation) do
graphql_name "X"
field :module, String, hash_key: :module_value
end
mutation.payload_type
end
end
class InterfaceMutationSchema < GraphQL::Schema
class SignIn < GraphQL::Schema::Mutation
argument :login, String
argument :password, String
field :success, Boolean, null: false
def resolve(login:, password:)
{ success: login == password }
end
end
module Auth
include GraphQL::Schema::Interface
field :sign_in, mutation: SignIn
end
class Mutation < GraphQL::Schema::Object
implements Auth
end
mutation(Mutation)
query(Mutation)
end
it "works when mutations are added via interfaces" do
result = InterfaceMutationSchema.execute("mutation { signIn(login: \"abc\", password: \"abc\") { success } }")
assert_equal true, result["data"]["signIn"]["success"]
end
it "returns manually-configured return types" do
mutation = Class.new(GraphQL::Schema::Mutation) do
graphql_name "DoStuff"
type(String)
end
field = GraphQL::Schema::Field.new(name: "f", owner: nil, resolver_class: mutation)
assert_equal "String", field.type.graphql_name
assert_equal GraphQL::Types::String, field.type
end
it "inherits arguments even when parent classes aren't attached to the schema" do
parent_mutation = Class.new(GraphQL::Schema::Mutation) do
graphql_name "ParentMutation"
argument :thing_id, "ID"
field :inputs, String
def resolve(**inputs)
{ inputs: inputs.inspect }
end
end
child_mutation = Class.new(parent_mutation) do
graphql_name "ChildMutation"
argument :thing_name, String
end
mutation_type = Class.new(GraphQL::Schema::Object) do
graphql_name "Mutation"
field :child, mutation: child_mutation
end
schema = Class.new(GraphQL::Schema) do
mutation(mutation_type)
end
assert_equal ["thingId", "thingName"], child_mutation.arguments.keys
assert_equal ["thingId", "thingName"], child_mutation.all_argument_definitions.map(&:graphql_name)
assert_equal ["thingId", "thingName"], schema.mutation.fields["child"].all_argument_definitions.map(&:graphql_name)
res = schema.execute("mutation { child(thingName: \"abc\", thingId: \"123\") { inputs } }")
expected_result = { thing_id: "123", thing_name: "abc" }.inspect
assert_equal expected_result, res["data"]["child"]["inputs"]
end
describe "flushing dataloader cache" do
class MutationDataloaderCacheSchema < GraphQL::Schema
module Database
DATA = {}
def self.get(id)
value = DATA[id] ||= 0
OpenStruct.new(id: id, value: value)
end
def self.increment(id)
DATA[id] ||= 0
DATA[id] += 1
end
def self.clear
DATA.clear
end
end
class CounterSource < GraphQL::Dataloader::Source
def fetch(ids)
ids.map { |id| Database.get(id) }
end
end
class CounterType < GraphQL::Schema::Object
def self.authorized?(obj, ctx)
# Just force the load here, too:
ctx.dataloader.with(CounterSource).load(obj.id)
true
end
field :value, Integer
end
class Increment < GraphQL::Schema::Mutation
field :counter, CounterType
argument :counter_id, ID, loads: CounterType
def resolve(counter:)
Database.increment(counter.id)
{
counter: dataloader.with(CounterSource).load(counter.id)
}
end
end
class ReadyCounter < GraphQL::Schema::Mutation
field :id, ID
argument :counter_id, ID
def ready?(counter_id:)
# Just fill the cache:
dataloader.with(CounterSource).load(counter_id)
true
end
def resolve(counter_id:)
{ id: counter_id }
end
end
class Mutation < GraphQL::Schema::Object
field :increment, mutation: Increment
field :ready_counter, mutation: ReadyCounter
end
mutation(Mutation)
def self.object_from_id(id, ctx)
ctx.dataloader.with(CounterSource).load(id)
end
def self.resolve_type(abs_type, obj, ctx)
CounterType
end
use GraphQL::Dataloader
end
it "clears the cache after authorized and loads" do
MutationDataloaderCacheSchema::Database.clear
res = MutationDataloaderCacheSchema.execute("mutation { increment(counterId: \"4\") { counter { value } } }")
assert_equal 1, res["data"]["increment"]["counter"]["value"]
res2 = MutationDataloaderCacheSchema.execute("mutation { increment(counterId: \"4\") { counter { value } } }")
assert_equal 2, res2["data"]["increment"]["counter"]["value"]
end
it "uses a fresh cache for `ready?` calls" do
multiplex = [
{ query: "mutation { r1: readyCounter(counterId: 1) { id } }" },
{ query: "mutation { r2: readyCounter(counterId: 1) { id } }" },
{ query: "mutation { r3: readyCounter(counterId: 1) { id } }" },
]
result = MutationDataloaderCacheSchema.multiplex(multiplex)
assert_equal ["1", "1", "1"], result.map { |r| r["data"].first.last["id"] }
end
end
end
|