本文整理汇总了PHP中GraphQL\Utils::invariant方法的典型用法代码示例。如果您正苦于以下问题:PHP Utils::invariant方法的具体用法?PHP Utils::invariant怎么用?PHP Utils::invariant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GraphQL\Utils
的用法示例。
在下文中一共展示了Utils::invariant方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getVariableValues
/**
* Prepares an object map of variables of the correct type based on the provided
* variable definitions and arbitrary input. If the input cannot be coerced
* to match the variable definitions, a Error will be thrown.
*
* @param Schema $schema
* @param VariableDefinitionNode[] $definitionNodes
* @param array $inputs
* @return array
* @throws Error
*/
public static function getVariableValues(Schema $schema, $definitionNodes, array $inputs)
{
$coercedValues = [];
foreach ($definitionNodes as $definitionNode) {
$varName = $definitionNode->variable->name->value;
$varType = Utils\TypeInfo::typeFromAST($schema, $definitionNode->type);
if (!Type::isInputType($varType)) {
throw new Error('Variable "$' . $varName . '" expected value of type ' . '"' . Printer::doPrint($definitionNode->type) . '" which cannot be used as an input type.', [$definitionNode->type]);
}
if (!array_key_exists($varName, $inputs)) {
$defaultValue = $definitionNode->defaultValue;
if ($defaultValue) {
$coercedValues[$varName] = Utils\AST::valueFromAST($defaultValue, $varType);
}
if ($varType instanceof NonNull) {
throw new Error('Variable "$' . $varName . '" of required type ' . '"' . Utils::printSafe($varType) . '" was not provided.', [$definitionNode]);
}
} else {
$value = $inputs[$varName];
$errors = self::isValidPHPValue($value, $varType);
if (!empty($errors)) {
$message = "\n" . implode("\n", $errors);
throw new Error('Variable "$' . $varName . '" got invalid value ' . json_encode($value) . '.' . $message, [$definitionNode]);
}
$coercedValue = self::coerceValue($varType, $value);
Utils::invariant($coercedValue !== Utils::undefined(), 'Should have reported error.');
$coercedValues[$varName] = $coercedValue;
}
}
return $coercedValues;
}
示例2: __construct
/**
* ScalarType constructor.
*/
public function __construct()
{
if (!isset($this->name)) {
$this->name = $this->tryInferName();
}
Utils::invariant($this->name, 'Type must be named.');
}
示例3: getField
/**
* @param $name
* @return FieldDefinition
* @throws \Exception
*/
public function getField($name)
{
if (null === $this->fields) {
$this->getFields();
}
Utils::invariant(isset($this->fields[$name]), 'Field "%s" is not defined for type "%s"', $name, $this->name);
return $this->fields[$name];
}
示例4: getTypeASTName
private function getTypeASTName(Type $typeAST)
{
if ($typeAST->kind === Node::NAME) {
return $typeAST->value;
}
Utils::invariant($typeAST->type, 'Must be wrapping type');
return $this->getTypeASTName($typeAST->type);
}
示例5: typeFromAST
/**
* @param Schema $schema
* @param $inputTypeAst
* @return ListOfType|NonNull|Name
* @throws \Exception
*/
public static function typeFromAST(Schema $schema, $inputTypeAst)
{
if ($inputTypeAst instanceof ListType) {
$innerType = self::typeFromAST($schema, $inputTypeAst->type);
return $innerType ? new ListOfType($innerType) : null;
}
if ($inputTypeAst instanceof NonNullType) {
$innerType = self::typeFromAST($schema, $inputTypeAst->type);
return $innerType ? new NonNull($innerType) : null;
}
Utils::invariant($inputTypeAst instanceof Name, 'Must be a type name');
return $schema->getType($inputTypeAst->value);
}
示例6: __construct
/**
* ObjectType constructor.
* @param array $config
*/
public function __construct(array $config)
{
if (!isset($config['name'])) {
$config['name'] = $this->tryInferName();
}
Utils::invariant(!empty($config['name']), 'Every type is expected to have name');
// Note: this validation is disabled by default, because it is resource-consuming
// TODO: add bin/validate script to check if schema is valid during development
Config::validate($config, ['name' => Config::NAME | Config::REQUIRED, 'fields' => Config::arrayOf(FieldDefinition::getDefinition(), Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE), 'description' => Config::STRING, 'interfaces' => Config::arrayOf(Config::INTERFACE_TYPE, Config::MAYBE_THUNK), 'isTypeOf' => Config::CALLBACK, 'resolveField' => Config::CALLBACK]);
$this->name = $config['name'];
$this->description = isset($config['description']) ? $config['description'] : null;
$this->resolveFieldFn = isset($config['resolveField']) ? $config['resolveField'] : null;
$this->config = $config;
}
示例7: __construct
public function __construct($config)
{
Config::validate($config, ['name' => Config::STRING | Config::REQUIRED, 'types' => Config::arrayOf(Config::OBJECT_TYPE | Config::REQUIRED), 'resolveType' => Config::CALLBACK, 'description' => Config::STRING]);
Utils::invariant(!empty($config['types']), "");
/**
* Optionally provide a custom type resolver function. If one is not provided,
* the default implemenation will call `isTypeOf` on each implementing
* Object type.
*/
$this->name = $config['name'];
$this->description = isset($config['description']) ? $config['description'] : null;
$this->_types = $config['types'];
$this->_resolveType = isset($config['resolveType']) ? $config['resolveType'] : null;
}
示例8: getTypes
/**
* @return ObjectType[]
*/
public function getTypes()
{
if (null === $this->types) {
if ($this->config['types'] instanceof \Closure) {
$types = call_user_func($this->config['types']);
} else {
$types = $this->config['types'];
}
Utils::invariant(is_array($types), 'Option "types" of union "%s" is expected to return array of types (or closure returning array of types)', $this->name);
$this->types = [];
foreach ($types as $type) {
$this->types[] = Type::resolve($type);
}
}
return $this->types;
}
示例9: __invoke
public function __invoke(ValidationContext $context)
{
return [Node::ARGUMENT => function (Argument $node) use($context) {
$fieldDef = $context->getFieldDef();
if ($fieldDef) {
$argDef = null;
foreach ($fieldDef->args as $arg) {
if ($arg->name === $node->name->value) {
$argDef = $arg;
break;
}
}
if (!$argDef) {
$parentType = $context->getParentType();
Utils::invariant($parentType);
return new Error(Messages::unknownArgMessage($node->name->value, $fieldDef->name, $parentType->name), [$node]);
}
}
}];
}
示例10: __invoke
public function __invoke(ValidationContext $context)
{
return [NodeKind::ARGUMENT => function (ArgumentNode $node, $key, $parent, $path, $ancestors) use($context) {
$argumentOf = $ancestors[count($ancestors) - 1];
if ($argumentOf->kind === NodeKind::FIELD) {
$fieldDef = $context->getFieldDef();
if ($fieldDef) {
$fieldArgDef = null;
foreach ($fieldDef->args as $arg) {
if ($arg->name === $node->name->value) {
$fieldArgDef = $arg;
break;
}
}
if (!$fieldArgDef) {
$parentType = $context->getParentType();
Utils::invariant($parentType);
$context->reportError(new Error(self::unknownArgMessage($node->name->value, $fieldDef->name, $parentType->name), [$node]));
}
}
} else {
if ($argumentOf->kind === NodeKind::DIRECTIVE) {
$directive = $context->getDirective();
if ($directive) {
$directiveArgDef = null;
foreach ($directive->args as $arg) {
if ($arg->name === $node->name->value) {
$directiveArgDef = $arg;
break;
}
}
if (!$directiveArgDef) {
$context->reportError(new Error(self::unknownDirectiveArgMessage($node->name->value, $directive->name), [$node]));
}
}
}
}
}];
}
示例11: _extractTypes
private function _extractTypes($type, &$map)
{
if (!$type) {
return $map;
}
if ($type instanceof WrappingType) {
return $this->_extractTypes($type->getWrappedType(), $map);
}
if (!empty($map[$type->name])) {
Utils::invariant($map[$type->name] === $type, "Schema must contain unique named types but contains multiple types named \"{$type}\".");
return $map;
}
$map[$type->name] = $type;
$nestedTypes = [];
if ($type instanceof InterfaceType || $type instanceof UnionType) {
$nestedTypes = $type->getPossibleTypes();
}
if ($type instanceof ObjectType) {
$nestedTypes = array_merge($nestedTypes, $type->getInterfaces());
}
if ($type instanceof ObjectType || $type instanceof InterfaceType || $type instanceof InputObjectType) {
foreach ((array) $type->getFields() as $fieldName => $field) {
if (isset($field->args)) {
$fieldArgTypes = array_map(function ($arg) {
return $arg->getType();
}, $field->args);
$nestedTypes = array_merge($nestedTypes, $fieldArgTypes);
}
$nestedTypes[] = $field->getType();
}
}
foreach ($nestedTypes as $type) {
$this->_extractTypes($type, $map);
}
return $map;
}
示例12: getWrappedType
/**
* @param bool $recurse
* @return mixed
* @throws \Exception
*/
public function getWrappedType($recurse = false)
{
$type = Type::resolve($this->ofType);
Utils::invariant(!$type instanceof NonNull, 'Cannot nest NonNull inside NonNull');
return $recurse && $type instanceof WrappingType ? $type->getWrappedType($recurse) : $type;
}
示例13: completeField
/**
* Implements the instructions for completeValue as defined in the
* "Field entries" section of the spec.
*
* If the field type is Non-Null, then this recursively completes the value
* for the inner type. It throws a field error if that completion returns null,
* as per the "Nullability" section of the spec.
*
* If the field type is a List, then this recursively completes the value
* for the inner type on each item in the list.
*
* If the field type is a Scalar or Enum, ensures the completed value is a legal
* value of the type by calling the `coerce` method of GraphQL type definition.
*
* Otherwise, the field type expects a sub-selection set, and will complete the
* value by evaluating all sub-selections.
*/
private static function completeField(ExecutionContext $exeContext, Type $fieldType, $fieldASTs, &$result)
{
// If field type is NonNull, complete for inner type, and throw field error
// if result is null.
if ($fieldType instanceof NonNull) {
$completed = self::completeField($exeContext, $fieldType->getWrappedType(), $fieldASTs, $result);
if ($completed === null) {
throw new Error('Cannot return null for non-nullable type.', $fieldASTs instanceof \ArrayObject ? $fieldASTs->getArrayCopy() : $fieldASTs);
}
return $completed;
}
// If result is null-like, return null.
if (null === $result) {
return null;
}
// If field type is List, complete each item in the list with the inner type
if ($fieldType instanceof ListOfType) {
$itemType = $fieldType->getWrappedType();
Utils::invariant(is_array($result) || $result instanceof \Traversable, 'User Error: expected iterable, but did not find one.');
$tmp = [];
foreach ($result as $item) {
$tmp[] = self::completeField($exeContext, $itemType, $fieldASTs, $item);
}
return $tmp;
}
// If field type is Scalar or Enum, coerce to a valid value, returning null
// if coercion is not possible.
if ($fieldType instanceof ScalarType || $fieldType instanceof EnumType) {
Utils::invariant(method_exists($fieldType, 'coerce'), 'Missing coerce method on type');
return $fieldType->coerce($result);
}
// Field type must be Object, Interface or Union and expect sub-selections.
$objectType = $fieldType instanceof ObjectType ? $fieldType : ($fieldType instanceof InterfaceType || $fieldType instanceof UnionType ? $fieldType->resolveType($result) : null);
if (!$objectType) {
return null;
}
// Collect sub-fields to execute to complete this value.
$subFieldASTs = new \ArrayObject();
$visitedFragmentNames = new \ArrayObject();
for ($i = 0; $i < count($fieldASTs); $i++) {
$selectionSet = $fieldASTs[$i]->selectionSet;
if ($selectionSet) {
$subFieldASTs = self::collectFields($exeContext, $objectType, $selectionSet, $subFieldASTs, $visitedFragmentNames);
}
}
return self::executeFields($exeContext, $objectType, $result, $subFieldASTs);
}
示例14: resolve
public static function resolve($type)
{
if (is_callable($type)) {
$type = $type();
}
Utils::invariant($type instanceof Type, 'Expecting instance of ' . __CLASS__ . ' (or callable returning instance of that type), got "%s"', Utils::getVariableType($type));
return $type;
}
示例15: completeListValue
/**
* Complete a list value by completing each item in the list with the
* inner type
*
* @param ExecutionContext $exeContext
* @param ListOfType $returnType
* @param $fieldNodes
* @param ResolveInfo $info
* @param array $path
* @param $result
* @return array|Promise
* @throws \Exception
*/
private static function completeListValue(ExecutionContext $exeContext, ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
{
$itemType = $returnType->getWrappedType();
Utils::invariant(is_array($result) || $result instanceof \Traversable, 'User Error: expected iterable, but did not find one for field ' . $info->parentType . '.' . $info->fieldName . '.');
$containsPromise = false;
$i = 0;
$completedItems = [];
foreach ($result as $item) {
$fieldPath = $path;
$fieldPath[] = $i++;
$completedItem = self::completeValueCatchingError($exeContext, $itemType, $fieldNodes, $info, $fieldPath, $item);
if (!$containsPromise && self::$promiseAdapter->isPromise($completedItem)) {
$containsPromise = true;
}
$completedItems[] = $completedItem;
}
return $containsPromise ? self::$promiseAdapter->createPromiseAll($completedItems) : $completedItems;
}