All definition config entries can use expression language but it must be explicitly triggered using the @=
prefix. This bundle provides a set of registered functions and variables. For more details on expression syntax see the official documentation.
- Registered functions:
- Registered variables
- Private services
- Custom expression functions
- Backslashes in expressions
Signature: service(string $id): object|null
| Alias: serv
Gets a service from the service container. Private services should be explicitly tagged to be accessible, see this section for more details.
Examples:
@=service('my_service').customMethod()
# Using the 'ser' alias
@=serv('my_service').customMethod()
# Using the FQCN for the service name (only works for public services).
# Note the double quotes.
@=serv("App\\Manager\\UserManager").someMethod()
# If using single quotes, you must use 4 slashes
@=serv('App\\\\Manager\\\\UserManager').someMethod()
Signature: parameter(string $name): mixed
| Alias: param
Gets a parameter from the service container.
Examples:
@=parameter('kernel.debug')
# Using the 'param' alias
@=param('mailer.transport')
Signature: isTypeOf(string $className): boolean
Checks if the value
is instance of the given class name.
Example:
@=isTypeOf("App\\User\\User")
Signature: query(string $alias, ...$args): mixed
| Alias: q
Calls a method on the tagged service overblog_graphql.query
with $args
Examples:
# Using aliased resolver name
@=query('blog_by_id', value['blogID'])
# Using the 'q' alias and a FQCN::methodName.
# Note the double quotes.
@=q("App\\GraphQL\\Resolver\\UserResolver::findOne", args, info, context, value)
# If using single quotes, you must use 4 slashes
@=q('App\\\\GraphQL\\\\Resolver\\\\UserResolver::findOne', info, args)
Signature: mutation(string $alias, ...$args): mixed
| Alias: m
Calls a method on the tagged service overblog_graphql.mutation
passing $args
as arguments.
Examples:
# Using aliased mutation name
@=mutation('remove_post_from_community', args.postId)
# Using the 'm' alias and a FQCN::methodName
# Note the double quotes.
@=m("App\\GraphQL\\Mutation\\PostMutation::findAll", args)
# If using single quotes, you must use 4 slashes
@=m('App\\\\GraphQL\\\\Mutation\\\\PostMutation::findAll', args)
Signature: arguments(array $mapping, mixed $data): mixed
Transforms and validates a list of arguments. See the Arguments Transformer section for more details.
Example:
@=arguments(['input' => 'MyInput'], ['input' => ['field1' => 'value1']])
Signature: globalId(string|int $id, string $typeName = null): string
Relay node globalId.
Example:
@=globalId(15, 'User')
Signature: fromGlobalId(string $globalId): array
Relay node globalId.
Example:
@=fromGlobalId(‘QmxvZzox’)
Signature: newObject(string $className, array $args = []): object
Creates a new class instance from given class name and arguments. Uses the following php code under the hood:
(new ReflectionClass($className))->newInstanceArgs($args)
See the official documentation for more details about the ReflectionClass::newInstanceArgs
method.
Examples:
@=newObject("App\\Entity\\User", ["John", 15])
# Using inside another function (query)
@=query("myResolver", newObject("App\\User\\User", [args]))
Signature: call(callable $target, array $args = []): mixed
Calls a function or a static method, passing $args
to it as arguments.
Examples:
# Calling a static method using a FCN string
@=call("App\\Util\\Validator::email", ["arg1", 2])
# Calling a static method using an array callable
@=call(["App\\Util\\Validator", "email"], [args["email"]])
# Calling a function
@=call('array_merge', [args['array1'], args['array2']])
Signature: hasRole(string $role): bool
Checks whether the logged in user has a certain role.
Example:
@=hasRole('ROLE_API')
Signature: hasAnyRole(string $role1, string $role2, ...string $roleN): bool
Checks whether the logged in user has at least one of the given roles.
Example:
@=hasAnyRole('ROLE_API', 'ROLE_ADMIN')
Signature: isAnonymous(): bool
Checks whether the token is anonymous. Shorthand for:
AuthorizationChecker::isGranted('IS_AUTHENTICATED_ANONYMOUSLY')
Example:
@=isAnonymous()
Signature: isRememberMe(): bool
Checks whether the token is remembered. Shorthand for :
AuthorizationChecker::isGranted('IS_AUTHENTICATED_REMEMBERED')
Example:
@=isRememberMe()
Signature: isFullyAuthenticated(): bool
Checks whether the token is fully authenticated. Shorthand for:
AuthorizationChecker::isGranted('IS_AUTHENTICATED_FULLY')
Example:
@=isFullyAuthenticated()
Signature: isAuthenticated(): bool
Checks whether the token is not anonymous. Shorthand for:
AuthorizationChecker::isGranted('IS_AUTHENTICATED_REMEMBERED') || AuthorizationChecker::isGranted('IS_AUTHENTICATED_FULLY')
Example:
@=isAuthenticated()
Signature: hasPermission(object $object, string $permission): bool
Checks whether logged in user has given permission for given object (requires symfony/acl-bundle to be installed).
Example:
# Using in combination with the 'service' function.
@=hasPermission(serv('user_repository').find(1), 'OWNER')
Signature: hasAnyPermission(object $object, array $permission): bool
Checks whether the token has any of the given permissions for the given object
Example:
# Using in combination with the 'service' function
@=hasAnyPermission(service('my_service').getObject(), ['OWNER', 'ADMIN'])
Signature: getUser(): Symfony\Component\Security\Core\User\UserInterface|null
Returns the user which is currently in the security token storage.
Examples
@=getUser()
# Checking if user has particular name
@=getUser().firstName === 'adam'
Signature: getType(string $alias): GraphQL\Type\Definition\Type|null
Returns a GraphQL type.
Examples
@=getType('Post')
Variable | Description | Scope |
---|---|---|
object |
Refers to the value of the field for which access is being requested. For array object will be each item of the array. For Relay connection object will be the node of each connection edges. |
only available for config.fields.*.access with query operation or mutation payload type. |
value |
The value returned by a previous resolver | available in the resolve and access contexts |
args |
An array of argument values of current resolver | available in the resolve and access contexts |
info |
A GraphQL\Type\Definition\ResolveInfo object of current resolver |
available in the resolve and access contexts |
context |
context is defined by your application on the top level of query execution (useful for storing current user, environment details, etc) | available in the resolve and access contexts |
childrenComplexity |
Selection field children complexity | only available in complexity context |
It is not possible to use private services with the service
function since this is equivalent to call the
get
method on the Service Container. In order to make
private services accessible, they must be tagged with overblog_graphql.service
.
Example:
App\MyPrivateService:
public: false
tags:
- { name: overblog_graphql.service, alias: my_private_service }
Usage:
MyType:
type: object
config:
fields:
name:
type: String!
resolve: "@=service('my_private_service').formatName(value)"
To use a vendor private services:
$vendorPrivateServiceDef = $container->findDefinition(\Vendor\PrivateService::class);
$vendorPrivateServiceDef->addTag('overblog_graphql.service', ['alias' => 'vendor_private_service']);
Adding custom expression function is easy since all you need to do is create a tagged service. Expression functions can help user create simple resolver without having to leave config file, this also improves performance by removing a useless external resolver call.
Here is an example to add a custom expression equivalent to php json_decode
:
<?php
namespace App\ExpressionLanguage;
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
class JsonDecode extends ExpressionFunction
{
public function __construct()
{
parent::__construct(
'json_encode',
function ($json, $assoc) {
return sprintf('json_decode(%s, %s)', $json, $assoc);
}
);
}
}
now register your service:
App\ExpressionLanguage\JsonDecode:
tags: ['overblog_graphql.expression_function']
Now json_decode
can be used in schema:
Object:
type: object
config:
fields:
name:
type: String!
resolve: "@=json_decode(value.json_data, true)['name']"
Tips: At last if this is not an answer to all your needs, the expression language service can be customized using bundle configuration.
Backslashes in expressions must be escaped by 2 or 4 backslasehs, depending on which quotes do you use.
When using single quotes as outer quotes, you must use double backslashes. e.g.:
...
resolve: '@=query("App\\GraphQL\\Resolver\\ResolverName::methodName")'
...
When using double quotes as outer quotes, you must use 4 backslashes, e.g.:
...
resolve: "@=query('App\\\\GraphQL\\\\Resolver\\\\ResolverName::methodName')"
...