DEV Community

Cover image for Graphql in laravel
xx1196
xx1196

Posted on • Edited 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.

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay