diff --git a/composer.json b/composer.json index bceaec0..c1e6372 100644 --- a/composer.json +++ b/composer.json @@ -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": [ ], @@ -13,7 +13,7 @@ }, "autoload": { "psr-4": { - "Ola\\": "src/Ola" + "Ola\\": "src/" } } } diff --git a/src/Ola/Graphql/Tools/BuildClientSchema.php b/src/Tools/BuildClientSchema.php old mode 100644 new mode 100755 similarity index 97% rename from src/Ola/Graphql/Tools/BuildClientSchema.php rename to src/Tools/BuildClientSchema.php index 3211e7e..14c363b --- a/src/Ola/Graphql/Tools/BuildClientSchema.php +++ b/src/Tools/BuildClientSchema.php @@ -1,9 +1,10 @@ typeDefCache[$typeName]) { + if (isset($this->typeDefCache[$typeName])) { return $this->typeDefCache[$typeName]; } $typeIntrospection = $this->typeIntrospectionMap[$typeName]; @@ -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]; @@ -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), ]); } diff --git a/src/Ola/Graphql/Tools/ExecutableSchema.php b/src/Tools/ExecutableSchema.php old mode 100644 new mode 100755 similarity index 98% rename from src/Ola/Graphql/Tools/ExecutableSchema.php rename to src/Tools/ExecutableSchema.php index df3d314..859fc24 --- a/src/Ola/Graphql/Tools/ExecutableSchema.php +++ b/src/Tools/ExecutableSchema.php @@ -1,5 +1,5 @@ $resolver) { + $type = $schema->getType($typeName); if (!$type && $typeName !== '__schema') { throw new SchemaError("\"$typeName\" defined in resolvers, but not in schema"); @@ -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 ]); diff --git a/src/Ola/Graphql/Tools/ExtendSchema.php b/src/Tools/ExtendSchema.php old mode 100644 new mode 100755 similarity index 99% rename from src/Ola/Graphql/Tools/ExtendSchema.php rename to src/Tools/ExtendSchema.php index 6ac6738..b574c83 --- a/src/Ola/Graphql/Tools/ExtendSchema.php +++ b/src/Tools/ExtendSchema.php @@ -1,5 +1,5 @@ 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"); } @@ -427,7 +427,6 @@ public function merge($schemas, $resolvers, $onTypeConflict = null){ }; } - $queryFields = []; $mutationFields = []; @@ -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 } @@ -457,22 +456,24 @@ 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) { @@ -480,43 +481,80 @@ public function merge($schemas, $resolvers, $onTypeConflict = null){ $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 @@ -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); }; @@ -545,7 +583,8 @@ 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) { @@ -553,13 +592,19 @@ private function mergeDeep($target, $source) { 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; } @@ -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); }, @@ -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), @@ -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()), diff --git a/src/Ola/Graphql/Tools/RemoteSchema.php b/src/Tools/RemoteSchema.php old mode 100644 new mode 100755 similarity index 98% rename from src/Ola/Graphql/Tools/RemoteSchema.php rename to src/Tools/RemoteSchema.php index ddbf8cf..8276de6 --- a/src/Ola/Graphql/Tools/RemoteSchema.php +++ b/src/Tools/RemoteSchema.php @@ -1,5 +1,5 @@ resolveType($type->getWrappedType(true))); } else if ($type instanceof NonNull) { return Type::nonNull($this->resolveType($type->getWrappedType(true))); - } else if (Type::isNamedType($type)) { + } else if (Type::getNamedType($type)) { return $this->getType(Type::getNamedType($type)->name); } else { return $type; } } - public function addSchema($schema) { + public function addSchema($schema, $service = null) { $query = $schema->getQueryType(); if ($query) { $fieldNames = array_keys($query->getFields()); foreach ($fieldNames as $fieldName) { - $this->schemaByField['query'][$fieldName] = $schema; + $this->schemaByField['query'][$service][$fieldName] = $schema; } } $mutation = $schema->getMutationType(); if ($mutation) { $fieldNames = array_keys($mutation->getFields()); foreach ($fieldNames as $fieldName) { - $this->schemaByField['mutation'][$fieldName] = $schema; + $this->schemaByField['mutation'][$service][$fieldName] = $schema; } } } public function addType($name, $type, $onTypeConflict=null) { - if ($this->types[$name]) { + if (isset($this->types[$name])) { if (!empty($onTypeConflict)) { $type = call_user_func($onTypeConflict, $this->types[$name], $type); } else { diff --git a/src/Ola/Graphql/Tools/Utils-do-not-push-yet.php b/src/Tools/Utils.php old mode 100644 new mode 100755 similarity index 96% rename from src/Ola/Graphql/Tools/Utils-do-not-push-yet.php rename to src/Tools/Utils.php index 2f12f30..c84b530 --- a/src/Ola/Graphql/Tools/Utils-do-not-push-yet.php +++ b/src/Tools/Utils.php @@ -1,9 +1,9 @@