private function getExceptedSchemaContainer() { if (self::$expectedSchema !== null) { return self::$expectedSchema; } $idField = new Field(); $idField->setName('id')->setDescription('The user primary key')->setType('Int'); $nameField = new Field(); $nameField->setName('name')->setDescription('The user name')->setType('String'); $emailField = new Field(); $emailField->setName('email')->setDescription('The user email')->setType('String'); $friendsField = new Field(); $friendsField->setName('friends')->setDescription('The user friends')->setType('[User]')->setResolveConfig('AppBundle\\Entity\\Friend'); $userType = new Type(); $userType->setName('User')->setDescription('User type description')->setExtends('Item')->setFields([$idField, $nameField, $emailField, $friendsField]); $idField = new Field(); $idField->setName('id')->setDescription('The item primary key')->setType('Int'); $nameField = new Field(); $nameField->setName('name')->setDescription('The item name')->setType('String'); $interface = new InterfaceType(); $interface->setName('Item')->setDescription('Item interface description')->setFields([$idField, $nameField]); $idArg = new Field(); $idArg->setName('id')->setDescription('The ID')->setType('Int'); $adminField = new Field(); $adminField->setName('admin')->setDescription('Admin description')->setType('[User]')->setResolveConfig('AppBundle\\Entity\\User')->setArguments([$idArg]); $idArg = clone $idArg; $userField = new Field(); $userField->setName('user')->setDescription('User description')->setType('User')->setResolveConfig('AppBundle\\Entity\\User')->setArguments([$idArg]); $query = new Query(); $query->setDescription('The root query description')->setFields([$adminField, $userField]); $schema = new SchemaContainer(); $schema->addType($userType)->addInterface($interface)->setQuerySchema($query); return self::$expectedSchema = $schema; }
/** * Apply the resolve config of types that are used by query fields * * @param SchemaContainer $schemaContainer * @param Field $field */ private function mergeResolveConfig(SchemaContainer $schemaContainer, Field $field) { $typeName = TypeParser::getFinalType($field->getType()); if ($schemaContainer->hasType($typeName)) { $typeConfig = $schemaContainer->getType($typeName)->getResolveConfig(); } elseif ($schemaContainer->hasInterface($typeName)) { $typeConfig = $schemaContainer->getInterface($typeName)->getResolveConfig(); } else { return; } $field->mergeResolveConfig($typeConfig); }
/** * @param string $name * @param array $mapping * @return Field */ private function createField($name, array $mapping) { $field = new Field(); $field->setName($name)->setType(isset($mapping['type']) ? $mapping['type'] : null)->setProperty(isset($mapping['property']) ? $mapping['property'] : null)->setResolveConfig(isset($mapping['resolve']) ? $mapping['resolve'] : []); $this->populateType($field, $mapping); if (isset($mapping['args'])) { $args = []; foreach ($mapping['args'] as $argName => $argMapping) { $args[] = $this->createField($argName, $argMapping); } $field->setArguments($args); } return $field; }
/** * @return Field */ private function createAppearsInField() { $field = new Field(); $field->setName('appearsIn')->setDescription('Which movies they appear in.')->setType('[Episode]'); return $field; }
/** * @param Field $field */ private function prepareResolver(Field $field) { $resolveConfig = $field->getResolveConfig(); if (isset($resolveConfig['handler'])) { $handler = $resolveConfig['handler']; if (!isset($this->resolveFactories[$handler])) { throw new \Exception(sprintf('Handle named "%s" does not exist', $resolveConfig['handler'])); } $resolver = $this->resolveFactories[$handler]->getFunction($resolveConfig, $field); $field->setResolve($resolver); } }
/** * @param string $className * @param Field $field * @return string|null */ private function getAccessor($className, Field $field) { $class = new \ReflectionClass($className); $property = $field->getField() ?: $field->getName(); $camelName = String::camelize($property); $getter = 'get' . $camelName; $getsetter = lcfirst($camelName); $isser = 'is' . $camelName; $hasser = 'has' . $camelName; $test = [$getter, $getsetter, $isser, $hasser]; $reflMethods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); $methods = []; foreach ($reflMethods as $reflMethod) { $methods[$reflMethod->getName()] = true; } foreach ($test as $method) { if (isset($methods[$method])) { return $method; } } }
/** * @param ClassMetadataInfo $metadata * @param Field $field * @param SchemaContainer $schemaContainer * @return TypeGuess * @throws MappingException */ private function guessAssociation(ClassMetadataInfo $metadata, Field $field, SchemaContainer $schemaContainer) { $property = $field->getProperty() ?: $field->getName(); $multiple = $metadata->isCollectionValuedAssociation($property); $mapping = $metadata->getAssociationMapping($property); foreach ($schemaContainer->getTypes() as $type) { $containerContext = new ContainerContext($type, $schemaContainer); if (!$this->isFieldContainerSupported($containerContext)) { continue; } if ($type->getModel() === $mapping['targetEntity']) { $typeName = $type->getName(); if ($multiple) { $typeName = sprintf('[%s]', $typeName); } return new TypeGuess($typeName, Guess::HIGH_CONFIDENCE); } } }