Skip to content

Commit

Permalink
Add support for type config decorator in SchemaExtender (#871)
Browse files Browse the repository at this point in the history
(cherry picked from commit ccc4746)
  • Loading branch information
AdrienPoupa authored and spawnia committed Jun 15, 2021
1 parent 578d087 commit f304483
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Utils/SchemaExtender.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,12 @@ protected static function extendDirective(Directive $directive) : Directive
/**
* @param array<string, bool> $options
*/
public static function extend(Schema $schema, DocumentNode $documentAST, array $options = []) : Schema
{
public static function extend(
Schema $schema,
DocumentNode $documentAST,
array $options = [],
?callable $typeConfigDecorator = null
) : Schema {
if (! (isset($options['assumeValid']) || isset($options['assumeValidSDL']))) {
DocumentValidator::assertValidSDLExtension($documentAST, $schema);
}
Expand Down Expand Up @@ -579,7 +583,8 @@ static function (string $typeName) use ($schema) {
}

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

static::$extendTypeCache = [];
Expand Down
51 changes: 51 additions & 0 deletions tests/Utils/SchemaExtenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2038,4 +2038,55 @@ public function testShouldBeAbleToIntroduceNewTypesThroughExtension()

static::assertEquals($this->dedent($expected), SchemaPrinter::doPrint($extendedSchema));
}

public function testSupportsTypeConfigDecorator()
{
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'hello' => [
'type' => Type::string(),
],
],
'resolveField' => static function () : string {
return 'Hello World!';
},
]);

$schema = new Schema(['query' => $queryType]);

$documentNode = Parser::parse('
type Foo {
value: String
}
extend type Query {
defaultValue: String
foo: Foo
}
');

$typeConfigDecorator = static function ($typeConfig) {
switch ($typeConfig['name']) {
case 'Foo':
$typeConfig['resolveField'] = static function () : string {
return 'bar';
};
break;
}

return $typeConfig;
};

$extendedSchema = SchemaExtender::extend($schema, $documentNode, [], $typeConfigDecorator);

$query = '{
hello
foo {
value
}
}';
$result = GraphQL::executeQuery($extendedSchema, $query);

self::assertSame(['data' => ['hello' => 'Hello World!', 'foo' => ['value' => 'bar']]], $result->toArray());
}
}

0 comments on commit f304483

Please sign in to comment.