GraphQL new structure for set context entities #2644

pull/2715/head
Angel Fernando Quiroz Campos 6 years ago
parent ecf03f4ca7
commit 6ed9d3875c
  1. 17
      src/ApiBundle/GraphQL/ApiGraphQLTrait.php
  2. 14
      src/ApiBundle/GraphQL/Mutation/UserMutation.php
  3. 43
      src/ApiBundle/GraphQL/Resolver/MessageResolver.php
  4. 28
      src/ApiBundle/GraphQL/Resolver/RootResolver.php
  5. 34
      src/ApiBundle/GraphQL/Resolver/SessionCategoryResolver.php
  6. 9
      src/ApiBundle/GraphQL/Resolver/UserResolver.php
  7. 7
      src/ApiBundle/GraphQL/Resources/config/Query.types.yaml

@ -146,10 +146,7 @@ trait ApiGraphQLTrait
*/
private function protectCurrentUserData(User $user)
{
$token = $this->container->get('security.token_storage')->getToken();
/** @var User $currentUser */
$currentUser = $token->getUser();
$currentUser = $this->getCurrentUser();
if ($user->getId() === $currentUser->getId()) {
return;
@ -157,4 +154,16 @@ trait ApiGraphQLTrait
throw new UserError($this->translator->trans("The user info doesn't match."));
}
/**
* Get the current logged user.
*
* @return User
*/
private function getCurrentUser(): User
{
$token = $this->container->get('security.token_storage')->getToken();
return $token->getUser();
}
}

