Example #1
0
 protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null)
 {
     /** @var AstQuery|AstField $ast */
     $arguments = $this->parseArgumentsValues($field, $ast);
     $astFields = $ast instanceof AstQuery ? $ast->getFields() : [];
     $resolveInfo = $this->createResolveInfo($field, $astFields);
     $this->assertClientHasFieldAccess($resolveInfo);
     if ($field instanceof Field) {
         if ($resolveFunc = $field->getConfig()->getResolveFunction()) {
             if ($this->isServiceReference($resolveFunc)) {
                 $service = substr($resolveFunc[0], 1);
                 $method = $resolveFunc[1];
                 if (!$this->executionContext->getContainer()->has($service)) {
                     throw new ResolveException(sprintf('Resolve service "%s" not found for field "%s"', $service, $field->getName()));
                 }
                 $serviceInstance = $this->executionContext->getContainer()->get($service);
                 if (!method_exists($serviceInstance, $method)) {
                     throw new ResolveException(sprintf('Resolve method "%s" not found in "%s" service for field "%s"', $method, $service, $field->getName()));
                 }
                 return $serviceInstance->{$method}($parentValue, $arguments, $resolveInfo);
             }
             return $resolveFunc($parentValue, $arguments, $resolveInfo);
         } else {
             return TypeService::getPropertyValue($parentValue, $field->getName());
         }
     } else {
         //instance of AbstractContainerAwareField
         if (in_array('Symfony\\Component\\DependencyInjection\\ContainerAwareInterface', class_implements($field))) {
             /** @var $field ContainerAwareInterface */
             $field->setContainer($this->executionContext->getContainer()->getSymfonyContainer());
         }
         return $field->resolve($parentValue, $arguments, $resolveInfo);
     }
 }
Example #2
0
 public function assetTypeHasField(AbstractType $objectType, AstFieldInterface $ast)
 {
     /** @var AbstractObjectType $objectType */
     if (!(TypeService::isObjectType($objectType) || TypeService::isInputObjectType($objectType)) || !$objectType->hasField($ast->getName())) {
         throw new ResolveException(sprintf('Field "%s" not found in type "%s"', $ast->getName(), $objectType->getNamedType()->getName()), $ast->getLocation());
     }
 }
Example #3
0
 public function testScalarPrimitives()
 {
     foreach (TypeFactory::getScalarTypesNames() as $typeName) {
         $scalarType = TypeFactory::getScalarType($typeName);
         $testDataMethod = 'get' . $typeName . 'TestData';
         $this->assertNotEmpty($scalarType->getDescription());
         $this->assertEquals($scalarType->getKind(), TypeMap::KIND_SCALAR);
         $this->assertEquals($scalarType->isCompositeType(), false);
         $this->assertEquals(TypeService::isAbstractType($scalarType), false);
         $this->assertEquals($scalarType->getType(), $scalarType);
         $this->assertEquals($scalarType->getType(), $scalarType->getNamedType());
         $this->assertNull($scalarType->getConfig());
         foreach (call_user_func(['Youshido\\Tests\\DataProvider\\TestScalarDataProvider', $testDataMethod]) as list($data, $serialized, $isValid)) {
             $this->assertSerialization($scalarType, $data, $serialized);
             $this->assertParse($scalarType, $data, $serialized);
             if ($isValid) {
                 $this->assertTrue($scalarType->isValidValue($data), $typeName . ' validation for :' . serialize($data));
             } else {
                 $this->assertFalse($scalarType->isValidValue($data), $typeName . ' validation for :' . serialize($data));
             }
         }
     }
     try {
         TypeFactory::getScalarType('invalid type');
     } catch (\Exception $e) {
         $this->assertEquals('Configuration problem with type invalid type', $e->getMessage());
     }
     $this->assertEquals('String', (string) new StringType());
 }
Example #4
0
 /**
  * NonNullType constructor.
  *
  * @param AbstractType|string $fieldType
  *
  * @throws ConfigurationException
  */
 public function __construct($fieldType)
 {
     if (!TypeService::isGraphQLType($fieldType)) {
         throw new ConfigurationException('NonNullType accepts only GraphpQL Types as argument');
     }
     if (TypeService::isScalarType($fieldType)) {
         $fieldType = TypeFactory::getScalarType($fieldType);
     }
     $this->_typeOf = $fieldType;
 }
