/

hasura-header-illustration

Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins

This articles was written by Chimezie Enyinnaya as part of the Hasura Technical Writer Program. If you would like to publish an article about Hasura or GraphQL on our blog, apply here.

Remote Joins in Hasura extend the concept of joining data  across tables, to being able to join data across tables and remote data  sources. Once you create relationships between types from your database  and types created from APIs, you can then “join” them by running GraphQL  queries.

Remote joins can join across the database and APIs.  These APIs can be custom GraphQL servers you write, 3rd party SaaS APIs,  or even other Hasura instances.

In this article, we will be building APIs in Laravel using PostgreSQL as a database and Hasura for query and Remote Joining multiple tables in the database.

Prerequisites

  • Knowledge of Laravel
  • Basic knowledge of PostgreSQL and GraphQL

Setting up

To start, we will need to install Laravel on our computer and set it up. Open your command line and type in the below command:

bash
composer create-project laravel/laravel hasura-graphql
cd hasura-graphql
php artisan serve

Next, we set up an online PostgreSQL database. I will be using ElephantSQL for my database. You can use other online PostgreSQL databases if you would like.

After setting up our ElephantSQL account and creating your database, we will then set up a Hasura.io account and create a new project. If you are new to Hasura, you can learn how to set up a new project here.

Then we proceed to database setup, copy your PostgreSQL database URL and paste in the Enter your database URL input.

Note: the URL starts with postgres:// so look out for a URL like that in your online PostgreSQL platform.

Go to your database and create a todos table and populate it with some data.

With that done, we are ready to test our database. We go back to our project and configure our .env file to add our database configuration.

DB_CONNECTION=pgsql
DB_HOST=<DATABASE_HOST>
DB_PORT=5432
DB_DATABASE=DATABASE_NAME>
DB_USERNAME=<DATABASE_USERNAME>
DB_PASSWORD=<DATABASE_PASSWORD>

After the configuration is complete, we need to install lighthouse php to use GraphQL in our Laravel application. Lighthouse is a framework for serving QueryQL on Laravel. To install use the below code:

bash
composer require nuwave/lighthouse

Then we publish the Schema using:

bash
php artisan vendor:publish --tag=lighthouse-schema

Next, let’s install GraphQL Playground to test out our GraphQL API. To do that, simply use:

bash
composer require mll-lab/laravel-graphql-playground

Then we can open it using http://127.0.0.1:8000/graphql-playground. But before that, we need to configure our Lighthouse and create a Todo Model. We do that using the below command:

bash
php artisan make:model Todo

This will create a new model in your app/Models directory. Configure it with the code:

php
<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class Todo extends Model
{
    use HasFactory;

    protected $table = 'todos';
    protected $primaryKey = 'id';


    protected $fillable = ['title', 'short_description', 'content'];
}

Then, in our graphql/schema.graphql file we can add the schema for the Todo type as well as a couple of GraphQL queries:

type Query {
    todos: [Todo!]! @all
    todo(id: ID @eq): Todo @find
}

type Todo {
    id: ID!
    title: String!
    short_description: String!
    content: String!
    created_at: DateTime!
    updated_at: DateTime!
}

With that done, we can open our Graphql-playground and test out our database using http://127.0.0.1:8000/graphql-playground

Remote Joins

Now we are done with setting up, we need to create our remote joins, to do this we will be

  • Modifying our todo table and added users table
  • creating a relationship between the users table and todo table (manually populating data)
  • Testing out our remote joins

To modify the database, go to your Hasura console and create a new Users table

Then create a relationship between todos table and users table. To do that, follow the instruction here. Once the relationship is created between todos and users, we can then test on our Hasura console.

As the above code shows, we have joined our todos table to our users table and we are fetching all todos with their corresponding user.

Conclusion

In this tutorial, we have

  • Created a new instance of Laravel
  • Installed and used Lighthouse and GraphQL playground for our GraphQL query
  • We then created our ElephantSQl database and tied it to our Hasura account.
  • Configured our Laravel application to interact with our ElephantSQL and then created a remote join for our todos and users table.

You should now understand the basics of building Laravel APIs with PostgreSQL and Hasura Remote Joins.

This articles was written by Chimezie Enyinnaya as part of the Hasura Technical Writer Program. If you would like to publish an article about Hasura or GraphQL on our blog, apply here.


About the Author

Chimezie Enyinnaya is a software developer and content creator based in Lagos, Nigeria. He is the founder of adonismastery.com where he teaches about AdonisJS, a fully-featured Node.js framework.

Blog
30 Dec, 2020
Email
Subscribe to stay up-to-date on all things Hasura. One newsletter, once a month.
Loading...
v3-pattern
Accelerate development and data access with radically reduced complexity.