Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scoping of context for mutations #1549

Merged
merged 1 commit into from
Jun 10, 2024
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 src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ function ($results, $responseName) use ($contextValue, $path, $parentType, $root

$fieldPath = $path;
$fieldPath[] = $responseName;
$result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath, $contextValue);
$result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath, $this->maybeScopeContext($contextValue));
if ($result === static::$UNDEFINED) {
return $results;
}
Expand Down
91 changes: 90 additions & 1 deletion tests/Executor/ScopedContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ public function setUp(): void
$context->path[] = 'g';
$this->contexts['g'] = $context;

return $rootValue;
},
],
],
]),
'mutation' => new ObjectType([
'name' => 'Mutation',
'fields' => [
'a' => [
'type' => $a,
'resolve' => function ($rootValue, $args, $context) {
$context->path[] = 'a';
$this->contexts['a'] = $context;

return $rootValue;
},
],
'g' => [
'type' => $g,
'resolve' => function ($rootValue, $args, $context) {
$context->path[] = 'g';
$this->contexts['g'] = $context;

return $rootValue;
},
],
Expand All @@ -156,7 +179,7 @@ public function setUp(): void
$this->promiseAdapter = new SyncPromiseAdapter();
}

public function testContextShouldBeScopedBeforePassingToChildren(): void
public function testQueryContextShouldBeScopedBeforePassingToChildren(): void
{
$context = new MyScopedContext();

Expand Down Expand Up @@ -222,6 +245,72 @@ public function testContextShouldBeScopedBeforePassingToChildren(): void
], $result->data);
}

public function testMutationContextShouldBeScopedBeforePassingToChildren(): void
{
$context = new MyScopedContext();

$doc = <<<'GRAPHQL'
mutation {
a {
b {
c
}
d {
e {
f
}
}
}
g {
h {
i
}
}
}
GRAPHQL;

$result = Executor::promiseToExecute(
$this->promiseAdapter,
$this->schema,
Parser::parse($doc),
'rootValue',
$context,
[],
null,
null
);

$result = $this->promiseAdapter->wait($result);

self::assertSame([], $context->path);
self::assertSame(['a'], $this->contexts['a']->path);
self::assertSame(['a', 'b'], $this->contexts['b']->path);
self::assertSame(['a', 'b', 'c'], $this->contexts['c']->path);
self::assertSame(['a', 'd'], $this->contexts['d']->path);
self::assertSame(['a', 'd', 'e'], $this->contexts['e']->path);
self::assertSame(['a', 'd', 'e', 'f'], $this->contexts['f']->path);
self::assertSame(['g'], $this->contexts['g']->path);
self::assertSame(['g', 'h'], $this->contexts['h']->path);
self::assertSame(['g', 'h', 'i'], $this->contexts['i']->path);
self::assertSame([
'a' => [
'b' => [
'c' => 'a.b.c',
],
'd' => [
'e' => [
'f' => 'a.d.e.f',
],
],
],
'g' => [
'h' => [
'i' => 'g.h.i',
],
],
], $result->data);
}

public function testContextShouldNotBeScoped(): void
{
$context = new MySharedContext();
Expand Down
Loading