Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "olamobile/graphql-php-tools",
"name": "garlic/graphql-php-tools",
"description": "Olamobile GraphQL multiple schemas stitching.",
"license": "GPL (free)",
"license": "MIT",
"minimum-stability": "stable",
"repositories": [
],
Expand All @@ -13,7 +13,7 @@
},
"autoload": {
"psr-4": {
"Ola\\": "src/Ola"
"Ola\\": "src/"
}
}
}
11 changes: 6 additions & 5 deletions src/Ola/Graphql/Tools/BuildClientSchema.php → src/Tools/BuildClientSchema.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace Ola\GraphQL\Tools;
namespace Ola\Tools;

use GraphQL\Error\Error;
use GraphQL\Executor\Values;

use GraphQL\Language\DirectiveLocation;
use GraphQL\Language\Parser;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\NodeKind;
Expand All @@ -27,7 +28,7 @@
use GraphQL\Type\Definition\UnionType;

use GraphQL\Utils\AST;
use GraphQL\Utils\Utils;
//use GraphQL\Utils\Utils;



Expand Down Expand Up @@ -296,7 +297,7 @@ private function getType($typeRef) {
}

private function getNamedType($typeName){
if ($this->typeDefCache[$typeName]) {
if (isset($this->typeDefCache[$typeName])) {
return $this->typeDefCache[$typeName];
}
$typeIntrospection = $this->typeIntrospectionMap[$typeName];
Expand All @@ -322,7 +323,7 @@ private function getObjectType($typeRef) {

private function buildDirective($directiveIntrospection) {
// Support deprecated `on****` fields for building `locations`, as this is used by GraphiQL which may need to support outdated servers.
$locations = $directiveIntrospection->locations ? $directiveIntrospection->locations : [];
$locations = isset($directiveIntrospection->locations) ? $directiveIntrospection->locations : [];
$onField = !$directiveIntrospection->onField ? [] : [DirectiveLocation::FIELD];
$onOperation = !$directiveIntrospection->onOperation ? [] : [DirectiveLocation::QUERY, DirectiveLocation::MUTATION, DirectiveLocation::SUBSCRIPTION];
$onFragment = !$directiveIntrospection->onFragment ? [] : [DirectiveLocation::FRAGMENT_DEFINITION, DirectiveLocation::FRAGMENT_SPREAD, DirectiveLocation::INLINE_FRAGMENT];
Expand All @@ -332,7 +333,7 @@ private function buildDirective($directiveIntrospection) {
return new Directive([
'name' => $directiveIntrospection->name,
'description' => $directiveIntrospection->description,
$locations,
'locations' => $locations,
'args' => $this->buildInputValueDefMap($directiveIntrospection->args),
]);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Ola/Graphql/Tools/ExecutableSchema.php → src/Tools/ExecutableSchema.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Ola\GraphQL\Tools;
namespace Ola\Tools;

use GraphQL\Error\Error;
use GraphQL\Executor\Values;
Expand Down Expand Up @@ -30,9 +30,9 @@
use GraphQL\Type\TypeKind;

use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\Utils;
//use GraphQL\Utils\Utils;

use Ola\GraphQL\Tools\ExtendSchema;
use Ola\Tools\ExtendSchema;

class SchemaError extends \ErrorException {
public $message;
Expand Down Expand Up @@ -293,8 +293,9 @@ public static function extractExtensionDefinitions(DocumentNode $ast) {
return $ast;
}

public static function addResolveFunctionsToSchema(&$schema, $resolveFunctions) {
public static function addResolveFunctionsToSchema(Schema &$schema, $resolveFunctions) {
foreach ($resolveFunctions as $typeName => $resolver) {

$type = $schema->getType($typeName);
if (!$type && $typeName !== '__schema') {
throw new SchemaError("\"$typeName\" defined in resolvers, but not in schema");
Expand Down Expand Up @@ -323,6 +324,7 @@ public static function addResolveFunctionsToSchema(&$schema, $resolveFunctions)
}
$field = $fields[$fieldName];
$fieldResolve = $callable;

if (is_callable($fieldResolve)) {
// for convenience. Allows shorter syntax in resolver definition file
self::setFieldProperties($field, ['resolve' => $fieldResolve ]);
Expand Down
2 changes: 1 addition & 1 deletion src/Ola/Graphql/Tools/ExtendSchema.php → src/Tools/ExtendSchema.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Ola\GraphQL\Tools;
namespace Ola\Tools;

use GraphQL\Error\Error;
use GraphQL\Executor\Values;
Expand Down
121 changes: 83 additions & 38 deletions src/Ola/Graphql/Tools/MergeSchemas.php → src/Tools/MergeSchemas.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Ola\GraphQL\Tools;
namespace Ola\Tools;

use GraphQL\Type\Schema;
use GraphQL\Type\Introspection;
Expand All @@ -21,7 +21,7 @@
use GraphQL\Language\Visitor;
use GraphQL\Language\Parser;

use GraphQL\Utils\Utils;
//use GraphQL\Utils\Utils;
use GraphQL\Utils\BuildSchema;

use GraphQL\Type\Definition\Type;
Expand All @@ -36,9 +36,9 @@
use GraphQL\GraphQL;


use Ola\GraphQL\Tools\ExecutableSchema;
use Ola\GraphQL\Tools\ExtendSchema;
use Ola\GraphQL\Tools\TypeRegistry;
use Ola\Tools\ExecutableSchema;
use Ola\Tools\ExtendSchema;
use Ola\Tools\TypeRegistry;

Class MergeInfo {
private $typeRegistry;
Expand Down Expand Up @@ -409,7 +409,7 @@ public static function mergeSchemas($schemas, $resolvers, $onTypeConflict = null
return $merger->merge($schemas, $resolvers, $onTypeConflict);
}

public function __construct($schemas){
public function __construct($schemas, $resolvers, $onTypeConflict){
if(!is_array($schemas)){
throw new \Exception("Input schemas must be array of \"GraphQL\\Type\\Schema\" or string schema definitions");
}
Expand All @@ -427,7 +427,6 @@ public function merge($schemas, $resolvers, $onTypeConflict = null){
};
}


$queryFields = [];
$mutationFields = [];

Expand All @@ -440,12 +439,12 @@ public function merge($schemas, $resolvers, $onTypeConflict = null){

foreach ($schemas as $key => $schema) {
if ($schema instanceof Schema) {
$actualSchemas[] = $schema;
$actualSchemas[$key] = $schema;
} else if (is_string($schema)) {
$parsedSchemaDocument = Parser::parse($schema);
try {
$actualSchema = BuildSchema::buildAST($parsedSchemaDocument);
$actualSchemas[] = $actualSchema;
$actualSchemas[$key] = $actualSchema;
} catch (\Exception $e) {
// Could not create a schema from parsed string, will use extensions
}
Expand All @@ -457,66 +456,105 @@ public function merge($schemas, $resolvers, $onTypeConflict = null){
}

foreach ($actualSchemas as $key => $schema) {
$typeRegistry->addSchema($schema);
$typeRegistry->addSchema($schema, $key);
$queryType = $schema->getQueryType();
$mutationType = $schema->getMutationType();
foreach ($schema->getTypeMap() as $typeName => $type) {
if (Type::isNamedType($type) && substr(Type::getNamedType($type)->name, 0, 2) !== '__' && $type !== $queryType && $type !== $mutationType) {
if (Type::getNamedType($type) && substr(Type::getNamedType($type)->name, 0, 2) !== '__' && $type !== $queryType && $type !== $mutationType) {
$newType = null;
if (Type::isCompositeType($type) || $type instanceof InputObjectType) {
$newType = $this->recreateCompositeType($schema, $type, $typeRegistry);
} else {
$newType = Type::getNamedType($type);
}


$typeRegistry->addType($newType->name, $newType, $onTypeConflict);
}
};
}

// This is not a bug/oversight, we iterate twice cause we want to first
// resolve all types and then force the type thunks
foreach ($actualSchemas as $key => $schema) {
$queryType = $schema->getQueryType();
$mutationType = $schema->getMutationType();

foreach ($queryType->getFields() as $name => $val) {
if (!$fullResolvers['Query']) {
if (!isset($fullResolvers['Query'])) {
$fullResolvers['Query'] = [];
}
$fullResolvers['Query'][$name] = $this->createDelegatingResolver($mergeInfo, 'query', $name);
$fullResolvers['Query'][$key][$name] = $this->createDelegatingResolver($mergeInfo, 'query', $name);


// var_dump($name);

}

$queryFields = array_merge($queryFields, $this->fieldMapToFieldConfigMap($queryType->getFields(), $typeRegistry));
$queryFields = array_merge(
$queryFields,
[
$key => new ObjectType(
[
'name' => $key.'Queries',
'fields' => $this->fieldMapToFieldConfigMap($queryType->getFields(), $typeRegistry),
]
)
]
);


if ($mutationType) {
if (!$fullResolvers['Mutation']) {
if (!isset($fullResolvers['Mutation'])) {
$fullResolvers['Mutation'] = [];
}
foreach ($mutationType->getFields() as $name => $val) {
$fullResolvers['Mutation'][$name] = $this->createDelegatingResolver($mergeInfo, 'mutation', $name);
$fullResolvers['Mutation'][$key][$name] = $this->createDelegatingResolver($mergeInfo, 'mutation', $name);
}

$mutationFields = array_merge($mutationFields, $this->fieldMapToFieldConfigMap($mutationType->getFields(), $typeRegistry));
if(in_array($key, array_keys($queryFields))) {
$mutations = $queryFields[$key];
} else {

}

$mutationFields = array_merge(
$mutationFields,
[
$key => new ObjectType(
[
'name' => $key.'Mutations',
'fields' => $this->fieldMapToFieldConfigMap($mutationType->getFields(), $typeRegistry),
]
)
]
);
}
}

$passedResolvers = [];

if(is_callable($resolvers)) $passedResolvers = call_user_func($resolvers, $mergeInfo);

if(count($passedResolvers))
foreach ($passedResolvers as $typeName => $type) {
if ($type instanceof ScalarType) {
break;
}
foreach ($type as $fieldName => $field) {
if ($field['fragment']) {
$typeRegistry->addFragment($typeName, $fieldName, $field['fragment']);
if(is_callable($resolvers)) {
$passedResolvers = call_user_func($resolvers, $mergeInfo);
}

if(count($passedResolvers)){
foreach ($passedResolvers as $typeName => $type) {
if ($type instanceof ScalarType) {
break;
}

foreach ($type as $fieldName => $field) {
if (isset($field['fragment'])) {
$typeRegistry->addFragment($typeName, $fieldName, $field['fragment']);
}
};
};
};
}

$fullResolvers = $this->mergeDeep($fullResolvers, $passedResolvers);


// var_dump($fullResolvers['Query']['notification']['AddressFind']);

$query = new ObjectType([
'name' => 'Query',
'fields' => $queryFields
Expand All @@ -529,13 +567,13 @@ public function merge($schemas, $resolvers, $onTypeConflict = null){
'fields' => $mutationFields,
]);
}

$mergedSchema = new Schema([
'query' => $query,
'mutation' => $mutation,
'types' => $typeRegistry->getAllTypes(),
]);

foreach ($extensions as $key => $extension) {
$mergedSchema = ExtendSchema::extend($mergedSchema, $extension);
};
Expand All @@ -545,21 +583,28 @@ public function merge($schemas, $resolvers, $onTypeConflict = null){
return $mergedSchema;
}

private function mergeDeep($target, $source) {
private function mergeDeep($target, $source)
{
$output = $target;
if (is_array($target) && is_array($source)) {
foreach ($source as $key => $src) {
if(is_array($src)){
if(empty($target[$key])){
$output[$key] = $src;
}else{

var_dump(array_keys($target));

$output[$key] = $this->mergeDeep($target[$key], $source[$key]);
}
}else{
$output[$key] = $src;
}
};
} else {
$output[$key] = $src;
}

return $output;
}

Expand All @@ -576,7 +621,7 @@ private function recreateCompositeType($schema, $type, $registry) {
return new ObjectType([
'name' => $type->name,
'description' => $type->description,
'isTypeOf' => $type->isTypeOf,
'isTypeOf' => $type->isTypeOf ?? '',
'fields' => function() use($fields, $registry) {
return $this->fieldMapToFieldConfigMap($fields, $registry);
},
Expand Down Expand Up @@ -632,7 +677,7 @@ private function fieldMapToFieldConfigMap($fields, $registry) {
return $result;
}

private function fieldToFieldConfig(\GraphQL\Type\Definition\FieldDefinition $field, \Ola\GraphQL\Tools\TypeRegistry $registry) {
private function fieldToFieldConfig(\GraphQL\Type\Definition\FieldDefinition $field, \Ola\Tools\TypeRegistry $registry) {
return [
'type' => $registry->resolveType($field->getType()),
'args' => $this->argsToFieldConfigArgumentMap($field->args, $registry),
Expand All @@ -649,7 +694,7 @@ private function argsToFieldConfigArgumentMap($args, $registry) {
return $result;
}

private function argumentToArgumentConfig(\GraphQL\Type\Definition\FieldArgument $argument, \Ola\GraphQL\Tools\TypeRegistry $registry) {
private function argumentToArgumentConfig(\GraphQL\Type\Definition\FieldArgument $argument, \Ola\Tools\TypeRegistry $registry) {
return [
'name' => $argument->name,
'type' => $registry->resolveType($argument->getType()),
Expand Down
2 changes: 1 addition & 1 deletion src/Ola/Graphql/Tools/RemoteSchema.php → src/Tools/RemoteSchema.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Ola\GraphQL\Tools;
namespace Ola\Tools;

use Ola\GraphQL\Tools\BuildClientSchema;
use Ola\GraphQL\Tools\RoutingResolvers;
Expand Down
2 changes: 1 addition & 1 deletion src/Ola/Graphql/Tools/RoutingResolvers.php → src/Tools/RoutingResolvers.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Ola\GraphQL\Tools;
namespace Ola\Tools;

use GraphQL\Language\Printer;
use GraphQL\Utils\SchemaPrinter;
Expand Down
Loading