Skip to content

Commit 1325f44

Browse files
MrGekkoMikk Mihkel Nurges
authored and
Mikk Mihkel Nurges
committed
Merge schema's instead of overwriting (#218)
* Update GraphQL.php * add Merge schema test * Test implemented incorrectly
1 parent a5d5a25 commit 1325f44

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/Rebing/GraphQL/GraphQL.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,18 @@ protected function buildObjectTypeFromFields($fields, $opts = [])
210210
}
211211

212212
public function addSchema($name, $schema)
213+
{
214+
$this->mergeSchemas($name, $schema);
215+
}
216+
217+
public function mergeSchemas($name, $schema)
213218
{
214-
$this->schemas[$name] = $schema;
219+
if (isset($this->schemas[$name]) && $this->schemas[$name]) {
220+
$this->schemas[$name] = array_merge_recursive($this->schemas[$name], $schema);
221+
}
222+
else {
223+
$this->schemas[$name] = $schema;
224+
}
215225
}
216226

217227
public function clearType($name)

tests/GraphQLTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,41 @@ public function testGetTypes()
270270
public function testAddSchema()
271271
{
272272
GraphQL::addSchema('custom_add', [
273+
'query' => [
274+
'examplesCustom' => ExamplesQuery::class
275+
],
276+
'mutation' => [
277+
'updateExampleCustom' => UpdateExampleMutation::class
278+
],
279+
'types' => [
280+
CustomExampleType::class
281+
]
282+
]);
283+
284+
$schemas = GraphQL::getSchemas();
285+
$this->assertArrayHasKey('custom_add', $schemas);
286+
}
287+
288+
/**
289+
* Test merge schema
290+
*
291+
* @test
292+
*/
293+
public function testMergeSchema()
294+
{
295+
GraphQL::addSchema('custom_add', [
296+
'query' => [
297+
'examplesCustom' => ExamplesQuery::class
298+
],
299+
'mutation' => [
300+
'updateExampleCustom' => UpdateExampleMutation::class
301+
],
302+
'types' => [
303+
CustomExampleType::class
304+
]
305+
]);
306+
307+
GraphQL::addSchema('custom_add_another', [
273308
'query' => [
274309
'examplesCustom' => ExamplesQuery::class
275310
],
@@ -283,6 +318,20 @@ public function testAddSchema()
283318

284319
$schemas = GraphQL::getSchemas();
285320
$this->assertArrayHasKey('custom_add', $schemas);
321+
$this->assertArrayHasKey('custom_add_another', $schemas);
322+
323+
GraphQL::addSchema('custom_add_another', [
324+
'query' => [
325+
'examplesCustomAnother' => ExamplesQuery::class
326+
]
327+
]);
328+
329+
$schemas = GraphQL::getSchemas();
330+
$this->assertArrayHasKey('custom_add_another', $schemas);
331+
332+
$querys = $schemas['custom_add_another']['query'];
333+
$this->assertArrayHasKey('examplesCustom', $querys);
334+
$this->assertArrayHasKey('examplesCustomAnother', $querys);
286335
}
287336

288337
/**

0 commit comments

Comments
 (0)