diff --git a/src/Rebing/GraphQL/GraphQL.php b/src/Rebing/GraphQL/GraphQL.php index 6ac5da0b..3bcf282d 100644 --- a/src/Rebing/GraphQL/GraphQL.php +++ b/src/Rebing/GraphQL/GraphQL.php @@ -210,8 +210,18 @@ protected function buildObjectTypeFromFields($fields, $opts = []) } public function addSchema($name, $schema) + { + $this->mergeSchemas($name, $schema); + } + + public function mergeSchemas($name, $schema) { - $this->schemas[$name] = $schema; + if (isset($this->schemas[$name]) && $this->schemas[$name]) { + $this->schemas[$name] = array_merge_recursive($this->schemas[$name], $schema); + } + else { + $this->schemas[$name] = $schema; + } } public function clearType($name) diff --git a/tests/GraphQLTest.php b/tests/GraphQLTest.php index cb7057dd..c0308b95 100644 --- a/tests/GraphQLTest.php +++ b/tests/GraphQLTest.php @@ -270,6 +270,41 @@ public function testGetTypes() public function testAddSchema() { GraphQL::addSchema('custom_add', [ + 'query' => [ + 'examplesCustom' => ExamplesQuery::class + ], + 'mutation' => [ + 'updateExampleCustom' => UpdateExampleMutation::class + ], + 'types' => [ + CustomExampleType::class + ] + ]); + + $schemas = GraphQL::getSchemas(); + $this->assertArrayHasKey('custom_add', $schemas); + } + + /** + * Test merge schema + * + * @test + */ + public function testMergeSchema() + { + GraphQL::addSchema('custom_add', [ + 'query' => [ + 'examplesCustom' => ExamplesQuery::class + ], + 'mutation' => [ + 'updateExampleCustom' => UpdateExampleMutation::class + ], + 'types' => [ + CustomExampleType::class + ] + ]); + + GraphQL::addSchema('custom_add_another', [ 'query' => [ 'examplesCustom' => ExamplesQuery::class ], @@ -283,6 +318,20 @@ public function testAddSchema() $schemas = GraphQL::getSchemas(); $this->assertArrayHasKey('custom_add', $schemas); + $this->assertArrayHasKey('custom_add_another', $schemas); + + GraphQL::addSchema('custom_add_another', [ + 'query' => [ + 'examplesCustomAnother' => ExamplesQuery::class + ] + ]); + + $schemas = GraphQL::getSchemas(); + $this->assertArrayHasKey('custom_add_another', $schemas); + + $querys = $schemas['custom_add_another']['query']; + $this->assertArrayHasKey('examplesCustom', $querys); + $this->assertArrayHasKey('examplesCustomAnother', $querys); } /**