The CrudBuilder helps you to create a GraphQL type and a GraphQL query and mutation.
Generate a GraphQL scalar for a given type. Its name is the name of the type suffixed by Id (e.g. CarId). EntityTypeId is a graphql representation of an entity and is can be used in the GraphQL schema.
The query builder can generate, for each type, two queries: Type and TypeList.
The query builder can generate a mutation for a given type TypeUpdate and TypeDelete.
All operations can be configured globally or per type.
To define wich operations you want to be available in your GraphQL schema, you can use the operations options, it accepts an array of strings with the values 'get', 'list', 'create', 'update', 'delete' and 'all'(which means all operations).
You can't disable operation entityTypeId as we use it in all the operations.
*Note: By default, only the entityTypeId is generated.
Each operation (
get,list,create,update,delete) can have the following parameters:
access: Act like access is true if not set, see herepermission: Shortcut foraccess, use expression '@=hasRole()'public: Control if a operation needs to be removed from the results, see herename: redefine the name of the operation, if indefaultconfig you must write explicitly '<Type>' somewhere in the name (e.g. : "name" => "<Type>List").Note: as
permissionis a shortcut foraccessyou can't use the both in the same deep level.
Generate a query for a given type, this query will expect an entityTypeId as argument. Default name: '<Type>' (e.g. : 'Car')
query Car($id: CarId!) {
res: Car(id:$id) {
id
# ...
}
}Generate a query list for a given type. Default name: '<Type>List' (e.g. : 'CarList').
query CarList {
res: CarList {
items{
id
#...
}
}
}In list operation, you can add orderBy and criterias parameters.
orderBy[]: Define order, the manager will sort the result by this order using the repository methodfindBycriterias[]: Define criterias, the manager will filter the result by this criterias using the repository methodfindBy
Generate a graphQL mutation that create a new entity, This Mutation will expect an typeInput as argument. Default name: '<Type>Create' (e.g. : 'CarCreate').
mutation CreateCar($input: CarInput!) {
res: CreateCar(input: $input) {
id
#...
}
}Generate a graphQL mutation that update an entity. This mutation will expect an entityTypeId and typeInput as argument. Default name: '<Type>Update' (e.g. : 'CarUpdate').
mutation CarUpdate($item: CarId!, $input: CarInput!) {
res: CarUpdate(item:$item ,input: $input) {
id
}
}Generate a graphQL mutation that delete an entity. This mutation will expect an entityTypeId as argument. Default name: '<Type>Delete' (e.g. : 'CarDelete').
mutation CarDelete($item: CarId!) {
res: CarDelete(item:$item)
}Requires a configuration table that defines the operations.
namespace App\GraphQL\Builder;
class CrudConfig
{
public const CONFIG = [
'default' => [
'permission' => "@=hasRole('ROLE_ADMIN')",for
'get' => [
'public' => "@=hasRole('ROLE_ADMIN')",
],
'create' => [
// You need to define <Type> in name of the field
'name' => '<Type>Create',
],
'list' => [
'permission' => 'ROLE_ADMIN',
'orderBy' => [
'name' => 'ASC',
],
],
],
'types' => [
'Car' => [
'permission' => 'ROLE_ADMIN',
'operations' => ['get', 'list', 'create', 'update', 'delete'],
'list' => [
'permission' => 'ROLE_USER',
// You can use orderBy in type list to sort the list
'orderby' => [
'name' => 'DESC',
],
],
],
'Bus' => [
'operations'=> 'all',
'get' => [
'name' => 'GetAutoCar',
'public' => "@=hasRole('ROLE_ADMIN')",
],
],
'Truck' => [
'operations' => ['create', 'update', 'delete'],
'list' => [
'permission' => 'ROLE_USER',
],
],
'Bike' => [
// Will throw an error, because both "permission" and "access" are set as "permission" is a shortcut for "access".
'permission' => 'ROLE_ADMIN',
'access' => '@=hasRole("ROLE_ADMIN")',
'list' => [
'permission' => 'ROLE_USER',
'orderBy' => ['name'=>'DESC'],
// You can use criterias in type list
'criterias' => [
'wheels' => 3,
'colors' => 'blue',
],
],
],
],
];
}