From 0028fae9b3bffe50a16716968551696828421a94 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Wed, 7 Feb 2018 10:59:23 +0100 Subject: [PATCH] Impose explicit declaration of non detected types --- Definition/Builder/SchemaBuilder.php | 11 ++-- DependencyInjection/Configuration.php | 4 ++ .../OverblogGraphQLExtension.php | 1 + Resources/doc/definitions/relay/node/node.md | 14 +++++ Resources/doc/definitions/schema.md | 4 ++ Tests/Functional/App/config/node/config.yml | 1 + UPGRADE-0.11.md | 58 ++++++++++++++++++- 7 files changed, 87 insertions(+), 6 deletions(-) diff --git a/Definition/Builder/SchemaBuilder.php b/Definition/Builder/SchemaBuilder.php index e81377aba..96e959008 100644 --- a/Definition/Builder/SchemaBuilder.php +++ b/Definition/Builder/SchemaBuilder.php @@ -32,16 +32,17 @@ public function __construct(TypeResolver $typeResolver, SchemaDecorator $decorat * @param null|string $mutationAlias * @param null|string $subscriptionAlias * @param ResolverMapInterface[] $resolverMaps + * @param string[] $types * * @return Schema */ - public function create($queryAlias = null, $mutationAlias = null, $subscriptionAlias = null, array $resolverMaps = []) + public function create($queryAlias = null, $mutationAlias = null, $subscriptionAlias = null, array $resolverMaps = [], array $types = []) { $query = $this->typeResolver->resolve($queryAlias); $mutation = $this->typeResolver->resolve($mutationAlias); $subscription = $this->typeResolver->resolve($subscriptionAlias); - $schema = new Schema($this->buildSchemaArguments($query, $mutation, $subscription)); + $schema = new Schema($this->buildSchemaArguments($query, $mutation, $subscription, $types)); reset($resolverMaps); $this->decorator->decorate($schema, 1 === count($resolverMaps) ? current($resolverMaps) : new ResolverMaps($resolverMaps)); @@ -52,7 +53,7 @@ public function create($queryAlias = null, $mutationAlias = null, $subscriptionA return $schema; } - private function buildSchemaArguments(Type $query = null, Type $mutation = null, Type $subscription = null) + private function buildSchemaArguments(Type $query = null, Type $mutation = null, Type $subscription = null, array $types = []) { return [ 'query' => $query, @@ -61,8 +62,8 @@ private function buildSchemaArguments(Type $query = null, Type $mutation = null, 'typeLoader' => function ($name) { return $this->typeResolver->resolve($name); }, - 'types' => function () { - return $this->typeResolver->getSolutions(); + 'types' => function () use ($types) { + return array_map([$this->typeResolver, 'getSolution'], $types); }, ]; } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 847281600..d0f25b632 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -200,6 +200,10 @@ private function definitionsSchemaSection() ->defaultValue([]) ->prototype('scalar')->end() ->end() + ->arrayNode('types') + ->defaultValue([]) + ->prototype('scalar')->end() + ->end() ->end() ->end() ->end(); diff --git a/DependencyInjection/OverblogGraphQLExtension.php b/DependencyInjection/OverblogGraphQLExtension.php index 822614f28..8e968693f 100644 --- a/DependencyInjection/OverblogGraphQLExtension.php +++ b/DependencyInjection/OverblogGraphQLExtension.php @@ -235,6 +235,7 @@ private function setSchemaArguments(array $config, ContainerBuilder $container) array_map(function ($id) { return new Reference($id); }, $schemaConfig['resolver_maps']), + $schemaConfig['types'], ]); $definition->setPublic(false); $container->setDefinition($schemaID, $definition); diff --git a/Resources/doc/definitions/relay/node/node.md b/Resources/doc/definitions/relay/node/node.md index 67e82671e..40dba4d8f 100644 --- a/Resources/doc/definitions/relay/node/node.md +++ b/Resources/doc/definitions/relay/node/node.md @@ -37,3 +37,17 @@ User: type: String interfaces: [Node] ``` + +In above example `Photo` and `User` can't be detected by graphql-php during +static schema analysis. That the reason why their should be explicitly declare +in schema definition: + +```yaml +overblog_graphql: + definitions: + schema: + query: Query + mutation: ~ + # here how this can be done + types: [User, Photo] +``` diff --git a/Resources/doc/definitions/schema.md b/Resources/doc/definitions/schema.md index 1413b3105..9c5c68c3a 100644 --- a/Resources/doc/definitions/schema.md +++ b/Resources/doc/definitions/schema.md @@ -69,6 +69,10 @@ overblog_graphql: schema: query: Query mutation: ~ + # the name of extra types that can not be detected + # by graphql-php during static schema analysis. + # These types names should be explicitly declare here + types: [] ``` ## Batching diff --git a/Tests/Functional/App/config/node/config.yml b/Tests/Functional/App/config/node/config.yml index e65d58ef1..f4c1ac422 100644 --- a/Tests/Functional/App/config/node/config.yml +++ b/Tests/Functional/App/config/node/config.yml @@ -17,6 +17,7 @@ overblog_graphql: schema: query: Query mutation: ~ + types: [User, Photo] mappings: auto_discover: true types: diff --git a/UPGRADE-0.11.md b/UPGRADE-0.11.md index a329e0475..197da4934 100644 --- a/UPGRADE-0.11.md +++ b/UPGRADE-0.11.md @@ -8,6 +8,7 @@ UPGRADE FROM 0.10 to 0.11 - [Promise adapter interface](#promise-adapter-interface) - [Expression language](#expression-language) - [Type autoMapping and Symfony DI autoconfigure](#type-automapping-and-symfony-di-autoconfigure) +- [Explicitly declare non detected types](#explicitly-declare-non-detected-types) ### GraphiQL @@ -166,7 +167,7 @@ UPGRADE FROM 0.10 to 0.11 class DateTimeType extends ScalarType implements AliasedInterface { - public $name = 'DateTme'; + public $name = 'DateTime'; /** * {@inheritdoc} @@ -178,3 +179,58 @@ UPGRADE FROM 0.10 to 0.11 // ... } ``` + +### Explicitly declare non detected types + + **Before 0.11** all types was declare as non detected types, this was not the correct way of declaring types. + This could lead to some performances issues or/and wrong types public exposition (in introspection query). + [See webonyx/graphql-php documentations for more details](http://webonyx.github.io/graphql-php/type-system/schema/#configuration-options) + + **Since 0.11** Non detect types should be explicitly declare + + here a concrete example: + ```yaml + Query: + type: object + config: + fields: + foo: {type: FooInterface!} + + FooInterface: + type: interface + config: + fields: + id: {type: ID!} + resolveType: '@=resolver("foo", [value])' + + Bar: + type: object + config: + fields: + id: {type: ID!} + # ... + interfaces: [FooInterface] + + Baz: + type: object + config: + fields: + id: {type: ID!} + # ... + interfaces: [FooInterface] + ``` + In above example `Baz` an `Bar` can not be detected by graphql-php during static schema analysis, + an `GraphQL\Error\InvariantViolation` exception will be throw with the following message: + ```text + Could not find possible implementing types for FooInterface in schema. + Check that schema.types is defined and is an array of all possible types in the schema. + ``` + here how this can be fix: + + ```yaml + overblog_graphql: + definitions: + schema: + query: Query + types: [Bar, Baz] + ```