medlabmg/youshido-graphql-extended

It Extends youshido graphql bundle to use easily

This package's canonical repository appears to be gone and the package has been frozen as a result.

Installs: 1 338

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 2

Type:symfony-bundle

v0.1.3 2017-05-23 07:35 UTC

This package is not auto-updated.

Last update: 2024-01-21 00:25:36 UTC


README

At this moment YoushidoGraphQLExtendedBundle is in alpha version

This package has been created to facilitate the use of yousido package with the following features:

  • Response entities directly and auto serialize by JMS Serializer with group "GraphQL" only asking for data required
  • Easy way to create Queries and Mutations
  • Command to auto generate types from entities
  • Command to validate Type with your response
  • Security per operation by roles declared in annotation
  • Improve errors trace

Instalation

composer require medlabmg/youshido-graphql-extended

add bundles youshido and extended

// app/AppKernel.php

$bundles = array(
    // add jms serializer, it is required 
    new JMS\SerializerBundle\JMSSerializerBundle(),
    
    new Youshido\GraphQLBundle\GraphQLBundle(),
    new \MedlabMG\YoushidoGraphQLExtendedBundle\YoushidoGraphQLExtendedBundle(),
);

Enable controllers

# app/config/routing.yml

graphql:
    resource: "@YoushidoGraphQLExtendedBundle/Controller/"
    type: annotation

! YoushidoGraphQLExtendedBundle is enabled :-) !

How to create a mutation resolver

Configure it

# app/config/services.yml

service_name:
    class: MedlabMG/MedlabBundle/GraphQL/Mutation/Security/SecurityCreateTokenField
    tags:
          - {name: graphql.mutation}

And his logic..

// src/MedlabMG/MedlabBundle/GraphQL/Mutation/Security/SecurityCreateTokenField.php
    
class SecurityCreateTokenField extends AbstractResolverField
{

    public function buildParent(FieldConfig $config)
    {
        $config
            ->setDescription('Mutation')
            ->addArguments([
                'username' => new NonNullType(new StringType()),
                'password' => new NonNullType(new StringType()),
            ])
        ;
    }

    public function resolveParent($value, ResolveInfo $info)
    {
        $user = $this->args->get('username');
        $password = $this->args->get('password');
        
        if ($user !== 'miguel')
            $this->addViolation('invalid username', 'username');
         
        if ($password !== 'pass') 
            $this->addViolation('invalid password', 'password');
            
        $this->verifyCurrentErrors();

        return 'jwt';
    }

    /**
     * @return AbstractObjectType|AbstractType
     */
    public function getType()
    {
        return new StringType();
    }

}

How to create a query resolver

Configure it

# app/config/services.yml

service_name:
    class: MedlabMG/MedlabBundle/GraphQL/Query/Category/CategoryAllField
    tags:
          - {name: graphql.query}

And his logic..

    // src/MedlabMG/MedlabBundle/GraphQL/Query/Category/CategoryAllField.php
    
    class CategoryAllField extends AbstractResolverField
    {
    
        public function resolveParent($value, ResolveInfo $info)
        {
            return $this->getEM()->getRepository("MedlabMGMedlabBundle:Category")->findAll();
        }
    
        /**
         * @return AbstractObjectType|AbstractType
         */
        public function getType()
        {
            return new ListType(new CategoryType());
        }
    
    }

Security in resolver

Its easy add annotation in a class query or mutation

use MedlabMG\YoushidoGraphQLExtendedBundle\Resolver\AbstractResolverField;
use MedlabMG\YoushidoGraphQLExtendedBundle\Annotation\SecurityGraphQL;

/**
 * @SecurityGraphQL({"ROLE_GRAPHQL_STUDENT"})
 */
class StudentProfileUpdateField extends AbstractResolverField
{
   // ...
}

This annotation also is used to auto insert roles required in doc