Start a Project

How To Create GraphQL API for Custom Package In Bagisto

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

  1. Define the shape of data using the GraphQL Schema Definition Language.
  2. Use directives to bring schema.
  3. Add custom functionality (resolver, fields, model)where you need it.

Installation Via composer:

Clear the cache

Lighthouse includes a default schema to get you going right away. Publish it using the following artisan command:

To make use of the amazing tooling around GraphQL, we recommend installing GraphQL Playground

After installation, visit /graphql-playground for executing your graphql API.

JWT Installation

Install the JWT auth dependency to authenticate your graphql API

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

Now you have to define the guard in your config/lighthouse.php

After that Register your schema under the config/lighthouse.php file

Defining the namespaces for graphql operations(query, mutation)

Add The JWT_TTL(JWT time to live) entry in the .env file

Now use the graphql-playground for testing  the API

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

Exit mobile version