Example #5
0
 public function __construct(array $config = [])
 {
     if (empty($config['type'])) {
         $config['type'] = $this->getType();
         $config['name'] = $this->getName();
     }
     if (TypeService::isScalarType($config['type'])) {
         $config['type'] = TypeFactory::getScalarType($config['type']);
     }
     $this->config = new InputFieldConfig($config, $this, $this->isFinal);
     $this->build($this->config);
 }
 public function resolve($value, array $args, ResolveInfo $info)
 {
     if ($resolveFunction = $this->getConfig()->getResolveFunction()) {
         return $resolveFunction($value, $args, $info);
     } else {
         if (is_array($value) && array_key_exists($this->getName(), $value)) {
             return $value[$this->getName()];
         } elseif (is_object($value)) {
             return TypeService::getPropertyValue($value, $this->getName());
         } elseif ($this->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) {
             return null;
         } else {
             throw new \Exception(sprintf('Property "%s" not found in resolve result', $this->getName()));
         }
     }
 }
Example #7
0
 public function testNonNullType()
 {
     $stringType = new StringType();
     $nonNullType = new NonNullType(new StringType());
     $nonNullOnString = new NonNullType(TypeMap::TYPE_STRING);
     $testArray = ['a' => 'b'];
     $this->assertEquals($nonNullType->getName(), null, 'Empty non-null name');
     $this->assertEquals($nonNullType->getKind(), TypeMap::KIND_NON_NULL);
     $this->assertEquals($nonNullType->getType(), new NonNullType($stringType));
     $this->assertEquals($nonNullType->getNullableType(), $stringType);
     $this->assertEquals($nonNullType->getNullableType(), $nonNullOnString->getNullableType());
     $this->assertEquals($nonNullType->getNamedType(), $stringType);
     $this->assertEquals($nonNullType->getTypeOf(), $stringType);
     $this->assertEquals($nonNullType->isCompositeType(), true);
     $this->assertEquals(TypeService::isAbstractType($nonNullType), false);
     $this->assertFalse($nonNullType->isValidValue(null));
     $this->assertTrue($nonNullType->isValidValue($stringType));
     $this->assertFalse($nonNullType->isValidValue(new \stdClass()));
     $this->assertEquals($nonNullType->parseValue($testArray), '');
     $this->assertEquals($nonNullType->resolve($testArray), $testArray);
 }
Example #8
0
 /**
  * @param string $type
  *
  * @throws ConfigurationException
  * @return AbstractScalarType
  */
 public static function getScalarType($type)
 {
     if (TypeService::isScalarType($type)) {
         if (is_object($type)) {
             $typeName = $type->getName();
             if (empty(self::$objectsHash[$typeName])) {
                 self::$objectsHash[$typeName] = $type;
             }
             return self::$objectsHash[$typeName];
         }
         if (empty(self::$objectsHash[$type])) {
             $name = ucfirst($type);
             $name = $name == 'Datetime' ? 'DateTime' : $name;
             $name = $name == 'Datetimetz' ? 'DateTimeTz' : $name;
             $className = 'Youshido\\GraphQL\\Type\\Scalar\\' . $name . 'Type';
             self::$objectsHash[$type] = new $className();
         }
         return self::$objectsHash[$type];
     } else {
         throw new ConfigurationException('Configuration problem with type ' . $type);
     }
 }
Example #9
0
 private function isInputField($data)
 {
     if (is_object($data)) {
         if ($data instanceof InputFieldInterface) {
             return true;
         } else {
             return TypeService::isInputType($data);
         }
     } else {
         if (!isset($data['type'])) {
             return false;
         }
         return TypeService::isInputType($data['type']);
     }
 }
Example #10
0
 public function testGetPropertyValue()
 {
     $arrayData = (new TestObjectType())->getData();
     $this->assertEquals('John', TypeService::getPropertyValue($arrayData, 'name'));
     $this->assertEquals('John', TypeService::getPropertyValue((object) $arrayData, 'name'));
 }
Example #11
0
 /**
  * @param mixed|AbstractType $type
  * @return bool
  */
 public static function isInputType($type)
 {
     if (is_object($type)) {
         $namedType = $type->getNullableType()->getNamedType();
         return $namedType instanceof AbstractScalarType || $type instanceof AbstractListType || $namedType instanceof AbstractInputObjectType || $namedType instanceof AbstractEnumType;
     } else {
         return TypeService::isScalarType($type);
     }
 }