|
|
|
@ -22,10 +22,49 @@ class SchemaSPIP {
|
|
|
|
|
public array $metas_autorisees = [];
|
|
|
|
|
|
|
|
|
|
private array $types = [];
|
|
|
|
|
private InterfaceType $interface;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->collections_autorisees = lire_config('/meta_graphql/objets_editoriaux', []);
|
|
|
|
|
$this->metas_autorisees = lire_config('/meta_graphql/meta', []);
|
|
|
|
|
|
|
|
|
|
$champs_interface = [];
|
|
|
|
|
foreach (GRAPHQL_CHAMPS_COMMUNS as $champ) {
|
|
|
|
|
switch ($champ) {
|
|
|
|
|
case 'id':
|
|
|
|
|
$champs_interface[$champ] = Type::id();
|
|
|
|
|
break;
|
|
|
|
|
case 'maj':
|
|
|
|
|
$champs_interface[$champ] = $this->get('Date');
|
|
|
|
|
break;
|
|
|
|
|
case 'typeCollection':
|
|
|
|
|
$champs_interface[$champ] = $this->get('Collection');
|
|
|
|
|
break;
|
|
|
|
|
case 'rang':
|
|
|
|
|
case 'points':
|
|
|
|
|
$champs_interface[$champ] = Type::int();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$champs_interface[$champ] = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->interface = new InterfaceType([
|
|
|
|
|
'name' => 'Objet',
|
|
|
|
|
'description' => _T('graphql:desc_type_objet'),
|
|
|
|
|
'fields' => $champs_interface,
|
|
|
|
|
'resolveType' => function ($value, $context, ResolveInfo $info): ObjectType {
|
|
|
|
|
// switch ($value->type ?? null) {
|
|
|
|
|
// case 'human':
|
|
|
|
|
// return MyTypes::human();
|
|
|
|
|
// case 'droid':
|
|
|
|
|
// return MyTypes::droid();
|
|
|
|
|
// // default: throw new Exception("Unknown Character type: {$value->type ?? null}");
|
|
|
|
|
// }
|
|
|
|
|
return new ObjectType([]);
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query, Date, MetaList, Article, SearchResult...
|
|
|
|
@ -38,25 +77,6 @@ class SchemaSPIP {
|
|
|
|
|
$typeDefinition = ['name' => $name];
|
|
|
|
|
|
|
|
|
|
switch ($name) {
|
|
|
|
|
case 'Node':
|
|
|
|
|
// Interface Objet pour mutualiser des champs entre objets
|
|
|
|
|
$typeDefinition['description'] = _T('graphql:desc_type_base');
|
|
|
|
|
|
|
|
|
|
$typeDefinition['fields'] = function () {
|
|
|
|
|
return $this->champsCommuns(GRAPHQL_CHAMPS_COMMUNS);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$typeDefinition['resolveType'] = function ($value, $context, ResolveInfo $info) {
|
|
|
|
|
// TODO : écrire le resolveType
|
|
|
|
|
// switch ($info->fieldDefinition->name ?? null) {
|
|
|
|
|
// case 'human': return MyTypes::human();
|
|
|
|
|
// case 'droid': return MyTypes::droid();
|
|
|
|
|
// default: throw new Exception("Unknown Character type: {$value->type ?? null}");
|
|
|
|
|
// }
|
|
|
|
|
};
|
|
|
|
|
return new InterfaceType($typeDefinition);
|
|
|
|
|
break;
|
|
|
|
|
break;
|
|
|
|
|
case 'Collection':
|
|
|
|
|
// Liste des collections exposées
|
|
|
|
|
$typeDefinition['description'] = _T('graphql:desc_type_collection');
|
|
|
|
@ -89,29 +109,6 @@ class SchemaSPIP {
|
|
|
|
|
};
|
|
|
|
|
return new ObjectType($typeDefinition);
|
|
|
|
|
break;
|
|
|
|
|
case 'Objet':
|
|
|
|
|
// Interface Objet pour mutualiser des champs entre objets
|
|
|
|
|
$typeDefinition['description'] = _T('graphql:desc_type_objet');
|
|
|
|
|
|
|
|
|
|
$typeDefinition['fields'] = function () {
|
|
|
|
|
return $this->champsCommuns(
|
|
|
|
|
array_merge(
|
|
|
|
|
GRAPHQL_CHAMPS_COMMUNS,
|
|
|
|
|
GRAPHQL_CHAMPS_INTERFACE_OBJET
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$typeDefinition['resolveType'] = function ($value, $context, ResolveInfo $info) {
|
|
|
|
|
// TODO : écrire le resolveType
|
|
|
|
|
// switch ($info->fieldDefinition->name ?? null) {
|
|
|
|
|
// case 'human': return MyTypes::human();
|
|
|
|
|
// case 'droid': return MyTypes::droid();
|
|
|
|
|
// default: throw new Exception("Unknown Character type: {$value->type ?? null}");
|
|
|
|
|
// }
|
|
|
|
|
};
|
|
|
|
|
return new InterfaceType($typeDefinition);
|
|
|
|
|
break;
|
|
|
|
|
case 'Query':
|
|
|
|
|
// Les requêtes disponibles
|
|
|
|
|
$typeDefinition['description'] = _T('graphql:desc_type_query');
|
|
|
|
@ -145,19 +142,15 @@ class SchemaSPIP {
|
|
|
|
|
|
|
|
|
|
if (array_key_exists($collection, $this->collections_autorisees)) {
|
|
|
|
|
$typeDefinition['description'] = _T('graphql:desc_type_objet') . ' ' . $name;
|
|
|
|
|
$typeDefinition['interfaces'] = [$this->interface];
|
|
|
|
|
|
|
|
|
|
$typeDefinition['fields'] = function () use (&$collectionType, $collection): array {
|
|
|
|
|
// On récupère les champs partagés
|
|
|
|
|
$graphQLfields = $this->champsCommuns(GRAPHQL_CHAMPS_COMMUNS);
|
|
|
|
|
$typeDefinition['interfaces'] = [$this->get('Node')];
|
|
|
|
|
|
|
|
|
|
if (!in_array($collection, GRAPHQL_COLLECTIONS_NON_OBJET)) {
|
|
|
|
|
$typeDefinition['interfaces'][] = $this->get('Objet');
|
|
|
|
|
|
|
|
|
|
$graphQLfields = array_merge(
|
|
|
|
|
$graphQLfields,
|
|
|
|
|
$this->champsCommuns(GRAPHQL_CHAMPS_INTERFACE_OBJET)
|
|
|
|
|
);
|
|
|
|
|
// On récupère les champs partagés
|
|
|
|
|
// $graphQLfields = $this->champsCommuns(GRAPHQL_CHAMPS_COMMUNS);
|
|
|
|
|
$graphQLfields = [];
|
|
|
|
|
foreach (GRAPHQL_CHAMPS_COMMUNS as $champ_commun) {
|
|
|
|
|
$graphQLfields[] = $this->interface->getField($champ_commun);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// On récupère les champs autorisés de la collection
|
|
|
|
@ -166,7 +159,6 @@ class SchemaSPIP {
|
|
|
|
|
$info_champs = $collection_infos['champs'];
|
|
|
|
|
foreach ($this->collections_autorisees[$collection]['champs'] as $champ) {
|
|
|
|
|
$fieldType = null;
|
|
|
|
|
$isString = false;
|
|
|
|
|
$typeSQL = $info_champs[$champ];
|
|
|
|
|
|
|
|
|
|
switch (true) {
|
|
|
|
@ -182,10 +174,6 @@ class SchemaSPIP {
|
|
|
|
|
case (in_array($champ, ['id_trad', 'id_parent'])):
|
|
|
|
|
$fieldType = $collectionType;
|
|
|
|
|
break;
|
|
|
|
|
case ($champ == 'id_vignette'):
|
|
|
|
|
if (array_key_exists('documents', $this->collections_autorisees))
|
|
|
|
|
$fieldType = $this->get('Document');
|
|
|
|
|
break;
|
|
|
|
|
// Ensuite, on gère les types scalaires
|
|
|
|
|
case (stripos($typeSQL, 'double') !== false):
|
|
|
|
|
$fieldType = Type::float();
|
|
|
|
@ -199,21 +187,12 @@ class SchemaSPIP {
|
|
|
|
|
case (stripos($typeSQL, 'datetime') !== false):
|
|
|
|
|
$fieldType = $this->get('Date');
|
|
|
|
|
break;
|
|
|
|
|
// case (stripos($typeSQL, 'char') !== false):
|
|
|
|
|
// case (stripos($typeSQL, 'longtext') !== false):
|
|
|
|
|
// case (stripos($typeSQL, 'mediumtext') !== false):
|
|
|
|
|
// case (stripos($typeSQL, 'tinytext') !== false):
|
|
|
|
|
// case (stripos($typeSQL, 'text') !== false):
|
|
|
|
|
default:
|
|
|
|
|
$isString = true;
|
|
|
|
|
$fieldType = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_null($fieldType)) {
|
|
|
|
|
// $fieldType = (strpos($typeSQL, 'NOT NULL') && !$isString) ?
|
|
|
|
|
// new NonNull($fieldType) :
|
|
|
|
|
// $fieldType;
|
|
|
|
|
$champ = preg_replace('#(id_)#', '', $champ);
|
|
|
|
|
$graphQLfields[$champ] = $fieldType;
|
|
|
|
|
}
|
|
|
|
@ -262,8 +241,6 @@ class SchemaSPIP {
|
|
|
|
|
return $graphQLfields;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$collectionType = new ObjectType($typeDefinition);
|
|
|
|
|
|
|
|
|
|
return $collectionType;
|
|
|
|
@ -352,34 +329,6 @@ class SchemaSPIP {
|
|
|
|
|
return $queryFields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retourne un tableau de champs avec leur type graphQL à partir d'une liste de champs
|
|
|
|
|
// Utilisé avec les constantes GRAPHQL_CHAMPS_COMMUNS et GRAPHQL_CHAMPS_INTERFACE_OBJET
|
|
|
|
|
// pour générer la liste des champs en communs
|
|
|
|
|
private function champsCommuns(array $champs_options): array {
|
|
|
|
|
foreach ($champs_options as $champ) {
|
|
|
|
|
switch ($champ) {
|
|
|
|
|
case 'id':
|
|
|
|
|
$champs[$champ] = Type::id();
|
|
|
|
|
break;
|
|
|
|
|
case 'maj':
|
|
|
|
|
$champs[$champ] = $this->get('Date');
|
|
|
|
|
break;
|
|
|
|
|
case 'typeCollection':
|
|
|
|
|
$champs[$champ] = $this->get('Collection');
|
|
|
|
|
break;
|
|
|
|
|
case 'rang':
|
|
|
|
|
case 'points':
|
|
|
|
|
$champs[$champ] = Type::int();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$champs[$champ] = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $champs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function collectionArgs(int $pagination) {
|
|
|
|
|
return [
|
|
|
|
|
'where' => [
|
|
|
|
|