graphql enabled and configured with existing content architecture

This commit is contained in:
2022-09-21 11:28:14 +02:00
parent a03688efa4
commit 787494bfc8
23 changed files with 1403 additions and 3 deletions

View File

@@ -0,0 +1,105 @@
scalar Violation
interface NodeInterface {
id: Int!
path: String!
}
type Concernement implements NodeInterface {
id: Int!
uuid: String!
bundle: String!
title: String!
path: String!
author: String
texte: String
entites: [Entiteintegre]
}
type Entiteintegre {
entite: Entite
rayon: Float
angle: Float
}
type Entite implements NodeInterface {
id: Int!
uuid: String!
bundle: String!
title: String!
path: String!
author: String
texte: String
images: [Image]
fichiers: [File]
liens: [Link]
}
type Static implements NodeInterface {
id: Int!
uuid: String!
bundle: String!
title: String!
path: String!
author: String
texte: String
}
type Filefield {
file: File!
description: String
}
type File {
fid: String!
uuid: String!
filename: String!
filemime: String!
filesize: String!
url: String!
}
type Taxoterm {
id: Int!
uuid: String!
name: String!
vocabulary: String
}
type VideoLink {
url: String
}
type Image {
id: Int!
url: String!
alt: String
# style_minicard: ImageStyle
# style_minicard_url: String
# style_linkedmaterialcard: ImageStyle
# style_linkedmaterialcard_url: String
# style_cardmedium: ImageStyle
# style_cardmedium_url: String
# style_cardfull: ImageStyle
# style_cardfull_url: String
# style_articlecardmedium: ImageStyle
# style_articlecardmedium_url: String
# style_hd: ImageStyle
# style_hd_url: String
}
type ImageStyle {
width: Int
height: Int
url: String
}
type Link {
url: String
title: String
}
type Date {
start: String
end: String
}

View File

@@ -0,0 +1,48 @@
# extend type Query {
# route(path: String!): NodeInterface
# }
extend type Query {
allconcernements: [Concernement]
}
extend type Query {
concernements(ids: [Int]): [Concernement]
}
extend type Query {
concernement(id: Int!): Concernement
}
extend type Query {
allentites: [Entite]
}
extend type Query {
entites(ids: [Int]): [Entite]
}
extend type Query {
entite(id: Int!): Entite
}
extend type Query {
allstatics: [Static]
}
extend type Query {
statics(ids: [Int]): [Static]
}
extend type Query {
static(id: Int!): Static
}
# extend type Query {
# alltags: [Taxoterm]
# }
# extend type Query {
# tag(id: Int!): Taxoterm
# }

View File

@@ -0,0 +1,9 @@
name: Où Atterrir Graphql
type: module
description: 'Où Atterrir GraphQL schema.'
package: Ouatterrir
core: 8.x
dependencies:
- graphql:graphql
- node:node
core_version_requirement: ^8 || ^9

View File

@@ -0,0 +1,47 @@
<?php
namespace Drupal\ouatt_graphql\Plugin\GraphQL\Schema;
use Drupal\graphql\Plugin\GraphQL\Schema\ComposableSchema;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistry;
// use Drupal\graphql\Plugin\GraphQL\Schema\SdlSchemaPluginBase;
use GraphQL\Error\Error;
/**
* @Schema(
* id = "ouatt",
* name = "Ou Atterrir schema",
* extensions = "ouatt",
* )
*/
class OuattSchema extends ComposableSchema {
// /**
// * {@inheritdoc}
// */
// public function getResolverRegistry() {
// $builder = new ResolverBuilder();
// $registry = new ResolverRegistry();
//
// // Tell GraphQL how to resolve types of a common interface.
// $registry->addTypeResolver('NodeInterface', function ($value) {
// if ($value instanceof NodeInterface) {
// switch ($value->bundle()) {
// case 'article': return 'Article';
// case 'materiau': return 'Materiau';
// }
// }
// throw new Error('Could not resolve content type.');
// });
//
// $registry->addFieldResolver('Query', 'route', $builder->compose(
// $builder->produce('route_load')
// ->map('path', $builder->fromArgument('path')),
// $builder->produce('route_entity')
// ->map('url', $builder->fromParent())
// ));
//
// return $registry;
// }
}

