Updated 4 August 2022
Graphql is a Query Language and Server side runtime for API. It is designed to make API fast and flexible and also developer friendly. This is an alternative to REST API, we can pull the data from multiple data sources with a single API endpoint. In this article, we will see how to create GraphQL API for the custom package in bagisto.
Why GraphQL?
Developers can use GraphQL Schema to define all the possible data that can be query through that service.
GraphQL Schema made up of ObjectType that define which type of object you can request and what are the fields it has.
We can attach each field of schema to a function it is also called Resolver , will return the value during the execution.
Strongly defined data types will reduce miscommunication between the client and the server.
Clints Get what they request for no overfetching.
More About GraphQL!
GraphQL With Bagisto
If you want to create GraphQL API for your custom package or you want to create API for core packages like(customer,products etc), below some points will be about the same that how we can create GraphQL API for Bagisto.
Installation Of Lighthouse dependency
We can integrate Lighthouse with any Laravel project to make it easy to serve GraphQL server.
The building process of graphql server
Installation Via composer:
1 |
composer require nuwave/lighthouse |
Clear the cache
1 |
php artisan cache:clear |
Lighthouse includes a default schema to get you going right away. Publish it using the following artisan
command:
1 2 |
php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider" --tag=config php artisan vendor:publish --tag=lighthouse-config |
To make use of the amazing tooling around GraphQL, we recommend installing GraphQL Playground
1 |
composer require mll-lab/laravel-graphql-playground |
After installation, visit /graphql-playground
for executing your graphql API.
JWT Installation
Install the JWT auth dependency to authenticate your graphql API
1 2 |
composer require tymon/jwt-auth:* php artisan jwt:secret --always-no |
After that, if you want to add any guard for your API you can also define the custom guard at the config/auth.php file
1 2 3 4 |
'admins' => [ 'driver' => 'eloquent', 'model' => Webkul\GraphQLAPI\Models\User\Admin::class, ] |
Now you have to define the guard in your config/lighthouse.php
1 |
'guard' => 'admin-api' |
After that Register your schema under the config/lighthouse.php file
1 2 3 |
'schema' => [ 'register' => base_path('yourpackage_path/graphql/schema.graphql'), ], |
Defining the namespaces for graphql operations(query, mutation)
1 2 3 4 5 6 7 8 9 10 11 |
'namespaces' => [ 'models' => ['App', 'package_path\\Models'], 'queries' => 'package_path\\Queries', 'mutations' => 'package_path\\Mutations', 'subscriptions' => 'package_path\\Subscriptions', 'interfaces' => 'package_path\\Interfaces', 'unions' => 'package_path\\Unions', 'scalars' => 'package_path\\Scalars', 'directives' => ['package_path\\Directives'], 'validators' => ['package_path\\Validators'], ], |
Add The JWT_TTL(JWT time to live) entry in the .env file
1 |
JWT_TTL=525600 |
Now use the graphql-playground for testing the API
1 |
http://example.com/graphql-playground |
So this was all about How To Create Graphql API with Bagisto? hope it will be helpful for you. If you have any issues feel free to raise a ticket at https://bagisto.uvdesk.com/en/
reference: GraphQLAPI, LightHouse for laravel , BagistoHeadLessCommerce
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.