lionixevolve/GraphQLSuiteCRM

Relational module Query and mutations

rigalpatel001 opened this issue · 5 comments

Hello,

I am trying to get relation module data but its not working properly. I have define custom relationship with Notes module i have checked NotesType.php file in schema folder and i have extend code for todoitem(custom module relationship with notes) but its not working.

NoteType.php

<?php

use Youshido\GraphQL\Type\Object\AbstractObjectType;
use Youshido\GraphQL\Type\Scalar\StringType;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Type\ListType\ListType;
require_once 'argsHelper.php';

class NoteType extends AbstractObjectType   // extending abstract Object type
{
    public function build($config)  // implementing an abstract function where you build your type
    {
        foreach (argsHelper::entityArgsHelper('Notes') as $field => $type) {
                $config->addField($field, $type);
        }
        $config->addField('created_user_details', [
                'type' => new UserType(),
                'resolve' => function ($value, array $args, ResolveInfo $info) {
                    if (!empty($value['created_user_details'])) {
                        $args['id']=$value['created_user_details'];
                        return UserType::resolve($value, $args, $info);
                    } else {
                        return null;
                    }
                 },
         ]);
        $config->addField('assigned_user_details',[
                'type' => new UserType(),
                'resolve' => function ($value, array $args, ResolveInfo $info) {
                    if (!empty($value['assigned_user_details'])) {
                        $args['id']=$value['assigned_user_details'];
                        return UserType::resolve($value, $args, $info);
                    } else {
                        return null;
                    }
                 },
         ]);
        $config->addField('modified_user_details', [
                'type' => new UserType(),
                'resolve' => function ($value, array $args, ResolveInfo $info) {
                    if (!empty($value['modified_user_details'])) {
                        $args['id']=$value['modified_user_details'];
                        return UserType::resolve($value, $args, $info);
                    } else {
                        return null;
                    }
                 },
         ]);
         $config->addField('contact_details', [
                 'type' => new ContactType(),
                 'resolve' => function ($value, array $args, ResolveInfo $info) {
                     if (!empty($value['contact_details'])) {
                         $args['id']=$value['contact_details'];
                         return ContactType::resolve($value, $args, $info);
                     } else {
                         return null;
                     }
                  },
          ]);
         $config->addField('parent_contact', [
                     'type'     => new ContactType(),
                     'args' => argsHelper::entityArgsHelper('Contacts'),
                     'resolve' => function ($value, array $args, ResolveInfo $info) {
                         if (!empty($value['parent_contact'])) {
                             $args['id']=$value['parent_contact'];
                             return ContactType::resolve($value, $args, $info);
                         } else {
                             return null;
                         }
                    },
            ]);
         $config->addField('parent_account', [
                     'type' => new AccountType(),
                     'args' => argsHelper::entityArgsHelper('Accounts'),
                     'resolve' => function ($value, array $args, ResolveInfo $info) {
                          if (!empty($value['parent_account'])) {
                              $args['id']=$value['parent_opportunity'];
                              return AccountType::resolve($value, $args, $info);
                          } else {
                              return null;
                          }
                     },
             ]);
         $config->addField('parent_opportunity', [
                     'type' => new OpportunityType(),
                     'args' => argsHelper::entityArgsHelper('Opportunities'),
                     'resolve' => function ($value, array $args, ResolveInfo $info) {
                          if (!empty($value['parent_opportunity'])) {
                              $args['id']=$value['parent_opportunity'];
                              return OpportunityType::resolve($value, $args, $info);
                          } else {
                              return null;
                          }
                     },
             ]);
        $config->addField('contacts',[
                    'type' => new ContactsListType(),
                    'args' => argsHelper::entityArgsHelper('Contacts'),
                    'resolve' => function ($value, array $args, ResolveInfo $info) {
                         if (!empty($value['contacts'])) {
                             $args['ids']=$value['contacts'];
                             return ContactsListType::resolve($value, $args, $info);
                         } else {
                             return null;
                         }
                    },
                ]);
        $config->addField('accounts', [
                    'type' => new AccountsListType(),
                    'args' => argsHelper::entityArgsHelper('Accounts'),
                    'resolve' => function ($value, array $args, ResolveInfo $info) {
                         if (!empty($value['accounts'])) {
                              $args['ids']=$value['accounts'];
                             return AccountsListType::resolve($value, $args, $info);
                         } else {
                             return null;
                         }
                    },
                ]);
        $config->addField('opportunities', [
                'type'=> new OpportunitiesListType(),
                'args' => argsHelper::entityArgsHelper('Opportunities'),
                'resolve' => function ($value, array $args, ResolveInfo $info) {
                     if (!empty($value['opportunities'])) {
                         $args['ids']=$value['opportunities'];
                         return OpportunitiesListType::resolve($value, $args,$info);
                     } else {
                         return null;
                     }
                 },
         ]);
         $config->addField('ti_todoitems_notes', [
            'type'=> new Ti_todoitemsListType(),
            'args' => argsHelper::entityArgsHelper('ti_todoitems_notes'),
            'resolve' => function ($value, array $args, ResolveInfo $info) {
                 if (!empty($value['ti_todoitems_notes'])) {
                     $args['ids']=$value['ti_todoitems_notes'];
                     return Ti_todoitemsListType::resolve($value, $args,$info);
                 } else {
                     return null;
                 }
             },
     ]);
    }
    private function retrieveNote($id, $info)
    {
        global $sugar_config, $current_user;
        $noteBean = BeanFactory::getBean('Notes');
        $note = $noteBean->retrieve($id);
        if($info!=null){
            $getFieldASTList=$info->getFieldASTList();
            $queryFields=[];
            foreach ($getFieldASTList as $key => $value) {
                $queryFields[$value->getName()]="";
            }
        }
        $module_arr = array();
        if ($note->id && $note->ACLAccess('view')) {
            $all_fields = $note->column_fields;
            foreach ($all_fields as $field) {
                if (isset($note->$field) && !is_object($note->$field)) {
                    $note->$field = from_html($note->$field);
                    $note->$field = preg_replace("/\r\n/", '<BR>', $note->$field);
                    $note->$field = preg_replace("/\n/", '<BR>', $note->$field);
                    $module_arr[$field] = $note->$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'];
            switch ($module_arr['parent_type']) {
                case 'Contacts':
                    $module_arr['parent_contact'] = $module_arr['parent_id'];
                    $module_arr['parent_account'] = '';
                    $module_arr['parent_opportunity'] = '';
                    $module_arr['parent_case'] = '';
                    $module_arr['parent_email'] = '';
                    break;
                case 'Accounts':
                    $module_arr['parent_account'] = $module_arr['parent_id'];
                    $module_arr['parent_contact'] = '';
                    $module_arr['parent_opportunity'] = '';
                    $module_arr['parent_case'] = '';
                    $module_arr['parent_email'] = '';
                    break;
                case 'Opportunities':
                    $module_arr['parent_opportunity'] = $module_arr['parent_id'];
                    $module_arr['parent_contact'] = '';
                    $module_arr['parent_account'] = '';
                    $module_arr['parent_case'] = '';
                    $module_arr['parent_email'] = '';
                    break;
                case 'Cases':
                    $module_arr['parent_opportunity'] = $module_arr['parent_id'];
                    $module_arr['parent_contact'] = '';
                    $module_arr['parent_account'] = '';
                    $module_arr['parent_case'] = $module_arr['parent_id'];
                    $module_arr['parent_email'] = '';
                    break;
                case 'Emails':
                    $module_arr['parent_opportunity'] = '';
                    $module_arr['parent_contact'] = '';
                    $module_arr['parent_account'] = '';
                    $module_arr['parent_case'] = '';
                    $module_arr['parent_email'] = $module_arr['parent_id'];
                    break;
                case 'ti_todoitems_notes':
                    $module_arr['parent_opportunity'] = '';
                    $module_arr['parent_contact'] = '';
                    $module_arr['parent_account'] = '';
                    $module_arr['parent_case'] = '';
                    $module_arr['parent_email'] = '';
                    $module_arr['parent_email'] = '';
                    break;    

                default:
                    $module_arr['parent_opportunity'] = '';
                    $module_arr['parent_contact'] = '';
                    $module_arr['parent_account'] = '';
                    $module_arr['parent_case'] = '';
                    $module_arr['parent_ti_todoitems_note'] = '';
                    break;

                }
                if(isset($queryFields) && array_key_exists('ti_todoitems_notes',$queryFields)){
                    $module_arr['ti_todoitems_notes'] =  array();
                    foreach ($note->get_linked_beans('ti_todoitems_notes', 'ti_todoitems_notes') as $contact) {
                        $module_arr['ti_todoitems_notes'][] = $contact->id;
                    }
                }
                if(isset($queryFields) && array_key_exists('contacts',$queryFields)){
                    $module_arr['contacts'] =  array();
                    foreach ($note->get_linked_beans('contacts', 'Contact') as $contact) {
                        $module_arr['contacts'][] = $contact->id;
                    }
                }
                if(isset($queryFields) && array_key_exists('accounts',$queryFields)){
                    $module_arr['accounts'] = array();
                    foreach ($note->get_linked_beans('accounts', 'Account') as $account) {
                        $module_arr['accounts'][] = $account->id;
                    }
                }
                if(isset($queryFields) && array_key_exists('opportunities',$queryFields)){
                    $module_arr['opportunities'] =  array();
                    foreach ($note->get_linked_beans('opportunities', 'Opportunity') as $opportunity) {
                        $module_arr['opportunities'][] = $opportunity->id;
                    }
                }
                $module_arr['contact_details'] =  array();
                if(isset($queryFields) && array_key_exists('contact_details',$queryFields)){
                        $module_arr['contact_details'] = $note->contact_id;
                }
            return $module_arr;
        } else {
            return null;
        }
    }

    public function resolve($value = null, $args = [], $info = null)  // implementing resolve function
    {
        if (isset($args['id']) && is_array($args['id'])) {
            foreach ($args as $key => $noteId) {
                // error_log(__LINE__.print_r($args,1));
                if (isset($noteId) && is_array($noteId)) {
                    foreach ($noteId as $key => $noteIdItem) {
                        $resultArray[] = self::retrieveNote($noteIdItem, $info);
                    }
                } elseif (!empty($noteId)) {
                    $resultArray[] = self::retrieveNote($noteId, $info);
                }
            }

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

    public function getName()
    {
        return 'Note';  // important to use the real name here, it will be used later in the Schema
    }
}

Would you please suggest to get pass paramater of custom realtion module query and also provide Mutation example .

Thanks

Are you sure you got the relationship name correctly? ti_todoitems_notes doesn't sound like a relationship name.

$note->get_linked_beans('ti_todoitems_notes', 'ti_todoitems_notes') this has to be called with the correct relationship name

take a look to Stackoverflow answer on how to get the correct name

Hello,

relationship name is correct . I think i make mistake in query . Do parameter i need to pass in query ?

relationship name as query arguments?

Thanks

if you used the correct name nothing else should be added.

I suggest you add test the get_linked_beans method in a logic hook just to see if you can get the results u expect

I have tested name is relationship name is correct.

Would you please let me know which paramaters i need to pass to get only todoitems notes?

@rigalpatel001 - there is no custom query arguments needed.

In the past comment I ask you to perform the get_linked_beans inside a logic hook to check the actual records are retrieved.

This library works on top of regular suitecrm beans method, so first make sure that method is showing expected results.