Skip to content

Commit ccc4746

Browse files
authored
Add support for type config decorator in SchemaExtender (#871)
1 parent 5bc702b commit ccc4746

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/Utils/SchemaExtender.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,12 @@ protected static function extendDirective(Directive $directive): Directive
506506
/**
507507
* @param array<string, bool> $options
508508
*/
509-
public static function extend(Schema $schema, DocumentNode $documentAST, array $options = []): Schema
510-
{
509+
public static function extend(
510+
Schema $schema,
511+
DocumentNode $documentAST,
512+
array $options = [],
513+
?callable $typeConfigDecorator = null
514+
): Schema {
511515
if (! (isset($options['assumeValid']) || isset($options['assumeValidSDL']))) {
512516
DocumentValidator::assertValidSDLExtension($documentAST, $schema);
513517
}
@@ -588,7 +592,8 @@ static function (string $typeName) use ($schema) {
588592
}
589593

590594
throw new Error('Unknown type: "' . $typeName . '". Ensure that this type exists either in the original schema, or is added in a type definition.', [$typeName]);
591-
}
595+
},
596+
$typeConfigDecorator
592597
);
593598

594599
static::$extendTypeCache = [];

tests/Utils/SchemaExtenderTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,4 +2039,55 @@ public function testShouldBeAbleToIntroduceNewTypesThroughExtension()
20392039

20402040
static::assertEquals($this->dedent($expected), SchemaPrinter::doPrint($extendedSchema));
20412041
}
2042+
2043+
public function testSupportsTypeConfigDecorator()
2044+
{
2045+
$queryType = new ObjectType([
2046+
'name' => 'Query',
2047+
'fields' => [
2048+
'hello' => [
2049+
'type' => Type::string(),
2050+
],
2051+
],
2052+
'resolveField' => static function (): string {
2053+
return 'Hello World!';
2054+
},
2055+
]);
2056+
2057+
$schema = new Schema(['query' => $queryType]);
2058+
2059+
$documentNode = Parser::parse('
2060+
type Foo {
2061+
value: String
2062+
}
2063+
extend type Query {
2064+
defaultValue: String
2065+
foo: Foo
2066+
}
2067+
');
2068+
2069+
$typeConfigDecorator = static function ($typeConfig) {
2070+
switch ($typeConfig['name']) {
2071+
case 'Foo':
2072+
$typeConfig['resolveField'] = static function (): string {
2073+
return 'bar';
2074+
};
2075+
break;
2076+
}
2077+
2078+
return $typeConfig;
2079+
};
2080+
2081+
$extendedSchema = SchemaExtender::extend($schema, $documentNode, [], $typeConfigDecorator);
2082+
2083+
$query = '{
2084+
hello
2085+
foo {
2086+
value
2087+
}
2088+
}';
2089+
$result = GraphQL::executeQuery($extendedSchema, $query);
2090+
2091+
self::assertSame(['data' => ['hello' => 'Hello World!', 'foo' => ['value' => 'bar']]], $result->toArray());
2092+
}
20422093
}

0 commit comments

Comments
 (0)