GraphQL\Schema::getPossibleTypes PHP Method

getPossibleTypes() public method

public getPossibleTypes ( GraphQL\Type\Definition\AbstractType $abstractType ) : ObjectType[]
$abstractType GraphQL\Type\Definition\AbstractType
return GraphQL\Type\Definition\ObjectType[]
    public function getPossibleTypes(AbstractType $abstractType)
    {
        if ($abstractType instanceof UnionType) {
            return $abstractType->getTypes();
        }
        Utils::invariant($abstractType instanceof InterfaceType);
        return isset($this->implementations[$abstractType->name]) ? $this->implementations[$abstractType->name] : [];
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Go through all of the implementations of type, and find other interaces
  * that they implement. If those interfaces include `field` as a valid field,
  * return them, sorted by how often the implementations include the other
  * interface.
  */
 static function getSiblingInterfacesIncludingField(Schema $schema, AbstractType $type, $fieldName)
 {
     $types = $schema->getPossibleTypes($type);
     $suggestedInterfaces = array_reduce($types, function ($acc, $t) use($fieldName) {
         foreach ($t->getInterfaces() as $i) {
             if (empty($i->getFields()[$fieldName])) {
                 continue;
             }
             if (!isset($acc[$i->name])) {
                 $acc[$i->name] = 0;
             }
             $acc[$i->name] += 1;
         }
         return $acc;
     }, []);
     $suggestedInterfaceNames = array_keys($suggestedInterfaces);
     usort($suggestedInterfaceNames, function ($a, $b) use($suggestedInterfaces) {
         return $suggestedInterfaces[$b] - $suggestedInterfaces[$a];
     });
     return $suggestedInterfaceNames;
 }
All Usage Examples Of GraphQL\Schema::getPossibleTypes