اذهب إلى المحتوى
  • 0

استخدام graphql في لارافل

Amir Alsaeed

السؤال

أريد جلب البيانات عن طريق استخدام graphql في لارافل، ولكن بعد إضافة الكود عندما أقوم بتنفيذ الأمر:

php artisan serve

يظهر لدي هذا الخطأ:

PHP Fatal error: Declaration of App\GraphQL\Queries\ItemsQuery::type() must be compatible with Rebing\GraphQL\Support\Field::type(): GraphQL\Type\Definition\Type in /Users/app/GraphQL/Queries/ItemsQuery.php on line 9

وهذا هو الجزء الخاص بالاستعلام:

<?php

namespace App\GraphQL\Queries;

use App\Item;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;

class ItemsQuery extends Query
{
    protected $attributes = [
        'name' => 'item',
    ];

    public function type()
    {
        return GraphQL::type('Item');
    }

    public function args()
    {
        return [
            'id' => [
                'name' => 'id',
                'type' => Type::int(),
                'rules' => ['required']
            ],
        ];
    }

    public function resolve($root, $args)
    {
        return Item::findOrFail($args['id']);
    }
}

وفي ملف graphql.php :


    'prefix' => 'graphql',
  
    'routes' => '{graphql_schema?}',

    'controllers' => \Rebing\GraphQL\GraphQLController::class.'@query',

    
    'middleware' => [],

    
    'route_group_attributes' => [],

    'default_schema' => 'default',

    
    'schemas' => [
        'default' => [
            'query' => [
                'item' => App\GraphQL\Queries\ItemQuery::class,
                'items' => App\GraphQL\Queries\ItemsQuery::class,
            ]
        ],
    ],

   
    'types' => [
        'Item' => App\GraphQL\Types\ItemType::class,
    ],

    
    'lazyload_types' => false,

    
    'error_formatter' => ['\Rebing\GraphQL\GraphQL', 'formatError'],

    
    'errors_handler' => ['\Rebing\GraphQL\GraphQL', 'handleErrors'],

    
    'params_key'    => 'variables',

    
    'security' => [
        'query_max_complexity'  => null,
        'query_max_depth'       => null,
        'disable_introspection' => false,
    ],

   
    'pagination_type' => \Rebing\GraphQL\Support\PaginationType::class,

    
    'graphiql' => [
        'prefix'     => '/graphiql',
        'controller' => \Rebing\GraphQL\GraphQLController::class.'@graphiql',
        'middleware' => [],
        'view'       => 'graphql::graphiql',
        'display'    => env('ENABLE_GRAPHIQL', true),
    ],

    
    'defaultFieldResolver' => null,

    
    'headers' => [],

    
    'json_encoding_options' => 0,
];

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

يجب إضافة التضمين التالي ضمن ملف الاستعلام:

use Rebing\GraphQL\Support\Type as GraphQLType;

والوراثة من الصف GraphQLType، وأيضاً يجب إعادة return لحقول الخصائص المعرفة ضمن resolvers هذا الغرض فيصبح الكود بالشكل التالي:

<?php
namespace App\GraphQL\Types;

use App\Wine;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class ItemType extends GraphQLType
{
     return [
            'id' => [
                'type' => Type::nonNull(Type::int()),
                'description' => 'Id of the item',
            ],
            'name' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'The name of the item',
            ],
            
        ];
}

وأيضاً ضمن الملف graphql، يجب التأكد من تضمين الصفين:

use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;

ومن ثم يمكن إنشاء ملف منفصل للاستعلامات:

<?php

namespace App\GraphQL\Queries;

use App\Item;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;

class ItemQuery extends Query
{
    protected $attributes = [
        'name' => 'item',
    ];

    public function type(): Type
    {
        return GraphQL::type('Item');
    }

    public function args():array
    {
        return [
            'id' => [
                'name' => 'id',
                'type' => Type::int(),
                'rules' => ['required']
            ],
        ];
    }

    public function resolve($root, $args)
    {
        return Item::findOrFail($args['id']);
    }
}

ويمكن استعادتها بالجمع عن طريق استخدام listOf:

public function type(): Type
    {
        return Type::listOf(GraphQL::type('Item'));
    }

    public function resolve($root, $args)
    {
        return Item::all();
    }

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

المشكلة موجودة فى دالة ال type يجب أن نحدد ال return data type 

public function type(): Type {
	return GraphQL::type('Item');
}

لا تنسا أن تقوم بعمل إمبورت للكلاس الخاصة بال Type

