2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL;
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
use GraphQL\Type\Definition\AbstractType;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Type\Definition\Directive;
|
2015-08-17 17:01:55 +03:00
|
|
|
use GraphQL\Type\Definition\InputObjectType;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Type\Definition\InterfaceType;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Type\Definition\UnionType;
|
|
|
|
use GraphQL\Type\Definition\WrappingType;
|
|
|
|
use GraphQL\Type\Introspection;
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
/**
|
2016-10-18 21:34:46 +03:00
|
|
|
* Schema Definition
|
|
|
|
*
|
|
|
|
* A Schema is created by supplying the root types of each type of operation:
|
|
|
|
* query, mutation (optional) and subscription (optional). A schema definition is
|
|
|
|
* then supplied to the validator and executor.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* $schema = new GraphQL\Schema([
|
|
|
|
* 'query' => $MyAppQueryRootType,
|
|
|
|
* 'mutation' => $MyAppMutationRootType,
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* Note: If an array of `directives` are provided to GraphQL\Schema, that will be
|
|
|
|
* the exact list of directives represented and allowed. If `directives` is not
|
|
|
|
* provided then a default set of the specified directives (e.g. @include and
|
|
|
|
* @skip) will be used. If you wish to provide *additional* directives to these
|
|
|
|
* specified directives, you must explicitly declare them. Example:
|
|
|
|
*
|
|
|
|
* $mySchema = new GraphQL\Schema([
|
|
|
|
* ...
|
|
|
|
* 'directives' => array_merge(GraphQL::getInternalDirectives(), [ $myCustomDirective ]),
|
|
|
|
* ])
|
|
|
|
*
|
2016-10-17 14:33:47 +03:00
|
|
|
* @package GraphQL
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
class Schema
|
|
|
|
{
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var ObjectType
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
protected $queryType;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var ObjectType
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
protected $mutationType;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var ObjectType
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
protected $subscriptionType;
|
2015-12-21 02:29:29 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var Directive[]
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
protected $directives;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, Type>
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
protected $typeMap;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var array<string, ObjectType[]>
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
protected $implementations;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var array<string, array<string, boolean>>
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
protected $possibleTypeMap;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Schema constructor.
|
|
|
|
* @param array $config
|
|
|
|
*/
|
|
|
|
public function __construct($config = null)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-04-25 00:57:09 +03:00
|
|
|
if (func_num_args() > 1 || $config instanceof Type) {
|
|
|
|
trigger_error(
|
|
|
|
'GraphQL\Schema constructor expects config object now instead of types passed as arguments. '.
|
|
|
|
'See https://github.com/webonyx/graphql-php/issues/36',
|
|
|
|
E_USER_DEPRECATED
|
|
|
|
);
|
2016-09-15 13:32:54 +03:00
|
|
|
list($queryType, $mutationType, $subscriptionType) = func_get_args() + [null, null, null];
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
$config = [
|
|
|
|
'query' => $queryType,
|
|
|
|
'mutation' => $mutationType,
|
|
|
|
'subscription' => $subscriptionType
|
|
|
|
];
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->init($config);
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
/**
|
|
|
|
* @param array $config
|
|
|
|
*/
|
|
|
|
protected function init(array $config)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
|
|
|
$config += [
|
|
|
|
'query' => null,
|
|
|
|
'mutation' => null,
|
|
|
|
'subscription' => null,
|
2016-05-02 00:42:05 +03:00
|
|
|
'types' => [],
|
2016-04-25 00:57:09 +03:00
|
|
|
'directives' => [],
|
|
|
|
'validate' => true
|
|
|
|
];
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
Utils::invariant(
|
|
|
|
$config['query'] instanceof ObjectType,
|
|
|
|
"Schema query must be Object Type but got: " . Utils::getVariableType($config['query'])
|
|
|
|
);
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->queryType = $config['query'];
|
2016-05-02 00:42:05 +03:00
|
|
|
|
|
|
|
Utils::invariant(
|
|
|
|
!$config['mutation'] || $config['mutation'] instanceof ObjectType,
|
|
|
|
"Schema mutation must be Object Type if provided but got: " . Utils::getVariableType($config['mutation'])
|
|
|
|
);
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->mutationType = $config['mutation'];
|
2016-05-02 00:42:05 +03:00
|
|
|
|
|
|
|
Utils::invariant(
|
|
|
|
!$config['subscription'] || $config['subscription'] instanceof ObjectType,
|
|
|
|
"Schema subscription must be Object Type if provided but got: " . Utils::getVariableType($config['subscription'])
|
|
|
|
);
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->subscriptionType = $config['subscription'];
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
Utils::invariant(
|
|
|
|
!$config['types'] || is_array($config['types']),
|
|
|
|
"Schema types must be Array if provided but got: " . Utils::getVariableType($config['types'])
|
|
|
|
);
|
|
|
|
|
|
|
|
Utils::invariant(
|
|
|
|
!$config['directives'] || (is_array($config['directives']) && Utils::every($config['directives'], function($d) {return $d instanceof Directive;})),
|
|
|
|
"Schema directives must be Directive[] if provided but got " . Utils::getVariableType($config['directives'])
|
|
|
|
);
|
|
|
|
|
2016-10-18 21:34:46 +03:00
|
|
|
$this->directives = $config['directives'] ?: GraphQL::getInternalDirectives();
|
2016-04-19 22:16:09 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
// Build type map now to detect any errors within this schema.
|
2016-04-25 00:57:09 +03:00
|
|
|
$initialTypes = [
|
|
|
|
$config['query'],
|
|
|
|
$config['mutation'],
|
|
|
|
$config['subscription'],
|
|
|
|
Introspection::_schema()
|
|
|
|
];
|
|
|
|
if (!empty($config['types'])) {
|
|
|
|
$initialTypes = array_merge($initialTypes, $config['types']);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($initialTypes as $type) {
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->extractTypes($type);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->typeMap += Type::getInternalTypes();
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
// Keep track of all implementations by interface name.
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->implementations = [];
|
|
|
|
foreach ($this->typeMap as $typeName => $type) {
|
2016-04-25 00:57:09 +03:00
|
|
|
if ($type instanceof ObjectType) {
|
|
|
|
foreach ($type->getInterfaces() as $iface) {
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->implementations[$iface->name][] = $type;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @return ObjectType
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function getQueryType()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->queryType;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @return ObjectType
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function getMutationType()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->mutationType;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @return ObjectType
|
|
|
|
*/
|
2015-12-21 02:29:29 +03:00
|
|
|
public function getSubscriptionType()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->subscriptionType;
|
2015-12-21 02:29:29 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
/**
|
2016-04-25 00:57:09 +03:00
|
|
|
* @return array
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2016-04-25 00:57:09 +03:00
|
|
|
public function getTypeMap()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeMap;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return Type
|
|
|
|
*/
|
|
|
|
public function getType($name)
|
|
|
|
{
|
|
|
|
$map = $this->getTypeMap();
|
|
|
|
return isset($map[$name]) ? $map[$name] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param AbstractType $abstractType
|
|
|
|
* @return ObjectType[]
|
|
|
|
*/
|
|
|
|
public function getPossibleTypes(AbstractType $abstractType)
|
|
|
|
{
|
|
|
|
if ($abstractType instanceof UnionType) {
|
|
|
|
return $abstractType->getTypes();
|
|
|
|
}
|
|
|
|
Utils::invariant($abstractType instanceof InterfaceType);
|
2016-10-17 14:33:47 +03:00
|
|
|
return isset($this->implementations[$abstractType->name]) ? $this->implementations[$abstractType->name] : [];
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param AbstractType $abstractType
|
|
|
|
* @param ObjectType $possibleType
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPossibleType(AbstractType $abstractType, ObjectType $possibleType)
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
if (null === $this->possibleTypeMap) {
|
|
|
|
$this->possibleTypeMap = [];
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
if (!isset($this->possibleTypeMap[$abstractType->name])) {
|
2016-04-25 00:57:09 +03:00
|
|
|
$tmp = [];
|
|
|
|
foreach ($this->getPossibleTypes($abstractType) as $type) {
|
|
|
|
$tmp[$type->name] = true;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-10-18 21:34:46 +03:00
|
|
|
|
|
|
|
Utils::invariant(
|
|
|
|
!empty($tmp),
|
|
|
|
'Could not find possible implementing types for $%s ' .
|
|
|
|
'in schema. Check that schema.types is defined and is an array of ' .
|
|
|
|
'all possible types in the schema.',
|
|
|
|
$abstractType->name
|
|
|
|
);
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->possibleTypeMap[$abstractType->name] = $tmp;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
return !empty($this->possibleTypeMap[$abstractType->name][$possibleType->name]);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-25 00:57:09 +03:00
|
|
|
* @return Directive[]
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
|
|
|
public function getDirectives()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->directives;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @return Directive
|
|
|
|
*/
|
|
|
|
public function getDirective($name)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-04-25 00:57:09 +03:00
|
|
|
foreach ($this->getDirectives() as $directive) {
|
|
|
|
if ($directive->name === $name) {
|
|
|
|
return $directive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @deprecated since 17.10.2016 in favor of $this->extractTypes
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-05-02 00:42:05 +03:00
|
|
|
protected function _extractTypes($type)
|
2016-10-17 14:33:47 +03:00
|
|
|
{
|
|
|
|
trigger_error(__METHOD__ . ' is deprecated in favor of ' . __CLASS__ . '::extractTypes', E_USER_DEPRECATED);
|
|
|
|
return $this->extractTypes($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function extractTypes($type)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
if (!$type) {
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeMap;
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
if ($type instanceof WrappingType) {
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->extractTypes($type->getWrappedType(true));
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:33:47 +03:00
|
|
|
if (!empty($this->typeMap[$type->name])) {
|
2015-08-17 17:01:55 +03:00
|
|
|
Utils::invariant(
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->typeMap[$type->name] === $type,
|
2015-08-17 17:01:55 +03:00
|
|
|
"Schema must contain unique named types but contains multiple types named \"$type\"."
|
|
|
|
);
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeMap;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->typeMap[$type->name] = $type;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
$nestedTypes = [];
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
if ($type instanceof UnionType) {
|
|
|
|
$nestedTypes = $type->getTypes();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
if ($type instanceof ObjectType) {
|
|
|
|
$nestedTypes = array_merge($nestedTypes, $type->getInterfaces());
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
if ($type instanceof ObjectType || $type instanceof InterfaceType || $type instanceof InputObjectType) {
|
2015-07-15 20:05:46 +03:00
|
|
|
foreach ((array) $type->getFields() as $fieldName => $field) {
|
2015-08-17 17:01:55 +03:00
|
|
|
if (isset($field->args)) {
|
|
|
|
$fieldArgTypes = array_map(function($arg) { return $arg->getType(); }, $field->args);
|
|
|
|
$nestedTypes = array_merge($nestedTypes, $fieldArgTypes);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
$nestedTypes[] = $field->getType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($nestedTypes as $type) {
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->extractTypes($type);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeMap;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|