PHP code example of lionixevolve / graphqlsuitecrm

1. Go to this page and download the library: Download lionixevolve/graphqlsuitecrm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lionixevolve / graphqlsuitecrm example snippets



//`graphql/CustomSuiteCRMSchema.php`
nputType.php';
\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Type\Scalar\StringType;

class CustomSuiteCRMSchema
{
    public function buildSchema($config)
    {
        $config->getQuery()->addFields([
            'prospectlists' => [
                'type' => new ProspectlistListType(),
                'args' => argsHelper::entityArgsHelper('ProspectLists'),
                'resolve' => function ($value, array $args, ResolveInfo $info) {
                    return $info->getReturnType()->resolve($value, $args, $info);
                },
            ],
        ]);
        $config->getMutation()->addFields([
            'assignProspectlistprospect' => [
                'type' => new ProspectlistprospectInputType(),
                'args' => ['beanid' => new StringType(), 'beanname' => new StringType(), 'prospectlistid' => new StringType()],
                'resolve' => function ($value, $args, $type) {
                    $result = ProspectlistprospectInputType::resolve($value, $args, $type);
                    return ["id" => $result];
                },
            ],
        ]);
    }

}


//Schema/ProspectlistsType.php
use Youshido\GraphQL\Type\Object\AbstractObjectType;
use Youshido\GraphQL\Execution\ResolveInfo;

class ProspectlistType extends AbstractObjectType   // extending abstract Object type
{
    public function build($config)  // implementing an abstract function where you build your type
    {
        foreach (argsHelper::entityArgsHelper('ProspectLists') as $field => $type) {
                $config->addField($field, $type);
        }
        
        $config->addField('related_contacts',[
                    'type' => new ContactsListType(),
                    'args' => argsHelper::entityArgsHelper('Contacts'),
                    'resolve' => function ($value, array $args, ResolveInfo $info) {
                         if (!empty($value['related_contacts'])) {
                             $args['ids']=$value['related_contacts'];
                             return ContactsListType::resolve($value, $args, $info);
                         } else {
                             return null;
                         }
                    },
                ]);
        $config->addField('related_accounts', [
                    'type' => new AccountsListType(),
                    'args' => argsHelper::entityArgsHelper('Accounts'),
                    'resolve' => function ($value, array $args, ResolveInfo $info) {
                         if (!empty($value['related_accounts'])) {
                              $args['ids']=$value['related_accounts'];
                             return AccountsListType::resolve($value, $args, $info);
                         } else {
                             return null;
                         }
                    },
                ]);
    }
    private function retrieveProspectlist($id, $info)
    {
        global $sugar_config, $current_user;
        $prospectlistBean = BeanFactory::getBean('ProspectLists');
        $bean = $prospectlistBean->retrieve($id);
        if($info!=null){
            $getFieldASTList=$info->getFieldASTList();
            $queryFields=[];
            foreach ($getFieldASTList as $key => $value) {
                $queryFields[$value->getName()]="";
            }
        }

        $module_arr = array();
        if ($bean->id && $bean->ACLAccess('view')) {
            $all_fields = $bean->column_fields;
            foreach ($all_fields as $field) {
                if (isset($bean->$field) && !is_object($bean->$field)) {
                    $bean->$field = from_html($bean->$field);
                    $bean->$field = preg_replace("/\r\n/", '<BR>', $bean->$field);
                    $bean->$field = preg_replace("/\n/", '<BR>', $bean->$field);
                    $module_arr[$field] = $bean->$field;
                }
            }
            $module_arr['created_user_details'] = $module_arr['created_by'];
            $module_arr['assigned_user_details'] = $module_arr['assigned_user_id'];
            $module_arr['modified_user_details'] = $module_arr['modified_user_id'];
            
            foreach ($bean->get_linked_beans('contacts', 'Contact') as $contact) {
                $module_arr['related_contacts'][] = $contact->id;
            }
            foreach ($bean->get_linked_beans('accounts', 'Account') as $account) {
                $module_arr['related_accounts'][] = $account->id;
            }
            foreach ($bean->get_linked_beans('opportunities', 'Opportunity') as $opportunity) {
                $module_arr['related_opportunities'][] = $opportunity->id;
            }
            return $module_arr;
        } else {
            error_log(__METHOD__.'----'.__LINE__.'----'.'error resolving CallType');
            return;
        }
    }