@ -35,19 +35,17 @@ class UserMutation implements MutationInterface, AliasedInterface, ContainerAwar
}
/**
* @param Argument $args
* @param \ArrayObject $context
* @param Argument $args
*
* @return array
*/
public function mutateSendMessage(Argument $args, \ArrayObject $context): array
public function mutateSendMessage(Argument $args): array
{
$this->checkAuthorization($context);
$this->checkAuthorization();
/** @var User $contextUser */
$contextUser = $context['user'];
$currentUser = $this->getCurrentUser();
$usersRepo = $this->em->getRepository('ChamiloUserBundle:User');
$users = $usersRepo->findUsersToSendMessage($contextUser->getId());
$users = $usersRepo->findUsersToSendMessage($currentUser->getId());
$result = [];
foreach ($args['receivers'] as $receiverId) {
@ -76,7 +74,7 @@ class UserMutation implements MutationInterface, AliasedInterface, ContainerAwar
0,
0,
0,
$contextUser->getId()
$currentUser->getId()
);
$item['sent'] = (bool) $messageId;

@ -5,7 +5,8 @@ namespace Chamilo\ApiBundle\GraphQL\Resolver;
use Chamilo\ApiBundle\GraphQL\ApiGraphQLTrait;
use Chamilo\CoreBundle\Entity\Message;
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
@ -14,25 +15,33 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
*
* @package Chamilo\ApiBundle\GraphQL\Resolver
*/
class MessageResolver implements ResolverInterface, AliasedInterface, ContainerAwareInterface
class MessageResolver implements ResolverInterface, ContainerAwareInterface
{
use ApiGraphQLTrait;
/**
* Returns methods aliases.
* @param Message $message
* @param Argument $args
* @param ResolveInfo $info
* @param \ArrayObject $context
*
* For instance:
* array('myMethod' => 'myAlias')
*
* @return array
* @return null
*/
public static function getAliases()
public function __invoke(Message $message, Argument $args, ResolveInfo $info, \ArrayObject $context)
{
return [
'resolveSender' => 'message_sender',
'resolveExcerpt' => 'message_excerpt',
'resolveHasAttachments' => 'message_has_attachments',
];
$method = 'resolve'.ucfirst($info->fieldName);
if (method_exists($this, $method)) {
return $this->$method($message, $args, $context);
}
$method = 'get'.ucfirst($info->fieldName);
if (method_exists($message, $method)) {
return $message->$method();
}
return null;
}
/**
@ -46,18 +55,18 @@ class MessageResolver implements ResolverInterface, AliasedInterface, ContainerA
}
/**
* @param Message $message
* @param int $length
* @param Message $message
* @param Argument $args
*
* @return string
*/
public function resolveExcerpt(Message $message, $length = 50)
public function resolveExcerpt(Message $message, Argument $args)
{
$striped = strip_tags($message->getContent());
$replaced = str_replace(["\r\n", "\n"], ' ', $striped);
$trimmed = trim($replaced);
return api_trunc_str($trimmed, $length);
return api_trunc_str($trimmed, $args['length']);
}
/**

@ -48,26 +48,21 @@ class RootResolver implements ResolverInterface, AliasedInterface, ContainerAwar
{
$this->checkAuthorization();
$token = $this->container->get('security.token_storage')->getToken();
return $token->getUser();
return $this->getCurrentUser();
}
/**
* @param int $courseId
* @param \ArrayObject $context
* @param int $courseId
*
* @return Course|null
*/
public function resolveCourse($courseId, \ArrayObject $context)
public function resolveCourse($courseId)
{
$this->checkAuthorization($context);
$this->checkAuthorization();
$courseRepo = $this->em->getRepository('ChamiloCoreBundle:Course');
$course = $courseRepo->find($courseId);
$context->offsetSet('course', $course);
if (!$course) {
throw new UserError($this->translator->trans('NoCourse'));
}
@ -82,20 +77,18 @@ class RootResolver implements ResolverInterface, AliasedInterface, ContainerAwar
}
/**
* @param int $sessionId
* @param \ArrayObject $context
* @param int $sessionId
*
* @return Session
*/
public function resolveSession($sessionId, \ArrayObject $context)
public function resolveSession($sessionId)
{
$this->checkAuthorization($context);
$this->checkAuthorization();
$sessionManager = $this->container->get('chamilo_core.entity.manager.session_manager');
$translator = $this->translator;
/** @var Session $session */
$session = $sessionManager->find($sessionId);
$context->offsetSet('session', $session);
if (!$session) {
throw new UserError($translator->trans('Session not found.'));
@ -105,14 +98,13 @@ class RootResolver implements ResolverInterface, AliasedInterface, ContainerAwar
}
/**
* @param int $categoryId
* @param \ArrayObject $context
* @param int $categoryId
*
* @return SessionCategory|null
*/
public function resolveSessionCategory($categoryId, \ArrayObject $context)
public function resolveSessionCategory($categoryId)
{
$this->checkAuthorization($context);
$this->checkAuthorization();
$repo = $this->em->getRepository('ChamiloCoreBundle:SessionCategory');
/** @var SessionCategory $category */

@ -5,7 +5,8 @@ namespace Chamilo\ApiBundle\GraphQL\Resolver;
use Chamilo\ApiBundle\GraphQL\ApiGraphQLTrait;
use Chamilo\CoreBundle\Entity\SessionCategory;
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
@ -14,24 +15,33 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
*
* @package Chamilo\ApiBundle\GraphQL\Resolver
*/
class SessionCategoryResolver implements ResolverInterface, AliasedInterface, ContainerAwareInterface
class SessionCategoryResolver implements ResolverInterface, ContainerAwareInterface
{
use ApiGraphQLTrait;
/**
* Returns methods aliases.
* @param SessionCategory $sessionCategory
* @param Argument $args
* @param ResolveInfo $info
* @param \ArrayObject $context
*
* For instance:
* array('myMethod' => 'myAlias')
*
* @return array
* @return null
*/
public static function getAliases(): array
public function __invoke(SessionCategory $sessionCategory, Argument $args, ResolveInfo $info, \ArrayObject $context)
{
return [
'resolveStartDate' => 'sessioncategory_startdate',
'resolveEndDate' => 'sessioncategory_enddate',
];
$method = 'resolve'.ucfirst($info->fieldName);
if (method_exists($this, $method)) {
return $this->$method($sessionCategory, $args, $context);
}
$method = 'get'.ucfirst($info->fieldName);
if (method_exists($sessionCategory, $method)) {
return $sessionCategory->$method();
}
return null;
}
/**

@ -124,15 +124,14 @@ class UserResolver implements ResolverInterface, ContainerAwareInterface
}
/**
* @param User $user
* @param string $filter
* @param \ArrayObject $context
* @param User $user
* @param string $filter
*
* @return array
*/
public function resolveMessageContacts(User $user, $filter, \ArrayObject $context): array
public function resolveMessageContacts(User $user, $filter): array
{
$this->protectCurrentUserData($context, $user);
$this->protectCurrentUserData($user);
if (strlen($filter) < 3) {
return [];

@ -82,6 +82,7 @@ UserMessage:
type: object
config:
description: 'Received message by user.'
resolveField: '@=resolver("Chamilo\\ApiBundle\\GraphQL\\Resolver\\MessageResolver", [value, args, info, context])'
fields:
id:
description: 'The unique ID of the message.'
@ -92,7 +93,6 @@ UserMessage:
type: 'String'
sender:
type: 'User'
resolve: "@=resolver('message_sender', [value])"
sendDate:
type: 'DateTime'
excerpt:
@ -102,10 +102,8 @@ UserMessage:
description: 'The approximate desired length. 50 chars by default.'
type: 'Int'
defaultValue: 50
resolve: "@=resolver('message_excerpt', [value, args['length']])"
hasAttachments:
type: 'Boolean'
resolve: "@=resolver('message_has_attachments', [value])"
Course:
type: object
@ -142,6 +140,7 @@ SessionCategory:
type: object
config:
description: 'A session category.'
resolveField: '@=resolver("Chamilo\\ApiBundle\\GraphQL\\Resolver\\SessionCategoryResolver", [value, args, info, context])'
fields:
id:
description: 'The unique ID of the category.'
@ -150,10 +149,8 @@ SessionCategory:
type: 'String'
startDate:
type: 'DateTime'
resolve: "@=resolver('sessioncategory_startdate', [value])"
endDate:
type: 'DateTime'
resolve: "@=resolver('sessioncategory_enddate', [value])"
Session:
type: object

Loading…
Cancel
Save