php GraphQL-Utils类(方法)实例源码

下面列出了php GraphQL-Utils 类(方法)源码代码实例,从而了解它的用法。

作者:rtui    项目:graphql-ph   
public function __invoke(ValidationContext $context)
 {
     return [Node::FIELD => function (Field $fieldAST) use($context) {
         $fieldDef = $context->getFieldDef();
         if (!$fieldDef) {
             return Visitor::skipNode();
         }
         $errors = [];
         $argASTs = $fieldAST->arguments ?: [];
         $argASTMap = Utils::keyMap($argASTs, function (Argument $arg) {
             return $arg->name->value;
         });
         foreach ($fieldDef->args as $argDef) {
             $argAST = isset($argASTMap[$argDef->name]) ? $argASTMap[$argDef->name] : null;
             if (!$argAST && $argDef->getType() instanceof NonNull) {
                 $errors[] = new Error(Messages::missingArgMessage($fieldAST->name->value, $argDef->name, $argDef->getType()), [$fieldAST]);
             }
         }
         $argDefMap = Utils::keyMap($fieldDef->args, function ($def) {
             return $def->name;
         });
         foreach ($argASTs as $argAST) {
             $argDef = $argDefMap[$argAST->name->value];
             if ($argDef && !DocumentValidator::isValidLiteralValue($argAST->value, $argDef->getType())) {
                 $errors[] = new Error(Messages::badValueMessage($argAST->name->value, $argDef->getType(), Printer::doPrint($argAST->value)), [$argAST->value]);
             }
         }
         return !empty($errors) ? $errors : null;
     }];
 }

作者:aeshio    项目:ZeroPH   
public function parseValue($value)
 {
     if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
         throw new \Exception('Cannot represent value as email: ' . Utils::printSafe($value));
     }
     return $value;
 }

作者:rtui    项目:graphql-ph   
private function doTypesOverlap($t1, $t2)
 {
     if ($t1 === $t2) {
         return true;
     }
     if ($t1 instanceof ObjectType) {
         if ($t2 instanceof ObjectType) {
             return false;
         }
         return in_array($t1, $t2->getPossibleTypes());
     }
     if ($t1 instanceof InterfaceType || $t1 instanceof UnionType) {
         if ($t2 instanceof ObjectType) {
             return in_array($t2, $t1->getPossibleTypes());
         }
         $t1TypeNames = Utils::keyMap($t1->getPossibleTypes(), function ($type) {
             return $type->name;
         });
         foreach ($t2->getPossibleTypes() as $type) {
             if (!empty($t1TypeNames[$type->name])) {
                 return true;
             }
         }
     }
     return false;
 }

作者:aeshio    项目:ZeroPH   
public function parseValue($value)
 {
     if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) {
         throw new \Exception('Cannot represent value as URL:' . Utils::printSafe($value));
     }
     return $value;
 }

作者:webony    项目:graphql-ph   
/**
  * ScalarType constructor.
  */
 public function __construct()
 {
     if (!isset($this->name)) {
         $this->name = $this->tryInferName();
     }
     Utils::invariant($this->name, 'Type must be named.');
 }

作者:aeshio    项目:ZeroPH   
/**
  * @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];
 }

作者:aeshio    项目:ZeroPH   
/**
  * Parses an externally provided value (query variable) to use as an input
  *
  * @param mixed $value
  * @return mixed
  */
 public function parseValue($value)
 {
     if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) {
         // quite naive, but after all this is example
         throw new \UnexpectedValueException("Cannot represent value as URL: " . Utils::printSafe($value));
     }
     return $value;
 }

作者:rtui    项目:graphql-ph   
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);
 }

作者:aeshio    项目:ZeroPH   
/**
  * @param $value
  * @return float|null
  */
 private function coerceFloat($value)
 {
     if ($value === '') {
         throw new InvariantViolation('Float cannot represent non numeric value: (empty string)');
     }
     if (is_numeric($value) || $value === true || $value === false) {
         return (double) $value;
     }
     throw new InvariantViolation('Float cannot represent non numeric value: ' . Utils::printSafe($value));
 }

作者:webony    项目:graphql-ph   
private function withModifiers($types)
 {
     return array_merge(Utils::map($types, function ($type) {
         return Type::listOf($type);
     }), Utils::map($types, function ($type) {
         return Type::nonNull($type);
     }), Utils::map($types, function ($type) {
         return Type::nonNull(Type::listOf($type));
     }));
 }