    public function resolve($value = null, $args = [], $info = null )  // implementing resolve function
    {
        if (isset($args['id']) && is_array($args['id'])) {
            foreach ($args as $key => $nodeId) {
                if (isset($nodeId) && is_array($nodeId)) {
                    foreach ($nodeId as $key => $nodeIdItem) {
                        $resultArray[] = self::retrieveProspectlist($nodeIdItem, $info );
                    }
                } elseif (!empty($nodeId)) {
                    $resultArray[] = self::retrieveProspectlist($nodeId, $info );
                }
            }

            return $resultArray;
        } elseif (!empty($args['id'])) {
            return self::retrieveProspectlist($args['id'],$info);
        }
    }

    public function getName()
    {
        return 'ProspectList'; 
    }
}



// schema/ProspectlistListType.php

use Youshido\GraphQL\Type\ListType\AbstractListType;


if (!defined('sugarEntry')) {
    define('sugarEntry', true);
}
turn new ProspectlistType();
    }

    public function build($config)
    {
        // $config
        // ->addArgument('limit', TypeMap::TYPE_INT)
        // ->addArgument('offset', TypeMap::TYPE_INT);
    }
    public function endswith($string, $test)
    {
        $strlen = strlen($string);
        $testlen = strlen($test);
        if ($testlen > $strlen) {
            return false;
        }
        return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
    }
    public function resolve($value = null, $args = [], $info = null)
    {
        global $current_user;
        


//schema/ProspectlistprospectInputType.php
use Youshido\GraphQL\Type\Object\AbstractObjectType;
use Youshido\GraphQL\Type\Scalar\StringType;

class ProspectlistprospectInputType extends AbstractObjectType   // extending abstract Object type
{
    public function build($config)  // implementing an abstract function where you build your type
    {
        $config->addField('id', new StringType());
    }

    public static function guidv4() //Helper function to imitate mysql GUID
        {
            if (function_exists('com_create_guid') === true)
                return trim(com_create_guid(), '{}');

            $data = openssl_random_pseudo_bytes(16);
            $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
            $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
            return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
        }
    public function resolve($value = null, $args = [], $type = null)  // implementing resolve function
    {
        //We use the crm Helper to create/save the Bean
        global $db;
        if(!empty($args['beanid']) && !empty($args['prospectlistid']) && !empty($args['beanname']) 
        && ($args['beanname'] =='Accounts' || $args['beanname'] =="Contacts"||$args['beanname']== "Prospects" )
        ){
            $insertQuery="
            INSERT INTO prospect_lists_prospects
            (id, prospect_list_id, related_id, related_type, date_modified)
            VALUES('?', '?', '?', '?', now());
            ";
            $uuid=\ProspectlistprospectInputType::guidv4();
            $resultByStatus = $db->pQuery($insertQuery,  [$uuid, $args['prospectlistid'],$args['beanid'],$args['beanname']]);
            if($resultByStatus=="1"){
                return $uuid;
            }else{
                return null;
            }
        }else{
            return null;
        }
         
    }

    public function getName()
    {
        return 'Prospectlistprospect';
    }
    public function getOutputType()
    {
        return new StringType();  
    }
}


$app->post('/newPost', function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response){
return $response->withJson(["result"=>"empty - not even tried"]);
}


$app->add(new \Eko3alpha\Slim\Middleware\CorsMiddleware([
    'http://www.example.com' => ['GET', 'POST','PUT', 'DELETE','OPTIONS'],
    'https://www.example2.com' => ['GET', 'POST'],
]));