lionixevolve / GraphQLSuiteCRM

GraphQL SuiteCRM - Integrate with SuiteCRM using GraphQL
19 stars 2 forks source link

Give error when return relational table field #12

Closed ankitpatelinitio closed 5 years ago

ankitpatelinitio commented 5 years ago

Hello, I am creating a custom schema to get data from 2 relational tables. But when I am returning relational table field from graphql query at that time it will give me field not found error. Please suggest me a solution for how to return relational table field value using graphql and suite CRM.

use Youshido\GraphQL\Type\ListType\AbstractListType;

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

class KanbanTodoitems extends AbstractListType
{
    public function getItemType()
    {
        return new Ti_todoitemsType();

    }

    public function build($config)
    {
    }
    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;
        $kanbanid =  $args['kanban_ref'];

        require_once 'vendor/lionixevolve/graphqlsuitecrm/graphql/Schema/ListHelper.php';
        global $db;
        $Query = "SELECT ti.id, ti.name, ti.contact_id_c, ti.todolist_id, ti.lession_id, ti.due_date, ti.priority, ti.status,pj.id as project_id, pj.agent_id, c.first_name, hlc.todorepeattype_c, ti.role FROM ti_todoitems ti LEFT JOIN contacts c ON c.id = ti.contact_id_c LEFT JOIN proj_project pj ON ti.todolist_id = pj.todo_list LEFT JOIN hlpkg_todolist_cstm hlc ON hlc.id_c = ti.todolist_id  WHERE kanban_ref='$kanbanid'  and ti.deleted='0'  ORDER By convert(ti.task_order,unsigned) ASC";
        $result = $db->query($Query);

        if($result) {
            return $result;
        } else {
                return null;
        }
    }

}
mrbarletta commented 5 years ago

hi @ankitpatelinitio

I can't fully understand your question, but this library is made specifically to work with SuiteCRM models and you are doing a regular query (without sanitizing input by the way).

If you just need a graphql interface for your data take a look at webonyx/graphql-php.

Also, graphql needs the response definition in advance, for example in your query you have ti.id, ti.name, but you don't define what graphql types are those.

If that table is part of a SuiteCRM module, then you better use this as a guide https://github.com/lionixevolve/GraphQLSuiteCRm#extendingcustomizing-suitecrm-graphql-schema.

ankitpatelinitio commented 5 years ago

@mrbarletta I would like to get data from suite CRM using graphql. So I created a custom file to write a query to get data from the relational table.

query Kanbantodoitems($kanban_ref: String) {
    Kanbantodoitems(kanban_ref: $kanban_ref) {
      id
      name
      due_date
      priority
      status
      todo_order
      description
      priority
      contact_id_c
    }
  }

It is working fine but when I am passing first_name at that time it will give me error field not found. Because first_name is relational table field. So How can I implement relation and return relational table result.

Thanks

mrbarletta commented 5 years ago

I am still finding hard to understand what do you mean by a relational table, SuiteCRM has models with relations, you have to use the model and its functions to get relationship data.

You can better understand this with the Account type, that has relationship with many other modules.

Here you can see how to get contacts related to the account:

https://github.com/lionixevolve/GraphQLSuiteCRM/blob/master/graphql/Schema/AccountType.php#L244

Also, please note that you are not defining fields in the build of your type

    {
    }