improving graphql defs, refactoring graphql defs

This commit is contained in:
Bachir Soussi Chiadmi 2020-12-22 18:17:02 +01:00
parent f39079c616
commit 5deb568786
3 changed files with 425 additions and 155 deletions

View File

@ -15,8 +15,8 @@ type Materiau {
thesaurus: [Thesaurus]
memo: String
attachments: [Filefield]
# distributor:
# manufacturer:
distributor: [Company]
manufacturer: [Company]
# famille:
# index:
# reference:
@ -69,6 +69,21 @@ type Showroom {
# phone: String
}
type Company {
id: Int!
uuid: String!
name: String!
attachments: [Filefield]
memo: String
note: Int
website: Link
departement: String
email: String
address: Address
# infos
# phone
}
type Tag {
id: Int!
uuid: String!
@ -81,6 +96,23 @@ type Thesaurus {
name: String!
}
type Address {
langcode: String
country_code: String
administrative_area: String
locality: String
dependent_locality: String
postal_code: String
sorting_code: String
address_line1: String
address_line2: String
organization: String
given_name: String
additional_name: String
family_name: String
}
type Date {
start: String
end: String

View File

@ -17,3 +17,7 @@ extend type Query {
extend type Query {
thesaurus(id: Int!): Thesaurus
}
extend type Query {
company(id: Int!): Company
}

View File

@ -24,121 +24,7 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
public function registerResolvers(ResolverRegistryInterface $registry) {
$builder = new ResolverBuilder();
// __ __ _ _
// | \/ |__ _| |_ ___ _ _(_)__ _ _ _
// | |\/| / _` | _/ -_) '_| / _` | || |
// |_| |_\__,_|\__\___|_| |_\__,_|\_,_|
$registry->addFieldResolver('Query', 'materiau',
$builder->produce('entity_load')
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['materiau']))
->map('id', $builder->fromArgument('id'))
);
$registry->addFieldResolver('Materiau', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Materiau', 'uuid',
$builder->produce('entity_uuid')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Materiau', 'title',
$builder->compose(
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Materiau', 'body',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('body.value'))
);
$registry->addFieldResolver('Materiau', 'short_description',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_short_description.value'))
);
$registry->addFieldResolver('Materiau', 'author',
$builder->compose(
$builder->produce('entity_owner')
->map('entity', $builder->fromParent()),
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
// https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md
// https://blog.chrismitchellonline.com/posts/custom_graphql_data/
$registry->addFieldResolver('Materiau', 'linked_materials',
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_materials'))
));
$registry->addFieldResolver('Materiau', 'linked_articles',
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_articles'))
));
$registry->addFieldResolver('Materiau', 'memo',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_memo.value'))
);
$registry->addFieldResolver('Materiau', 'images',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_materiau_images'))
);
$registry->addFieldResolver('Materiau', 'attachments',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_attachments'))
// $builder->produce('entity_reference')
// ->map('entity', $builder->fromParent())
// ->map('field', $builder->fromValue('field_attachments'))
);
$registry->addFieldResolver('Materiau', 'videos',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_video'))
);
$registry->addFieldResolver('Materiau', 'tags',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_tags'))
);
$registry->addFieldResolver('Materiau', 'thesaurus',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_thesaurus'))
);
// field_distributor
// field_famille
// field_index
// field_manufacturer
// field_reference
// field_samples
$this->addMateriau($registry, $builder);
// _ _ _ _
// /_\ _ _| |_(_)__| |___
@ -246,25 +132,217 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
);
// ___ _ _
// | __(_) |___
// | _|| | / -_)
// |_| |_|_\___|
// ___
// / __|___ _ __ _ __ __ _ _ _ _ _
// | (__/ _ \ ' \| '_ \/ _` | ' \ || |
// \___\___/_|_|_| .__/\__,_|_||_\_, |
// |_| |__/
$registry->addFieldResolver('Query', 'company',
$builder->produce('entity_load')
->map('type', $builder->fromValue('taxonomy_term'))
->map('bundles', $builder->fromValue(['company']))
->map('id', $builder->fromArgument('id'))
);
$registry->addFieldResolver('Company', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Company', 'uuid',
$builder->produce('entity_uuid')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Company', 'name',
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
);
// TODO: property_path helper
// $props = [
// [
// "res" => "attachments",
// "type" => "entity:taxonomy_term",
// "path" => "field_attachments",
// ]
// ]
// foreach ($props as $key => $value) {
// $registry->addFieldResolver('Company', $value['res'],
// $builder->produce('property_path')
// ->map('type', $builder->fromValue('entity:taxonomy_term'))
// ->map('value', $builder->fromParent())
// ->map('path', $builder->fromValue('field_attachments'))
// );
// }
$registry->addFieldResolver('Company', 'attachments',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:taxonomy_term'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_attachments'))
);
$registry->addFieldResolver('Company', 'memo',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:taxonomy_term'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_memo.value'))
);
$registry->addFieldResolver('Company', 'note',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:taxonomy_term'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_note.value'))
);
$registry->addFieldResolver('Company', 'website',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:taxonomy_term'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_website'))
);
$registry->addFieldResolver('Company', 'departement',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:taxonomy_term'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_departement.value'))
);
$registry->addFieldResolver('Company', 'email',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:taxonomy_term'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_public_email.value'))
);
$registry->addFieldResolver('Company', 'address',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:taxonomy_term'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_public_address'))
);
// field_infos_from_company
// field_public_phone
// _ _ _
// /_\ __| |__| |_ _ ___ ______
// / _ \/ _` / _` | '_/ -_|_-<_-<
// /_/ \_\__,_\__,_|_| \___/__/__/
// $addr_fields = [
// 'langcode',
// 'country_code',
// 'administrative_area',
// 'locality',
// 'dependent_locality',
// 'postal_code',
// 'sorting_code',
// 'address_line1',
// 'address_line2',
// 'organization',
// 'given_name',
// 'additional_name',
// 'family_name'
// ];
//
// foreach ($addr_fields as $key => $value) {
// $registry->addFieldResolver('Address', $value,
// $builder->callback(function ($parent, $args) {
// return $parent[0][$value];
// })
// );
// }
$registry->addFieldResolver('Address', 'langcode',
$builder->callback(function ($parent, $args) {
return $parent[0]['langcode'];
})
);
$registry->addFieldResolver('Address', 'country_code',
$builder->callback(function ($parent, $args) {
return $parent[0]['country_code'];
})
);
$registry->addFieldResolver('Address', 'administrative_area',
$builder->callback(function ($parent, $args) {
return $parent[0]['administrative_area'];
})
);
$registry->addFieldResolver('Address', 'locality',
$builder->callback(function ($parent, $args) {
return $parent[0]['locality'];
})
);
$registry->addFieldResolver('Address', 'dependent_locality',
$builder->callback(function ($parent, $args) {
return $parent[0]['dependent_locality'];
})
);
$registry->addFieldResolver('Address', 'postal_code',
$builder->callback(function ($parent, $args) {
return $parent[0]['postal_code'];
})
);
$registry->addFieldResolver('Address', 'sorting_code',
$builder->callback(function ($parent, $args) {
return $parent[0]['sorting_code'];
})
);
$registry->addFieldResolver('Address', 'address_line1',
$builder->callback(function ($parent, $args) {
return $parent[0]['address_line1'];
})
);
$registry->addFieldResolver('Address', 'address_line2',
$builder->callback(function ($parent, $args) {
return $parent[0]['address_line2'];
})
);
$registry->addFieldResolver('Address', 'organization',
$builder->callback(function ($parent, $args) {
return $parent[0]['organization'];
})
);
$registry->addFieldResolver('Address', 'given_name',
$builder->callback(function ($parent, $args) {
return $parent[0]['given_name'];
})
);
$registry->addFieldResolver('Address', 'additional_name',
$builder->callback(function ($parent, $args) {
return $parent[0]['additional_name'];
})
);
$registry->addFieldResolver('Address', 'family_name',
$builder->callback(function ($parent, $args) {
return $parent[0]['family_name'];
})
);
// ___ _ _ __ _ _ _
// | __(_) |___ / _(_)___| |__| |
// | _|| | / -_) _| / -_) / _` |
// |_| |_|_\___|_| |_\___|_\__,_|
$registry->addFieldResolver('Filefield', 'description',
$builder->callback(function ($parent, $args) {
return $parent['description'];
})
);
$registry->addFieldResolver('Filefield', 'file',
// $builder->produce('entity_reference')
// ->map('entity', $builder->fromParent())
// ->map('field', $builder->fromValue('field_showroom'))
$builder->callback(function ($parent, $args) {
return \Drupal\file\Entity\File::load($parent['target_id']);
})
);
// ___ _ _
// | __(_) |___
// | _|| | / -_)
// |_| |_|_\___|
$registry->addFieldResolver('File', 'url',
$builder->callback(function ($parent, $args) {
return $parent->url();
@ -389,50 +467,160 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
);
// _____ _
// |_ _| |_ ___ ___ __ _ _ _ _ _ _ _ ___
// | | | ' \/ -_|_-</ _` | || | '_| || (_-<
// |_| |_||_\___/__/\__,_|\_,_|_| \_,_/__/
$registry->addFieldResolver('Query', 'thesaurus',
$this->addThesaurus($registry, $builder);
$this->addVideolink($registry, $builder);
$this->addImage($registry, $builder);
// Response type resolver.
$registry->addTypeResolver('Response', [
__CLASS__,
'resolveResponse',
]);
}
// __ __ _ _
// | \/ |__ _| |_ ___ _ _(_)__ _ _ _
// | |\/| / _` | _/ -_) '_| / _` | || |
// |_| |_\__,_|\__\___|_| |_\__,_|\_,_|
/**
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
*/
protected function addMateriau(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Query', 'materiau',
$builder->produce('entity_load')
->map('type', $builder->fromValue('taxonomy_term'))
->map('bundles', $builder->fromValue(['thesaurus']))
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['materiau']))
->map('id', $builder->fromArgument('id'))
);
$registry->addFieldResolver('Thesaurus', 'id',
$registry->addFieldResolver('Materiau', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Thesaurus', 'uuid',
$registry->addFieldResolver('Materiau', 'uuid',
$builder->produce('entity_uuid')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Thesaurus', 'name',
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Materiau', 'title',
$builder->compose(
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
// __ ___ _ _ _ _
// \ \ / (_)__| |___ ___| | (_)_ _ | |__
// \ V /| / _` / -_) _ \ |__| | ' \| / /
// \_/ |_\__,_\___\___/____|_|_||_|_\_\
$registry->addFieldResolver('VideoLink', 'url',
$registry->addFieldResolver('Materiau', 'body',
$builder->produce('property_path')
->map('type', $builder->fromValue('field_item:video_embed_field'))
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('value'))
->map('path', $builder->fromValue('body.value'))
);
$registry->addFieldResolver('Materiau', 'short_description',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_short_description.value'))
);
// ___
// |_ _|_ __ __ _ __ _ ___
// | || ' \/ _` / _` / -_)
// |___|_|_|_\__,_\__, \___|
// |___/
$registry->addFieldResolver('Materiau', 'author',
$builder->compose(
$builder->produce('entity_owner')
->map('entity', $builder->fromParent()),
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
));
// https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md
// https://blog.chrismitchellonline.com/posts/custom_graphql_data/
$registry->addFieldResolver('Materiau', 'linked_materials',
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_materials'))
));
$registry->addFieldResolver('Materiau', 'linked_articles',
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_articles'))
));
$registry->addFieldResolver('Materiau', 'memo',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_memo.value'))
);
$registry->addFieldResolver('Materiau', 'images',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_materiau_images'))
);
$registry->addFieldResolver('Materiau', 'attachments',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_attachments'))
);
$registry->addFieldResolver('Materiau', 'videos',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_video'))
);
$registry->addFieldResolver('Materiau', 'tags',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_tags'))
);
$registry->addFieldResolver('Materiau', 'thesaurus',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_thesaurus'))
);
$registry->addFieldResolver('Materiau', 'manufacturer',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_manufacturer'))
);
$registry->addFieldResolver('Materiau', 'distributor',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_distributor'))
);
// field_famille
// field_index
// field_reference
// field_samples
}
// ___
// |_ _|_ __ __ _ __ _ ___
// | || ' \/ _` / _` / -_)
// |___|_|_|_\__,_\__, \___|
// |___/
/**
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
*/
protected function addImage(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Image', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
@ -474,11 +662,57 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
->map('style', $builder->fromValue('article_card_medium'))
);
// Response type resolver.
$registry->addTypeResolver('Response', [
__CLASS__,
'resolveResponse',
]);
}
// __ ___ _ _ _ _
// \ \ / (_)__| |___ ___| | (_)_ _ | |__
// \ V /| / _` / -_) _ \ |__| | ' \| / /
// \_/ |_\__,_\___\___/____|_|_||_|_\_\
/**
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
*/
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'))
);
}
// _____ _
// |_ _| |_ ___ ___ __ _ _ _ _ _ _ _ ___
// | | | ' \/ -_|_-</ _` | || | '_| || (_-<
// |_| |_||_\___/__/\__,_|\_,_|_| \_,_/__/
/**
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
*/
protected function addThesaurus(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Query', 'thesaurus',
$builder->produce('entity_load')
->map('type', $builder->fromValue('taxonomy_term'))
->map('bundles', $builder->fromValue(['thesaurus']))
->map('id', $builder->fromArgument('id'))
);
$registry->addFieldResolver('Thesaurus', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Thesaurus', 'uuid',
$builder->produce('entity_uuid')
->map('entity', $builder->fromParent())
);
$registry->addFieldResolver('Thesaurus', 'name',
$builder->produce('entity_label')
->map('entity', $builder->fromParent())
);
}
/**