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.
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:
composer require nuwave/lighthouse
Clear the cache
php artisan cache:clear
Lighthouse includes a default schema to get you going right away. Publish it using the following
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
composer require mll-lab/laravel-graphql-playground
After installation, visit
/graphql-playground for executing your graphql API.
Install the JWT auth dependency to authenticate your graphql API
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
'admins' => [
'driver' => 'eloquent',
'model' => Webkul\GraphQLAPI\Models\User\Admin::class,
Now you have to define the guard in your config/lighthouse.php
'guard' => 'admin-api'
After that Register your schema under the config/lighthouse.php file
'schema' => [
'register' => base_path('yourpackage_path/graphql/schema.graphql'),
Defining the namespaces for graphql operations(query, mutation)
'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
Now use the graphql-playground for testing the API
Be the first to comment.