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

Commit

Permalink
Merge f200ba5 into b9da506
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 committed Feb 26, 2018
2 parents b9da506 + f200ba5 commit 73af766
Show file tree
Hide file tree
Showing 16 changed files with 846 additions and 48 deletions.
91 changes: 91 additions & 0 deletions Resources/introspection.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}

fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}

fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}

fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"./src/Type/definition.php",
"./src/Type/directives.php",
"./src/Type/scalars.php",
"./src/Util/helpers.php"
"./src/Util/helpers.php",
"./src/graphql.php"
],
"psr-4": {
"Digia\\GraphQL\\": "./src"
Expand All @@ -37,7 +38,8 @@
"./src/Type/definition.php",
"./src/Type/directives.php",
"./src/Type/scalars.php",
"./src/Util/helpers.php"
"./src/Util/helpers.php",
"./src/graphql.php"
],
"psr-4": {
"Digia\\GraphQL\\Test\\": "./tests"
Expand Down
62 changes: 62 additions & 0 deletions src/Error/GraphQLError.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Digia\GraphQL\Error;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;
use Digia\GraphQL\Language\Source;

/**
* A GraphQLError describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
Expand All @@ -10,4 +13,63 @@
*/
class GraphQLError extends \Exception
{

/**
* @var string[]
*/
protected $locations;

/**
* @var string[]
*/
protected $path;

/**
* @var NodeInterface[]
*/
protected $nodes;

/**
* @var Source|null
*/
protected $source;

/**
* @var int[]
*/
protected $positions;

/**
* @var array
*/
protected $extensions;

/**
* GraphQLError constructor.
*
* @param string $message
* @param array|null $nodes
* @param Source|null $source
* @param array|null $positions
* @param array|null $path
* @param array|null $extensions
*/
public function __construct(
string $message,
?array $nodes = null,
?Source $source = null,
?array $positions = null,
?array $path = null,
?array $extensions = null
) {
parent::__construct($message);

$this->nodes = $nodes;
$this->source = $source;
$this->positions = $positions;
$this->path = $path;
$this->extensions = $extensions;
}

// TODO: Implement the rest of this class.
}
20 changes: 10 additions & 10 deletions src/Execution/Execution.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ private static function buildExecutionContext(
'Must provide operation name if query contains multiple operations.'
);
}
if (!$operationName || (!empty($definition->getName()) && $definition->getName()->getValue() === $operationName)) {
if (!$operationName || $definition->getNameValue() === $operationName) {
$operation = $definition;
}
break;
case NodeKindEnum::FRAGMENT_DEFINITION:
$fragments[$definition->getName()->getValue()] = $definition;
$fragments[$definition->getNameValue()] = $definition;
break;
default:
throw new GraphQLError(
"GraphQL cannot execute a request containing a {$definition->getKind()}.",
[$definition]
);
// default:
// throw new GraphQLError(
// "GraphQL cannot execute a request containing a {$definition->getKind()}.",
// [$definition]
// );
}
}

Expand Down Expand Up @@ -162,7 +162,7 @@ private function executeOperation(
$fields = $this->collectFields($query, $operation->getSelectionSet(), [], []);
$path = [];

if ($context->getOperation()->getName()->getValue() === 'query') {
if ($context->getOperation()->getOperation() === 'query') {
$data = $this->executeFields($query, $rootValue, $path, $fields);

return new ExecutionResult($data, []);
Expand All @@ -182,7 +182,7 @@ private function collectFields(
switch ($selection->getKind()) {
case NodeKindEnum::FIELD:
/** @var FieldNode $selection */
$name = $selection->getName()->getValue();
$name = $selection->getNameValue();
$fields[$name][] = $selection;
break;
}
Expand Down Expand Up @@ -229,7 +229,7 @@ private function resolveField(ObjectType $parentType, $source, $fieldNodes, $pat
/** @var FieldNode $fieldNode */
$fieldNode = $fieldNodes[0];

$field = $parentType->getFields()[$fieldNode->getName()->getValue()];
$field = $parentType->getFields()[$fieldNode->getNameValue()];

$inputValues = $fieldNode->getArguments() ?? [];

Expand Down
20 changes: 17 additions & 3 deletions src/Execution/ExecutionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Digia\GraphQL\Execution;

use Digia\GraphQL\Contract\SerializationInterface;
use Digia\GraphQL\Error\GraphQLError;

class ExecutionResult
class ExecutionResult implements SerializationInterface
{

/**
* @var GraphQLError[]
*/
Expand All @@ -18,10 +20,11 @@ class ExecutionResult

/**
* ExecutionResult constructor.
* @param mixed[] $data
*
* @param mixed[] $data
* @param GraphQLError[] $errors
*/
public function __construct(array $data, array $errors)
public function __construct(?array $data, ?array $errors)
{
$this->errors = $errors;
$this->data = $data;
Expand All @@ -36,4 +39,15 @@ public function addError(GraphQLError $error): ExecutionResult
$this->errors[] = $error;
return $this;
}

/**
* @inheritdoc
*/
public function toArray(): array
{
return [
'data' => $this->data,
'errors' => $this->errors,
];
}
}
6 changes: 3 additions & 3 deletions src/Language/AST/Node/Behavior/NameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function getName(): ?NameNode
}

/**
* @return string
* @return string|null
*/
public function getNameValue(): string
public function getNameValue(): ?string
{
return $this->name->getValue();
return null !== $this->name ? $this->name->getValue() : null;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Language/AST/Node/Contract/DefinitionNodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ function getKind(): string;
* @return NameNode|null
*/
function getName(): ?NameNode;

/**
* @return string|null
*/
function getNameValue(): ?string;
}
25 changes: 0 additions & 25 deletions src/Type/Definition/Behavior/FieldsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,6 @@ trait FieldsTrait
*/
private $_isFieldMapDefined = false;

/**
* @param Field $field
* @return $this
* @throws \Exception
*/
public function addField(Field $field)
{
$this->_fieldMap[$field->getName()] = $field;

return $this;
}

/**
* @param array $fields
* @return $this
*/
public function addFields(array $fields)
{
foreach ($fields as $field) {
$this->addField($field);
}

return $this;
}

/**
* @return Field[]
* @throws \Exception
Expand Down
2 changes: 0 additions & 2 deletions src/Type/Definition/Behavior/NameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
trait NameTrait
{

// TODO: Add support for automatically resolving the name from the class name

/**
* @var string
*/
Expand Down
25 changes: 25 additions & 0 deletions src/Type/TypeKindEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Digia\GraphQL\Type;

class TypeKindEnum
{

const SCALAR = 'SCALAR';
const OBJECT = 'OBJECT';
const INTERFACE = 'INTERFACE';
const UNION = 'UNION';
const ENUM = 'ENUM';
const INPUT_OBJECT = 'INPUT_OBJECT';
const LIST = 'LIST';
const NON_NULL = 'NON_NULL';

/**
* @return array
* @throws \ReflectionException
*/
public static function values(): array
{
return array_values((new \ReflectionClass(__CLASS__))->getConstants());
}
}
1 change: 1 addition & 0 deletions src/Type/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Digia\GraphQL\Type\Definition\Contract\TypeInterface;
use Digia\GraphQL\Type\Definition\Contract\WrappingTypeInterface;
use Digia\GraphQL\Type\Definition\EnumType;
use Digia\GraphQL\Type\Definition\Field;
use Digia\GraphQL\Type\Definition\InputObjectType;
use Digia\GraphQL\Type\Definition\InterfaceType;
use Digia\GraphQL\Type\Definition\ListType;
Expand Down

0 comments on commit 73af766

Please sign in to comment.