当前位置: 首页>>代码示例>>PHP>>正文


PHP Type::serialize方法代码示例

本文整理汇总了PHP中GraphQL\Type\Definition\Type::serialize方法的典型用法代码示例。如果您正苦于以下问题:PHP Type::serialize方法的具体用法?PHP Type::serialize怎么用?PHP Type::serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在GraphQL\Type\Definition\Type的用法示例。


在下文中一共展示了Type::serialize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: completeValue

 /**
  * 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 `serialize` 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 completeValue(ExecutionContext $exeContext, Type $returnType, $fieldASTs, ResolveInfo $info, &$result)
 {
     // If field type is NonNull, complete for inner type, and throw field error
     // if result is null.
     if ($returnType instanceof NonNull) {
         $completed = self::completeValue($exeContext, $returnType->getWrappedType(), $fieldASTs, $info, $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 Scalar or Enum, serialize to a valid value, returning
     // null if serialization is not possible.
     if ($returnType instanceof ScalarType || $returnType instanceof EnumType) {
         return $returnType->serialize($result);
     }
     // If field type is List, and return type is Composite - complete by executing these fields with list value as parameter
     if ($returnType instanceof ListOfType) {
         $itemType = $returnType->getWrappedType();
         Utils::invariant(is_array($result) || $result instanceof \Traversable, 'User Error: expected iterable, but did not find one.');
         // For Object[]:
         // Allow all object fields to process list value in it's `map` callback:
         if ($itemType instanceof ObjectType) {
             // Filter out nulls (as `map` doesn't expect it):
             $list = [];
             foreach ($result as $index => $item) {
                 if (null !== $item) {
                     $list[] = $item;
                 }
             }
             $subFieldASTs = self::collectSubFields($exeContext, $itemType, $fieldASTs);
             $mapped = self::executeFields($exeContext, $itemType, $list, $subFieldASTs);
             $i = 0;
             $completed = [];
             foreach ($result as $index => $item) {
                 if (null === $item) {
                     // Complete nulls separately
                     $completed[] = self::completeValueCatchingError($exeContext, $itemType, $fieldASTs, $info, $item);
                 } else {
                     // Assuming same order of mapped values
                     $completed[] = $mapped[$i++];
                 }
             }
             return $completed;
         } else {
             if ($itemType instanceof AbstractType) {
                 // Values sharded by ObjectType
                 $listPerObjectType = [];
                 // Helper structures to restore ordering after resolve calls
                 $resultTypeMap = [];
                 $typeNameMap = [];
                 $cursors = [];
                 $copied = [];
                 foreach ($result as $index => $item) {
                     $copied[$index] = $item;
                     if (null !== $item) {
                         $objectType = $itemType->getObjectType($item, $info);
                         if ($objectType && !$itemType->isPossibleType($objectType)) {
                             $exeContext->addError(new Error("Runtime Object type \"{$objectType}\" is not a possible type for \"{$itemType}\"."));
                             $copied[$index] = null;
                         } else {
                             $listPerObjectType[$objectType->name][] = $item;
                             $resultTypeMap[$index] = $objectType->name;
                             $typeNameMap[$objectType->name] = $objectType;
                         }
                     }
                 }
                 $mapped = [];
                 foreach ($listPerObjectType as $typeName => $list) {
                     $objectType = $typeNameMap[$typeName];
                     $subFieldASTs = self::collectSubFields($exeContext, $objectType, $fieldASTs);
                     $mapped[$typeName] = self::executeFields($exeContext, $objectType, $list, $subFieldASTs);
                     $cursors[$typeName] = 0;
                 }
                 // Restore order:
                 $completed = [];
                 foreach ($copied as $index => $item) {
                     if (null === $item) {
//.........这里部分代码省略.........
开发者ID:mrsinguyen,项目名称:graphql-php,代码行数:101,代码来源:DeprecatedMappingExecutor.php


注:本文中的GraphQL\Type\Definition\Type::serialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。