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

Commit

Permalink
Add support for parsing schema definition
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 committed Feb 25, 2018
1 parent 4148f43 commit 92a74af
Show file tree
Hide file tree
Showing 27 changed files with 1,278 additions and 198 deletions.
2 changes: 1 addition & 1 deletion src/Language/AST/Builder/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function createLocation(array $ast): Location
* @param null $defaultValue
* @return mixed|null
*/
protected function getOne(array $ast, string $propertyName, $defaultValue = null)
protected function get(array $ast, string $propertyName, $defaultValue = null)
{
return $ast[$propertyName] ?? $defaultValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/Builder/BooleanBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BooleanBuilder extends AbstractBuilder
public function build(array $ast): NodeInterface
{
return new BooleanValueNode([
'value' => $this->getOne($ast, 'value'),
'value' => $this->get($ast, 'value'),
'location' => $this->createLocation($ast),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/Builder/EnumBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EnumBuilder extends AbstractBuilder
public function build(array $ast): NodeInterface
{
return new EnumValueNode([
'value' => $this->getOne($ast, 'value'),
'value' => $this->get($ast, 'value'),
'location' => $this->createLocation($ast),
]);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Language/AST/Builder/FieldDefinitionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Digia\GraphQL\Language\AST\Builder;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;
use Digia\GraphQL\Language\AST\Node\FieldDefinitionNode;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class FieldDefinitionBuilder extends AbstractBuilder
{

/**
* @inheritdoc
*/
public function build(array $ast): NodeInterface
{
return new FieldDefinitionNode([
'description' => $this->buildOne($ast, 'description'),
'name' => $this->buildOne($ast, 'name'),
'arguments' => $this->buildMany($ast, 'arguments'),
'type' => $this->buildOne($ast, 'type'),
'directives' => $this->buildMany($ast, 'directives'),
'location' => $this->createLocation($ast),
]);
}

/**
* @inheritdoc
*/
public function supportsKind(string $kind): bool
{
return $kind === NodeKindEnum::FIELD_DEFINITION;
}
}
2 changes: 1 addition & 1 deletion src/Language/AST/Builder/FloatBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FloatBuilder extends AbstractBuilder
public function build(array $ast): NodeInterface
{
return new FloatValueNode([
'value' => $this->getOne($ast, 'value'),
'value' => $this->get($ast, 'value'),
'location' => $this->createLocation($ast),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/Builder/IntBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class IntBuilder extends AbstractBuilder
public function build(array $ast): NodeInterface
{
return new IntValueNode([
'value' => $this->getOne($ast, 'value'),
'value' => $this->get($ast, 'value'),
'location' => $this->createLocation($ast),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/Builder/NameBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NameBuilder extends AbstractBuilder
public function build(array $ast): NodeInterface
{
return new NameNode([
'value' => $this->getOne($ast, 'value'),
'value' => $this->get($ast, 'value'),
'location' => $this->createLocation($ast),
]);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Language/AST/Builder/ObjectTypeDefinitionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Digia\GraphQL\Language\AST\Builder;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;
use Digia\GraphQL\Language\AST\Node\ObjectTypeDefinitionNode;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class ObjectTypeDefinitionBuilder extends AbstractBuilder
{

/**
* @inheritdoc
*/
public function build(array $ast): NodeInterface
{
return new ObjectTypeDefinitionNode([
'description' => $this->buildOne($ast, 'description'),
'name' => $this->buildOne($ast, 'name'),
'interfaces' => $this->buildMany($ast, 'interfaces'),
'directives' => $this->buildMany($ast, 'directives'),
'fields' => $this->buildMany($ast, 'fields'),
'location' => $this->createLocation($ast),
]);
}

/**
* @inheritdoc
*/
public function supportsKind(string $kind): bool
{
return $kind === NodeKindEnum::OBJECT_TYPE_DEFINITION;
}
}
2 changes: 1 addition & 1 deletion src/Language/AST/Builder/OperationDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OperationDefinitionBuilder extends AbstractBuilder
public function build(array $ast): NodeInterface
{
return new OperationDefinitionNode([
'operation' => $this->getOne($ast, 'operation'),
'operation' => $this->get($ast, 'operation'),
'variableDefinitions' => $this->buildMany($ast, 'variableDefinitions'),
'directives' => $this->buildMany($ast, 'directives'),
'selectionSet' => $this->buildOne($ast, 'selectionSet'),
Expand Down
4 changes: 2 additions & 2 deletions src/Language/AST/Builder/StringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class StringBuilder extends AbstractBuilder
public function build(array $ast): NodeInterface
{
return new StringValueNode([
'value' => $this->getOne($ast, 'value'),
'block' => $this->getOne($ast, 'block', false),
'value' => $this->get($ast, 'value'),
'block' => $this->get($ast, 'block', false),
'location' => $this->createLocation($ast),
]);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Language/AST/DirectiveLocationEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ class DirectiveLocationEnum
const ENUM_VALUE = 'ENUM_VALUE';
const INPUT_OBJECT = 'INPUT_OBJECT';
const INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION';

/**
* @return array
* @throws \ReflectionException
*/
public static function values(): array
{
return array_values((new \ReflectionClass(__CLASS__))->getConstants());
}
}
12 changes: 10 additions & 2 deletions src/Language/AST/Node/Behavior/DescriptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ trait DescriptionTrait
{

/**
* @var ?StringValueNode
* @var StringValueNode|null
*/
protected $description;

/**
* @return StringValueNode
* @return StringValueNode|null
*/
public function getDescription(): ?StringValueNode
{
return $this->description;
}

/**
* @return array|null
*/
public function getDescriptionAsArray(): ?array
{
return null !== $this->description ? $this->description->toArray() : null;
}
}
11 changes: 11 additions & 0 deletions src/Language/AST/Node/Behavior/FieldsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Digia\GraphQL\Language\AST\Node\Behavior;

use Digia\GraphQL\Contract\SerializationInterface;
use Digia\GraphQL\Language\AST\Node\FieldDefinitionNode;

trait FieldsTrait
Expand All @@ -19,4 +20,14 @@ public function getFields(): array
{
return $this->fields;
}

/**
* @return array
*/
public function getFieldsAsArray(): array
{
return array_map(function (SerializationInterface $node) {
return $node->toArray();
}, $this->fields);
}
}
9 changes: 9 additions & 0 deletions src/Language/AST/Node/Behavior/InterfacesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public function getInterfaces(): array
{
return $this->interfaces;
}

/**
* @return array
*/
public function getInterfacesAsArray(): array
{
// TODO: Implement this method.
return [];
}
}
2 changes: 1 addition & 1 deletion src/Language/AST/Node/DocumentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function toArray(): array
{
return [
'kind' => $this->kind,
'loc' => $this->getLocationAsArray(),
'definitions' => $this->getDefinitionsAsArray(),
'loc' => $this->getLocationAsArray(),
];
}
}
23 changes: 14 additions & 9 deletions src/Language/AST/Node/FieldDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

namespace Digia\GraphQL\Language\AST\Node;

use Digia\GraphQL\Language\AST\NodeKindEnum;
use Digia\GraphQL\Language\AST\Node\Behavior\ArgumentsTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\DescriptionTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\DirectivesTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\NameTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\TypeTrait;
use Digia\GraphQL\Language\AST\Node\Contract\DefinitionNodeInterface;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class FieldDefinitionNode extends AbstractNode implements DefinitionNodeInterface
{

use DescriptionTrait;
use NameTrait;
use ArgumentsTrait;
use TypeTrait;
use DirectivesTrait;

Expand All @@ -23,15 +25,18 @@ class FieldDefinitionNode extends AbstractNode implements DefinitionNodeInterfac
protected $kind = NodeKindEnum::FIELD_DEFINITION;

/**
* @var InputValueDefinitionNode[]
*/
protected $arguments;

/**
* @return InputValueDefinitionNode[]
* @inheritdoc
*/
public function getArguments(): array
public function toArray(): array
{
return $this->arguments;
return [
'kind' => $this->kind,
'description' => $this->description,
'name' => $this->getNameAsArray(),
'arguments' => $this->getArgumentsAsArray(),
'type' => $this->getTypeAsArray(),
'directives' => $this->getDirectivesAsArray(),
'loc' => $this->getLocationAsArray(),
];
}
}
2 changes: 1 addition & 1 deletion src/Language/AST/Node/NameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function toArray(): array
{
return [
'kind' => $this->kind,
'loc' => $this->getLocationAsArray(),
'value' => $this->value,
'loc' => $this->getLocationAsArray(),
];
}
}
2 changes: 1 addition & 1 deletion src/Language/AST/Node/NamedTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function toArray(): array
{
return [
'kind' => $this->kind,
'loc' => $this->getLocationAsArray(),
'name' => $this->getNameAsArray(),
'loc' => $this->getLocationAsArray(),
];
}
}
19 changes: 18 additions & 1 deletion src/Language/AST/Node/ObjectTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Digia\GraphQL\Language\AST\Node;

use Digia\GraphQL\Language\AST\NodeKindEnum;
use Digia\GraphQL\Language\AST\Node\Behavior\DescriptionTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\DirectivesTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\FieldsTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\InterfacesTrait;
use Digia\GraphQL\Language\AST\Node\Behavior\NameTrait;
use Digia\GraphQL\Language\AST\Node\Contract\TypeDefinitionNodeInterface;
use Digia\GraphQL\Language\AST\NodeKindEnum;

class ObjectTypeDefinitionNode extends AbstractNode implements TypeDefinitionNodeInterface
{
Expand All @@ -23,4 +23,21 @@ class ObjectTypeDefinitionNode extends AbstractNode implements TypeDefinitionNod
* @var string
*/
protected $kind = NodeKindEnum::OBJECT_TYPE_DEFINITION;

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
'kind' => $this->kind,
'description' => $this->getDescriptionAsArray(),
'name' => $this->getNameAsArray(),
'interfaces' => $this->getInterfacesAsArray(),
'directives' => $this->getDirectivesAsArray(),
'fields' => $this->getFieldsAsArray(),
'loc' => $this->getLocationAsArray(),
];
}

}

0 comments on commit 92a74af

Please sign in to comment.