DEV Community

Cover image for Graphql in laravel
xx1196
xx1196

Posted on • Updated on

Graphql in laravel

Hi, my name is Andrés Felipe, on this post we are make a server graphql with laravel.

Requisites

  • Php v8
  • Composer v2

Craft Laravel app

To create a laravel proyect run this commando in console

composer create-project laravel/laravel graphql-server
Enter fullscreen mode Exit fullscreen mode

Install Graphql libraries

Inside in our proyect laravel run the following command

composer require nuwave/lighthouse
Enter fullscreen mode Exit fullscreen mode

This command will install the graphql for laravel and php.

Mext step run the following command:

php artisan vendor:publish --tag=lighthouse-config
Enter fullscreen mode Exit fullscreen mode

to publish the config the graphql in

config/lighthouse.php
Enter fullscreen mode Exit fullscreen mode

and add the graphql uri to cors in

config/cors.php

'paths' => ['api/*', 'graphql', 'sanctum/csrf-cookie'],
Enter fullscreen mode Exit fullscreen mode

Next step publish the default schema where you add the code for graphql.

Install Graphql Playground

The playground is a enviroment for develop stage for test your queries or mutation below the pass a code in your front whit apollo.

For install this playground run de follow command

composer require mll-lab/laravel-graphql-playground
Enter fullscreen mode Exit fullscreen mode

When the installation is finished it will be published the route

/graphql-playground
Enter fullscreen mode Exit fullscreen mode

and publish the config with

php artisan vendor:publish --tag=graphql-playground-config
Enter fullscreen mode Exit fullscreen mode

this will public the config the playground in

config/graphql-playground.php
Enter fullscreen mode Exit fullscreen mode

The url for consume the api is

/graphql
Enter fullscreen mode Exit fullscreen mode

In this moment we have:

  • The libraries for laravel the graphql.
  • The playground for test our api graphql.

it would look something like this
Playground

Finish

In other post, we a create a api whit

  • Users
  • Posts

when one user have many posts like a social network

This is my firsts post, i hope this help you and i would aprreciatte if you comment,rate and follow me

TANKS.

Top comments (0)