use GraphQL\Type\Definition\Type;

هناك حل أخر وهوا كالتالى : 

use Rebing\GraphQL\Support\Facades\GraphQL;
use GraphQL\Type\Definition\Type;

public function type(): Type
{
	return Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Item'))));
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

يبدوا أن سبب الخطأ كان إما في نسيان إنشاء نوع أو خطأ في تسجيله . فالexception يقول أن العائد من الدالة type داخل ملف ال queries ليس متوافق مع المتوقع فالأرجح هو أن الدالة الاخيرة تقوم باعادة null في حين أنها يجب أن تقوم بارجاع النوع المرادف للQuery التي يتم العمل عليها . 

فطبقا للتوثيق الرسمي للحزمة على الجيت هب فان انشاءgraphql query يتطلب الخطوات التالية : 

1 - أولاً ، تقوم بإنشاء نوع تريد إرجاعه من الاستعلام : 

<?php 

namespace App\GraphQL\Types;

use App\Item;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class ItemType extends GraphQLType
{
    protected $attributes = [
        'name' => 'Item',
         
         // لاحظ تعريف المودل في النوع 
        'description' => 'An Item of Graphql Type',
        'model'         => Item::class,
    ];
  
    //  يتطلب تعريف الحقول في النوع
    public function fields()
    {
        return [
            'id' => [
                'type' => Type::nonNull(Type::int()),
                'description' => 'The id of the item'
            ],
        ];
    }
  
    public function resolve($root, $args)
    {
        return Item::findOrFail($args['id']);
    }
}

2 - ثم تحتاج الى اضافة النوع المنشئ حديثا و تسجيله .

 اما باضافته مباشرة الى المخطط الخاص بك schemas : 

<?php 

'schemas' => [
    'default' => [
        // ...
        'types' => [
            App\GraphQL\Types\ItemType::class,
        ],

او اضافته بشكل عام globally في ملف الاعداد graphql.php : 

<?php 

..
'types' => [
    App\GraphQL\Types\ItemType::class,
],

او باستعمال الواجهة GraphQL : 

<?php 

GraphQL::addType(\App\GraphQL\Types\ItemType::class);

3 - الان وبعد اضافة النوع يمكنك انشاء الqueries الخاصة بك والتي تنتمي لهذا النوع على هذا النحو : 

<?php 

namespace App\GraphQL\Queries;

use Closure;
use App\Item;
use Rebing\GraphQL\Support\Facades\GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;

class ItemsQuery extends Query
{
    protected $attributes = [
        'name' => 'items',
    ];

    public function type() // يجب أن تعيد instance من Type المرافق
    {
        return Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Item'))));
    }

    public function args()
    {
         return [
            'id' => [
                'name' => 'id',
                'type' => Type::int(),
                'rules' => ['required']
            ],
        ];
    }

    public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
    {
        return Item::findOrFail($args['id']);
    }
}

4 - يتبقى فقط تسجيل الQuery المنشئة حديثا بملف الاعداد graphql.php :

<?php 

..
'schemas' => [
    'default' => [
        'query' => [
            App\GraphQL\Queries\ItemsQuery::class
        ],
        // ...
    ]
]

و الان المفروض كل شيء سيعمل بشكل سليم .

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

يبدو أنك نسيت إضافة 

use Rebing\GraphQL\Support\Type as GraphQLType;

إلى ملف 

app/GraphQL/Types/ItemType.php

ثم جعل الكلاس يرث من GraphQLType, كما في الكود التالي 

<?php

namespace App\GraphQL\Types;

use App\Item;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class ItemType extends GraphQLType
{
    
}

ثم لم تقم بإنشاء ملف ItemQuery بهذا الشكل 

<?php

namespace App\GraphQL\Queries;

use App\Item;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;

class ItemQuery extends Query
{
    protected $attributes = [
        'name' => 'item',
    ];

    public function type(): Type
    {
        return GraphQL::type('Item');
    }

    public function args():array
    {
        return [
            'id' => [
                'name' => 'id',
                'type' => Type::int(),
                'rules' => ['required']
            ],
        ];
    }

    public function resolve($root, $args)
    {
        return Item::findOrFail($args['id']);
    }
}

يمكنك تجربة ذلك ومن إخبارنا بالنتيجة, ولكن بشكل عام, يمكنك إرفاق ملفات مشروعك هنا ليتم مساعدتك في حل المشكلة. 

او يمكنك مراجعة التوثيق الخاص بالحزمة على github من هنا.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...