Skip to content

Merge schema's instead of overwriting #218

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

Merged
merged 3 commits into from
Feb 26, 2019
Merged
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
12 changes: 11 additions & 1 deletion src/Rebing/GraphQL/GraphQL.php
Original file line number Diff line number Diff line change
@@ -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)
49 changes: 49 additions & 0 deletions tests/GraphQLTest.php
Original file line number Diff line number Diff line change
@@ -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);
}

/**