View File

@@ -0,0 +1,658 @@
<?php
namespace Drupal\ouatt_graphql\Plugin\GraphQL\SchemaExtension;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\GraphQL\Response\ResponseInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
use Drupal\materio_graphql\GraphQL\Response\MaterioResponse;
use Drupal\Core\Url;
use GraphQL\Error\Error;
/**
* @SchemaExtension(
* id = "ouatt_extension",
* name = "Ou Atterrir extension",
* description = "Ou Atterrir extension.",
* schema = "ouatt"
* )
*/
class OuattSchemaExtension extends SdlSchemaExtensionPluginBase {
/**
* {@inheritdoc}
*/
public function registerResolvers(ResolverRegistryInterface $registry) {
$builder = new ResolverBuilder();
$this->addRouteResolver($registry, $builder);
//
$this->addConcernements($registry, $builder);
//
$this->addEntitesIntegres($registry, $builder);
//
$this->addEntites($registry, $builder);
//
$this->addStatic($registry, $builder);
//
$this->addFilefield($registry, $builder);
//
$this->addFile($registry, $builder);
//
$this->addDate($registry, $builder);
//
$this->addVideolink($registry, $builder);
//
$this->addImage($registry, $builder);
//
// $this->addTaxoterm($registry, $builder);
//
$this->addLink($registry, $builder);
}
// ___ _
// | _ \___ _ _| |_ ___
// | / _ \ || | _/ -_)
// |_|_\___/\_,_|\__\___|
protected function addRouteResolver(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
// Tell GraphQL how to resolve types of a common interface.
$registry->addTypeResolver('NodeInterface', function ($value) {
$path = explode('\\', get_class($value));
$class = array_pop($path);
if ($class === 'Node') {
switch ($value->bundle()) {
case 'concernement': return 'Concernement';
case 'entite': return 'Entite';
}
}
throw new Error('Could not resolve content type.');
});
$registry->addFieldResolver('Query', 'route',
$builder->compose(
$builder->produce('route_load')
->map('path', $builder->fromArgument('path')),
$builder->produce('route_entity')
->map('url', $builder->fromParent())
));
}
// ____ _
// / ___|___ _ __ ___ ___ _ __ _ __ ___ _ __ ___ ___ _ __ | |_ ___
// | | / _ \| '_ \ / __/ _ \ '__| '_ \ / _ \ '_ ` _ \ / _ \ '_ \| __/ __|
// | |__| (_) | | | | (_| __/ | | | | | __/ | | | | | __/ | | | |_\__ \
// \____\___/|_| |_|\___\___|_| |_| |_|\___|_| |_| |_|\___|_| |_|\__|___/
protected function addConcernements(ResolverRegistryInterface $registry, ResolverBuilder $builder){
$registry->addFieldResolver('Query', 'allconcernements',
$builder->compose(
$builder->callback(function($parent, $arg){
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
->condition('type', ['concernement'], 'IN')
->accessCheck(TRUE);
$results = $query->execute();
return $results;
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromParent())
)
);
$registry->addFieldResolver('Query', 'concernements',
$builder->compose(
$builder->callback(function($parent, $arg){
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
->condition('type', ['concernement'], 'IN')
->accessCheck(TRUE);
$results = $query->execute();
return $results;
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromArgument('ids'))
)
);
$registry->addFieldResolver('Query', 'concernement',
$builder->produce('entity_load')
->map('type', $builder->fromValue('node'))
->map('id', $builder->fromArgument('id'))
);
$registry->addFieldResolver('Concernement', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Concernement', 'uuid',
$builder->produce('entity_uuid')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Concernement', 'path',
$builder->compose(
$builder->produce('entity_url')
->map('entity', $builder->fromParent()),
$builder->produce('url_path')
->map('url', $builder->fromParent())
)
);
$registry->addFieldResolver('Concernement', 'title',
$builder->compose(
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Concernement', 'bundle',
$builder->compose(
$builder->produce('entity_bundle')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Concernement', 'texte',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('body.value'))
);
$registry->addFieldResolver('Concernement', 'author',
$builder->compose(
$builder->produce('entity_owner')
->map('entity', $builder->fromParent()),
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Concernement', 'entites',
$builder->produce('entity_reference_revisions')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_entite'))
);
}
// _____ _ _ _ __ ___ _ __ __
// | ____|_ __ | |_(_) |_ /_/ ___ |_ _|_ __ | |_ /_/ __ _ _ __ /_/ ___ ___
// | _| | '_ \| __| | __/ _ \/ __| | || '_ \| __/ _ \/ _` | '__/ _ \/ _ \/ __|
// | |___| | | | |_| | || __/\__ \ | || | | | || __/ (_| | | | __/ __/\__ \
// |_____|_| |_|\__|_|\__\___||___/ |___|_| |_|\__\___|\__, |_| \___|\___||___/
// |___/
protected function addEntitesIntegres(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Entiteintegre', 'entite',
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_entite')),
$builder->callback(function ($items) {
return $items[0];
})
));
$registry->addFieldResolver('Entiteintegre', 'rayon',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:paragraph'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_rayon.value'))
);
$registry->addFieldResolver('Entiteintegre', 'angle',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:paragraph'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_angle.value'))
);
}
// _____ _ _ _
// | ____|_ __ | |_(_) |_ ___ ___
// | _| | '_ \| __| | __/ _ \/ __|
// | |___| | | | |_| | || __/\__ \
// |_____|_| |_|\__|_|\__\___||___/
protected function addEntites(ResolverRegistryInterface $registry, ResolverBuilder $builder){
$registry->addFieldResolver('Query', 'allentites',
$builder->compose(
$builder->callback(function($parent, $arg){
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
->condition('type', ['entite'], 'IN')
->accessCheck(TRUE);
$results = $query->execute();
return $results;
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromParent())
)
);
$registry->addFieldResolver('Query', 'entites',
$builder->compose(
$builder->callback(function($parent, $arg){
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
->condition('type', ['entite'], 'IN')
->accessCheck(TRUE);
$results = $query->execute();
return $results;
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromArgument('ids'))
)
);
$registry->addFieldResolver('Query', 'entite',
$builder->produce('entity_load')
->map('type', $builder->fromValue('node'))
->map('id', $builder->fromArgument('id'))
);
$registry->addFieldResolver('Entite', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Entite', 'uuid',
$builder->produce('entity_uuid')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Entite', 'path',
$builder->compose(
$builder->produce('entity_url')
->map('entity', $builder->fromParent()),
$builder->produce('url_path')
->map('url', $builder->fromParent())
)
);
$registry->addFieldResolver('Entite', 'title',
$builder->compose(
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Entite', 'bundle',
$builder->compose(
$builder->produce('entity_bundle')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Entite', 'texte',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('body.value'))
);
$registry->addFieldResolver('Entite', 'author',
$builder->compose(
$builder->produce('entity_owner')
->map('entity', $builder->fromParent()),
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Entite', 'images',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_images'))
);
$registry->addFieldResolver('Entite', 'videos',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_videos'))
);
$registry->addFieldResolver('Entite', 'fichiers',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_fichiers'))
);
$registry->addFieldResolver('Entite', 'liens',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_liens'))
);
}
// ___ _ _ _
// / __| |_ __ _| |_(_)__ ___
// \__ \ _/ _` | _| / _(_-<
// |___/\__\__,_|\__|_\__/__/
protected function addStatic(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Query', 'allstatics',
$builder->compose(
$builder->callback(function($parent, $arg){
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
->condition('type', ['static'], 'IN')
->accessCheck(TRUE);
$results = $query->execute();
return $results;
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromParent())
)
);
$registry->addFieldResolver('Query', 'statics',
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('ids', $builder->fromArgument('ids'))
);
$registry->addFieldResolver('Query', 'static',
$builder->produce('entity_load')
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['static']))
->map('id', $builder->fromArgument('id'))
);
$registry->addFieldResolver('Static', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Static', 'uuid',
$builder->produce('entity_uuid')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Static', 'path',
$builder->compose(
$builder->produce('entity_url')
->map('entity', $builder->fromParent()),
$builder->produce('url_path')
->map('url', $builder->fromParent())
)
);
$registry->addFieldResolver('Static', 'title',
$builder->compose(
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Static', 'bundle',
$builder->compose(
$builder->produce('entity_bundle')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Static', 'texte',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('body.value'))
);
$registry->addFieldResolver('Static', 'author',
$builder->compose(
$builder->produce('entity_owner')
->map('entity', $builder->fromParent()),
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Static', 'fichiers',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_fichier'))
);
}
// ___ _
// | \ __ _| |_ ___
// | |) / _` | _/ -_)
// |___/\__,_|\__\___|
protected function addDate(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Date', 'start',
$builder->callback(function ($parent, $args) {
return isset($parent[0]) ? $parent[0]['value'] : null;
})
);
$registry->addFieldResolver('Date', 'end',
$builder->callback(function ($parent, $args) {
return isset($parent[0]) ? $parent[0]['end_value'] : null;
})
);
}
// ___ _ _ __ _ _ _
// | __(_) |___ / _(_)___| |__| |
// | _|| | / -_) _| / -_) / _` |
// |_| |_|_\___|_| |_\___|_\__,_|
protected function addFilefield(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Filefield', 'description',
$builder->callback(function ($parent, $args) {
return $parent['description'];
})
);
$registry->addFieldResolver('Filefield', 'file',
$builder->callback(function ($parent, $args) {
return \Drupal\file\Entity\File::load($parent['target_id']);
})
);
}
// ___ _ _
// | __(_) |___
// | _|| | / -_)
// |_| |_|_\___|
protected function addFile(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('File', 'url',
$builder->callback(function ($parent, $args) {
return $parent->createFileUrl();
})
);
$registry->addFieldResolver('File', 'filesize',
$builder->callback(function ($parent, $args) {
return $parent->getSize();
})
);
$registry->addFieldResolver('File', 'filemime',
$builder->callback(function ($parent, $args) {
return $parent->getMimeType();
})
);
$registry->addFieldResolver('File', 'filename',
$builder->callback(function ($parent, $args) {
return $parent->getFilename();
})
);
$registry->addFieldResolver('File', 'fid',
$builder->callback(function ($parent, $args) {
return $parent->id();
})
);
$registry->addFieldResolver('File', 'uuid',
$builder->callback(function ($parent, $args) {
return $parent->uuid();
})
);
}
// ___
// |_ _|_ __ __ _ __ _ ___
// | || ' \/ _` / _` / -_)
// |___|_|_|_\__,_\__, \___|
// |___/
protected function addImage(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Image', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Image', 'url',
$builder->produce('image_url')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Image', 'alt',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_image.alt'))
);
$registry->addFieldResolver('Image', 'style_minicard',
$builder->produce('image_derivative')
->map('entity', $builder->fromParent())
->map('style', $builder->fromValue('card_medium_half'))
);
$registry->addFieldResolver('Image', 'style_minicard_url',
$builder->compose(
$builder->produce('image_derivative')
->map('entity', $builder->fromParent())
->map('style', $builder->fromValue('card_medium_half')),
$builder->callback(function($parent, $args){
return $parent['url'];
})
));
}
// __ ___ _ _ _ _
// \ \ / (_)__| |___ ___| | (_)_ _ | |__
// \ V /| / _` / -_) _ \ |__| | ' \| / /
// \_/ |_\__,_\___\___/____|_|_||_|_\_\
protected function addVideolink(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('VideoLink', 'url',
$builder->produce('property_path')
->map('type', $builder->fromValue('field_item:video_embed_field'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('value'))
);
}
// _____
// |_ _|_ _ __ _
// | |/ _` / _` |
// |_|\__,_\__, |
// |___/
// protected function addTaxoterm(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
// $registry->addFieldResolver('Query', 'alltags',
// $builder->compose(
// $builder->callback(function($parent, $arg){
// $entity_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
// $query = $entity_storage->getQuery()
// ->condition('vid', ['tag','tag_prod'], 'IN')
// ->accessCheck(TRUE);
// $results = $query->execute();
// return $results;
// }),
// $builder->produce('entity_load_multiple')
// ->map('type', $builder->fromValue('taxonomy_term'))
// ->map('ids', $builder->fromParent())
// )
// );
// $registry->addFieldResolver('Query', 'tag',
// $builder->produce('entity_load')
// ->map('type', $builder->fromValue('taxonomy_term'))
// ->map('bundles', $builder->fromValue(['tag']))
// ->map('id', $builder->fromArgument('id'))
// );
// $registry->addFieldResolver('Query', 'famille',
// $builder->produce('entity_load')
// ->map('type', $builder->fromValue('taxonomy_term'))
// ->map('bundles', $builder->fromValue(['famille']))
// ->map('id', $builder->fromArgument('id'))
// );
// $registry->addFieldResolver('Query', 'tagprod',
// $builder->produce('entity_load')
// ->map('type', $builder->fromValue('taxonomy_term'))
// ->map('bundles', $builder->fromValue(['tag_prod']))
// ->map('id', $builder->fromArgument('id'))
// );
// // get all tags for a given familly
// // $registry->addFieldResolver('Query', 'famillytags',
// // $builder->compose(
// // $builder->callback(function($parent, $arg){
// // $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
// // $query = $entity_storage->getQuery()
// // ->condition('type', ['texte', 'texte_prod'], 'IN')
// // ->condition('field_famille', $arg->famillyid)
// // ->accessCheck(TRUE);
// // $results = $query->execute();
// // return $results;
// // }),
// // $builder->produce('entity_load_multiple')
// // ->map('type', $builder->fromValue('node'))
// // ->map('ids', $builder->fromParent())
// // // $builder->produce('entity_load_multiple')
// // // ->map('type', $builder->fromValue('taxonomy_term'))
// // // ->map('ids', $builder->fromParent())
// // )
// // );
// $registry->addFieldResolver('Taxoterm', 'id',
// $builder->produce('entity_id')
// ->map('entity', $builder->fromParent())
// );
// $registry->addFieldResolver('Taxoterm', 'uuid',
// $builder->produce('entity_uuid')
// ->map('entity', $builder->fromParent())
// );
// $registry->addFieldResolver('Taxoterm', 'name',
// $builder->produce('entity_label')
// ->map('entity', $builder->fromParent())
// );
// $registry->addFieldResolver('Taxoterm', 'vocabulary',
// $builder->produce('entity_bundle')
// ->map('entity', $builder->fromParent())
// );
// }
// _ _ _
// | | (_)_ _ | |__
// | |__| | ' \| / /
// |____|_|_||_|_\_\
protected function addLink(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Link', 'url',
$builder->callback(function ($parent, $args) {
return isset($parent[0]) ? $parent[0]['uri'] : null;
})
);
$registry->addFieldResolver('Link', 'title',
$builder->callback(function ($parent, $args) {
return isset($parent[0]) ? $parent[0]['title'] : null;
})
);
}
}