Example #1
0
 public function __construct(array $config)
 {
     $this->name = $config['name'];
     $this->description = isset($config['description']) ? $config['description'] : null;
     $this->_config = $config;
     if (isset($config['interfaces'])) {
         InterfaceType::addImplementationToInterfaces($this);
     }
 }
Example #2
0
 public function __construct(array $config)
 {
     Utils::invariant(!empty($config['name']), 'Every type is expected to have name');
     $this->name = $config['name'];
     $this->description = isset($config['description']) ? $config['description'] : null;
     $this->resolveFieldFn = isset($config['resolveField']) ? $config['resolveField'] : null;
     $this->_config = $config;
     if (isset($config['interfaces'])) {
         InterfaceType::addImplementationToInterfaces($this);
     }
 }
Example #3
0
 public function __construct(array $config)
 {
     Config::validate($config, ['name' => Config::STRING | Config::REQUIRED, 'fields' => Config::arrayOf(FieldDefinition::getDefinition(), Config::KEY_AS_NAME), 'description' => Config::STRING, 'interfaces' => Config::arrayOf(Config::INTERFACE_TYPE), 'isTypeOf' => Config::CALLBACK]);
     $this->name = $config['name'];
     $this->description = isset($config['description']) ? $config['description'] : null;
     if (isset($config['fields'])) {
         $this->_fields = FieldDefinition::createMap($config['fields']);
     }
     $this->_interfaces = isset($config['interfaces']) ? $config['interfaces'] : [];
     $this->_isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null;
     if (!empty($this->_interfaces)) {
         InterfaceType::addImplementationToInterfaces($this, $this->_interfaces);
     }
 }
Example #4
0
 public function __construct(array $config)
 {
     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::STRING | Config::REQUIRED, 'fields' => Config::arrayOf(FieldDefinition::getDefinition(), Config::KEY_AS_NAME), 'description' => Config::STRING, 'interfaces' => Config::arrayOf(Config::INTERFACE_TYPE), '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->_isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null;
     $this->config = $config;
     if (isset($config['interfaces'])) {
         InterfaceType::addImplementationToInterfaces($this);
     }
 }
 /**
  * Generate connection field.
  *
  * @param  string $name
  * @param  ObjectType $nodeType
  * @return array
  */
 public function getInstance($name, ObjectType $nodeType)
 {
     $isConnection = $name instanceof Connection;
     $connection = new RelayConnectionType();
     $instanceName = $this->instanceName($name);
     $connectionName = !preg_match('/Connection$/', $instanceName) ? $instanceName . 'Connection' : $instanceName;
     $connection->setName(studly_case($connectionName));
     $pageInfoType = $this->getSchema()->typeInstance('pageInfo');
     $edgeType = $this->getSchema()->edgeInstance($instanceName, $nodeType);
     $connection->setEdgeType($edgeType);
     $connection->setPageInfoType($pageInfoType);
     $instance = $connection->toType();
     $field = new ConnectionField(['args' => $isConnection ? array_merge($name->args(), RelayConnectionType::connectionArgs()) : RelayConnectionType::connectionArgs(), 'type' => $instance, 'resolve' => $isConnection ? array($name, 'resolve') : null]);
     if ($connection->interfaces) {
         InterfaceType::addImplementationToInterfaces($instance);
     }
     return $field;
 }
Example #6
0
 public function type($name, $fresh = false)
 {
     if (!isset($this->types[$name])) {
         throw new \Exception('Type ' . $name . ' not found.');
     }
     if (!$fresh && isset($this->typesInstances[$name])) {
         return $this->typesInstances[$name];
     }
     /** @var Type $type */
     $type = $this->types[$name];
     $type = new $type();
     $type->setManager($this);
     $type->toType();
     $this->typesInstances[$name] = $type;
     //Check if the object has interfaces
     if ($type->interfaces) {
         InterfaceType::addImplementationToInterfaces($type->original);
     }
     return $type;
 }
 public function testRejectsWhenAPossibleTypeDoesNotImplementTheInterface()
 {
     // rejects when a possible type does not implement the interface
     $InterfaceType = new InterfaceType(['name' => 'InterfaceType', 'fields' => []]);
     $SubType = new ObjectType(['name' => 'SubType', 'fields' => [], 'interfaces' => []]);
     InterfaceType::addImplementationToInterfaces($SubType, [$InterfaceType]);
     // Sanity check.
     $this->assertEquals(1, count($InterfaceType->getPossibleTypes()));
     $this->assertEquals($SubType, $InterfaceType->getPossibleTypes()[0]);
     $schema = new Schema($InterfaceType);
     $validationResult = SchemaValidator::validate($schema, [SchemaValidator::interfacePossibleTypesMustImplementTheInterfaceRule()]);
     $this->assertSame(false, $validationResult->isValid);
     $this->assertSame(1, count($validationResult->errors));
     $this->assertSame('SubType is a possible type of interface InterfaceType but does not ' . 'implement it!', $validationResult->errors[0]->message);
 }
 public function type($name, $fresh = false)
 {
     if (!isset($this->types[$name])) {
         throw new \Exception('Type ' . $name . ' not found.');
     }
     if (!$fresh && isset($this->typesInstances[$name])) {
         return $this->typesInstances[$name];
     }
     $type = $this->types[$name];
     if (!is_object($type)) {
         $type = app($type);
     }
     $instance = $type->toType();
     $this->typesInstances[$name] = $instance;
     //Check if the object has interfaces
     if ($type->interfaces) {
         InterfaceType::addImplementationToInterfaces($instance);
     }
     return $instance;
 }
Example #9
0
 /**
  * Generate connection field.
  *
  * @param  string $name
  * @param  Closure|null $resolve
  * @return array
  */
 public function connectionField($name, $resolve = null)
 {
     $type = new RelayConnectionType();
     $connectionName = !preg_match('/Connection$/', $name) ? $name . 'Connection' : $name;
     $type->setName(studly_case($connectionName));
     $type->setEdgeType($name);
     $instance = $type->toType();
     $this->addEdge($instance, $name);
     $field = ['args' => RelayConnectionType::connectionArgs(), 'type' => $instance, 'resolve' => $resolve];
     if ($type->interfaces) {
         InterfaceType::addImplementationToInterfaces($instance);
     }
     return $field;
 }