作者:rtui    项目:graphql-ph   
public function __construct($config)
 {
     Config::validate($config, ['name' => Config::STRING | Config::REQUIRED, 'values' => Config::arrayOf(['name' => Config::STRING | Config::REQUIRED, 'value' => Config::ANY, 'deprecationReason' => Config::STRING, 'description' => Config::STRING], Config::KEY_AS_NAME), 'description' => Config::STRING]);
     $this->name = $config['name'];
     $this->description = isset($config['description']) ? $config['description'] : null;
     $this->_values = [];
     if (!empty($config['values'])) {
         foreach ($config['values'] as $name => $value) {
             $this->_values[] = Utils::assign(new EnumValueDefinition(), $value + ['name' => $name]);
         }
     }
 }

作者:aeshio    项目:ZeroPH   
/**
  * Return implementations of `type` that include `fieldName` as a valid field.
  *
  * @param Schema $schema
  * @param AbstractType $type
  * @param $fieldName
  * @return array
  */
 static function getImplementationsIncludingField(Schema $schema, AbstractType $type, $fieldName)
 {
     $types = $schema->getPossibleTypes($type);
     $types = Utils::filter($types, function ($t) use($fieldName) {
         return isset($t->getFields()[$fieldName]);
     });
     $types = Utils::map($types, function ($t) {
         return $t->name;
     });
     sort($types);
     return $types;
 }

作者:aeshio    项目:ZeroPH   
/**
  * @param $value
  * @return int|null
  */
 private function coerceInt($value)
 {
     if ($value === '') {
         throw new InvariantViolation('Int cannot represent non 32-bit signed integer value: (empty string)');
     }
     if (false === $value || true === $value) {
         return (int) $value;
     }
     if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
         return (int) $value;
     }
     throw new InvariantViolation('Int cannot represent non 32-bit signed integer value: ' . Utils::printSafe($value));
 }

作者:webony    项目:graphql-ph   
public function __invoke(ValidationContext $context)
 {
     $operationCount = 0;
     return [NodeKind::DOCUMENT => function (DocumentNode $node) use(&$operationCount) {
         $tmp = Utils::filter($node->definitions, function ($definition) {
             return $definition->kind === NodeKind::OPERATION_DEFINITION;
         });
         $operationCount = count($tmp);
     }, NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use(&$operationCount, $context) {
         if (!$node->name && $operationCount > 1) {
             $context->reportError(new Error(self::anonOperationNotAloneMessage(), [$node]));
         }
     }];
 }

作者:rtui    项目:graphql-ph   
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;
 }

作者:webony    项目:graphql-ph   
/**
  * @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;
 }

作者:rtui    项目:graphql-ph   
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]);
             }
         }
     }];
 }

作者:webony    项目:graphql-ph   
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]));
                     }
                 }
             }
         }
     }];
 }

作者:aeshio    项目:ZeroPH   
private function detectCycleRecursive(FragmentDefinition $fragment, ValidationContext $context)
 {
     $fragmentName = $fragment->name->value;
     $this->visitedFrags[$fragmentName] = true;
     $spreadNodes = $context->getFragmentSpreads($fragment);
     if (empty($spreadNodes)) {
         return;
     }
     $this->spreadPathIndexByName[$fragmentName] = count($this->spreadPath);
     for ($i = 0; $i < count($spreadNodes); $i++) {
         $spreadNode = $spreadNodes[$i];
         $spreadName = $spreadNode->name->value;
         $cycleIndex = isset($this->spreadPathIndexByName[$spreadName]) ? $this->spreadPathIndexByName[$spreadName] : null;
         if ($cycleIndex === null) {
             $this->spreadPath[] = $spreadNode;
             if (empty($this->visitedFrags[$spreadName])) {
                 $spreadFragment = $context->getFragment($spreadName);
                 if ($spreadFragment) {
                     $this->detectCycleRecursive($spreadFragment, $context);
                 }
             }
             array_pop($this->spreadPath);
         } else {
             $cyclePath = array_slice($this->spreadPath, $cycleIndex);
             $nodes = $cyclePath;
             if (is_array($spreadNode)) {
                 $nodes = array_merge($nodes, $spreadNode);
             } else {
                 $nodes[] = $spreadNode;
             }
             $context->reportError(new Error(self::cycleErrorMessage($spreadName, Utils::map($cyclePath, function ($s) {
                 return $s->name->value;
             })), $nodes));
         }
     }
     $this->spreadPathIndexByName[$fragmentName] = null;
 }

作者:rtui    项目:graphql-ph   
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;
 }


问题


面经


文章

微信
公众号

扫码关注公众号