Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Move Schema class
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 committed Feb 26, 2018
1 parent 5d10252 commit 4a04fad
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Execution/Execution.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Digia\GraphQL\Language\AST\Node\SelectionSetNode;
use Digia\GraphQL\Language\AST\NodeKindEnum;
use Digia\GraphQL\Type\Definition\ObjectType;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;

/**
* Class Execution
Expand Down
2 changes: 1 addition & 1 deletion src/Execution/ExecutionArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Digia\GraphQL\Language\AST\Node\DocumentNode;
use Digia\GraphQL\Language\AST\Node\OperationDefinitionNode;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;

class ExecutionArguments
{
Expand Down
2 changes: 1 addition & 1 deletion src/Execution/ExecutionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Digia\GraphQL\Error\GraphQLError;
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode;
use Digia\GraphQL\Language\AST\Node\OperationDefinitionNode;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;

class ExecutionContext
{
Expand Down
60 changes: 60 additions & 0 deletions src/Type/Contract/SchemaInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Digia\GraphQL\Type\Contract;

use Digia\GraphQL\Type\Definition\Contract\AbstractTypeInterface;
use Digia\GraphQL\Type\Definition\Contract\NamedTypeInterface;
use Digia\GraphQL\Type\Definition\Contract\TypeInterface;
use Digia\GraphQL\Type\Definition\ObjectType;

interface SchemaInterface
{
/**
* @return ObjectType
*/
public function getQuery(): ObjectType;

/**
* @return ObjectType
*/
public function getMutation(): ObjectType;

/**
* @return ObjectType
*/
public function getSubscription(): ObjectType;

/**
* @return array
*/
public function getDirectives(): array;

/**
* @return NamedTypeInterface[]
*/
public function getTypeMap(): array;

/**
* @return bool
*/
public function getAssumeValid(): bool;

/**
* @param AbstractTypeInterface $abstractType
* @param TypeInterface $possibleType
* @return bool
*/
public function isPossibleType(AbstractTypeInterface $abstractType, TypeInterface $possibleType): bool;

/**
* @param AbstractTypeInterface $abstractType
* @return null|TypeInterface[]
*/
public function getPossibleTypes(AbstractTypeInterface $abstractType): ?array;

/**
* @param string $name
* @return TypeInterface|null
*/
public function getType(string $name): ?TypeInterface;
}
89 changes: 51 additions & 38 deletions src/Type/Schema/Schema.php → src/Type/Schema.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

namespace Digia\GraphQL\Type\Schema;
namespace Digia\GraphQL\Type;

use Digia\GraphQL\ConfigObject;
use Digia\GraphQL\Language\AST\Node\NodeTrait;
use Digia\GraphQL\Language\AST\Node\SchemaDefinitionNode;
use Digia\GraphQL\ConfigObject;
use Digia\GraphQL\Type\Contract\SchemaInterface;
use Digia\GraphQL\Type\Definition\Argument;
use Digia\GraphQL\Type\Definition\Contract\AbstractTypeInterface;
use Digia\GraphQL\Type\Definition\Contract\NamedTypeInterface;
use Digia\GraphQL\Type\Definition\Contract\DirectiveInterface;
use Digia\GraphQL\Type\Definition\Contract\TypeInterface;
use Digia\GraphQL\Type\Definition\Contract\WrappingTypeInterface;
use Digia\GraphQL\Type\Definition\InputObjectType;
use Digia\GraphQL\Type\Definition\InterfaceType;
use Digia\GraphQL\Type\Definition\ObjectType;
use Digia\GraphQL\Type\Definition\UnionType;
use Digia\GraphQL\Type\Definition\Contract\DirectiveInterface;
use function Digia\GraphQL\Util\invariant;

/**
Expand Down Expand Up @@ -44,7 +44,7 @@
* @package Digia\GraphQL\Type
* @property SchemaDefinitionNode $astNode
*/
class Schema extends ConfigObject
class Schema extends ConfigObject implements SchemaInterface
{

use NodeTrait;
Expand Down Expand Up @@ -95,66 +95,55 @@ class Schema extends ConfigObject
private $_possibleTypeMap = [];

/**
* @return ObjectType
* @inheritdoc
*/
public function getQuery(): ObjectType
{
return $this->query;
}

/**
* @return ObjectType
* @inheritdoc
*/
public function getMutation(): ObjectType
{
return $this->mutation;
}

/**
* @return ObjectType
* @inheritdoc
*/
public function getSubscription(): ObjectType
{
return $this->subscription;
}

/**
* @return NamedTypeInterface[]
*/
public function getTypeMap(): array
{
return $this->_typeMap;
}

/**
* @return bool
* @inheritdoc
*/
public function getAssumeValid(): bool
public function getDirectives(): array
{
return $this->assumeValid;
return $this->directives;
}

/**
* @inheritdoc
*/
protected function beforeConfig(): void
public function getTypeMap(): array
{
$this->setDirectives(specifiedDirectives());
return $this->_typeMap;
}

/**
* @throws \Exception
* @inheritdoc
*/
protected function afterConfig(): void
public function getAssumeValid(): bool
{
$this->buildTypeMap();
$this->buildImplementations();
return $this->assumeValid;
}

/**
* @param AbstractTypeInterface $abstractType
* @param TypeInterface $possibleType
* @return bool
* @inheritdoc
* @throws \Exception
*/
public function isPossibleType(AbstractTypeInterface $abstractType, TypeInterface $possibleType): bool
Expand Down Expand Up @@ -185,11 +174,10 @@ public function isPossibleType(AbstractTypeInterface $abstractType, TypeInterfac
}

/**
* @param AbstractTypeInterface $abstractType
* @return null|TypeInterface[]
* @inheritdoc
* @throws \Exception
*/
protected function getPossibleTypes(AbstractTypeInterface $abstractType): ?array
public function getPossibleTypes(AbstractTypeInterface $abstractType): ?array
{
if ($abstractType instanceof UnionType) {
return $abstractType->getTypes();
Expand All @@ -198,6 +186,31 @@ protected function getPossibleTypes(AbstractTypeInterface $abstractType): ?array
return $this->_implementations[$abstractType->getName()] ?? null;
}

/**
* @inheritdoc
*/
public function getType(string $name): ?TypeInterface
{
return $this->_typeMap[$name] ?? null;
}

/**
* @inheritdoc
*/
protected function beforeConfig(): void
{
$this->setDirectives(specifiedDirectives());
}

/**
* @throws \Exception
*/
protected function afterConfig(): void
{
$this->buildTypeMap();
$this->buildImplementations();
}

/**
*
*/
Expand All @@ -217,10 +230,10 @@ protected function buildTypeMap(): void
$typeMap = [];

// First by deeply visiting all initial types.
$typeMap = array_reduce($initialTypes, 'Digia\GraphQL\Type\Schema\typeMapReducer', $typeMap);
$typeMap = array_reduce($initialTypes, 'Digia\GraphQL\Type\typeMapReducer', $typeMap);

// Then by deeply visiting all directive types.
$typeMap = array_reduce($this->directives, 'Digia\GraphQL\Type\Schema\typeMapDirectiveReducer', $typeMap);
$typeMap = array_reduce($this->directives, 'Digia\GraphQL\Type\typeMapDirectiveReducer', $typeMap);

// Storing the resulting map for reference by the schema.
$this->_typeMap = $typeMap;
Expand All @@ -232,7 +245,7 @@ protected function buildTypeMap(): void
protected function buildImplementations()
{
$implementations = [];

// Keep track of all implementations by interface name.
foreach ($this->_typeMap as $typeName => $type) {
if ($type instanceof ObjectType) {
Expand Down Expand Up @@ -288,7 +301,7 @@ protected function setSubscription(ObjectType $subscription): Schema
* @param array $types
* @return Schema
*/
public function setTypes(array $types): Schema
protected function setTypes(array $types): Schema
{
$this->types = $types;

Expand Down Expand Up @@ -353,11 +366,11 @@ function typeMapReducer(array $map, ?TypeInterface $type): array
$reducedMap = $map;

if ($type instanceof UnionType) {
$reducedMap = array_reduce($type->getTypes(), 'Digia\GraphQL\Type\Schema\typeMapReducer', $reducedMap);
$reducedMap = array_reduce($type->getTypes(), 'Digia\GraphQL\Type\typeMapReducer', $reducedMap);
}

if ($type instanceof ObjectType) {
$reducedMap = array_reduce($type->getInterfaces(), 'Digia\GraphQL\Type\Schema\typeMapReducer', $reducedMap);
$reducedMap = array_reduce($type->getInterfaces(), 'Digia\GraphQL\Type\typeMapReducer', $reducedMap);
}

if ($type instanceof ObjectType || $type instanceof InterfaceType) {
Expand All @@ -367,7 +380,7 @@ function typeMapReducer(array $map, ?TypeInterface $type): array
return $argument->getType();
}, $field->getArgs());

$reducedMap = array_reduce($fieldArgTypes, 'Digia\GraphQL\Type\Schema\typeMapReducer', $reducedMap);
$reducedMap = array_reduce($fieldArgTypes, 'Digia\GraphQL\Type\typeMapReducer', $reducedMap);
}

$reducedMap = typeMapReducer($reducedMap, $field->getType());
Expand Down
2 changes: 1 addition & 1 deletion src/Type/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Digia\GraphQL\Type\Definition\UnionType;
use Digia\GraphQL\Type\Definition\Directive;
use Digia\GraphQL\Type\Definition\Contract\DirectiveInterface;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;
use function Digia\GraphQL\Util\invariant;
use function Digia\GraphQL\Util\toString;

Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Excecution/ExecutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Digia\GraphQL\Language\SourceLocation;
use Digia\GraphQL\Test\TestCase;
use Digia\GraphQL\Type\Definition\ObjectType;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;
use function Digia\GraphQL\Type\GraphQLInt;
use function Digia\GraphQL\Type\GraphQLList;
use function Digia\GraphQL\Type\GraphQLString;
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Type/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Digia\GraphQL\Type\Definition\ScalarType;
use Digia\GraphQL\Type\Definition\TypeNameEnum;
use Digia\GraphQL\Type\Definition\UnionType;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;

class DefinitionTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Type/EnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use function Digia\GraphQL\Type\GraphQLObjectType;
use function Digia\GraphQL\Type\GraphQLSchema;
use function Digia\GraphQL\Type\GraphQLString;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;

class EnumTypeTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Type/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function Digia\GraphQL\Type\GraphQLObjectType;
use function Digia\GraphQL\Type\GraphQLSchema;
use function Digia\GraphQL\Type\GraphQLString;
use Digia\GraphQL\Type\Schema\Schema;
use Digia\GraphQL\Type\Schema;

class SchemaTest extends TestCase
{
Expand Down

0 comments on commit 4a04fad

Please sign in to comment.