GraphQl and Laravel translatable

In one of our latest custom made web application, we have been decided to give a try to GraphQl, one of the greatest advantages for us is that the client application can send a GraphQL query to get exactly what it needs, which makes development faster and cooperation with client application developers way easier.

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

As for that project we are using Laravel Nova, we decided to give a try to Lighthouse .

We must say that, being new to GraphQl we had to play a bit around before getting it working, but finally it wasn’t too bad.

With Lighthouse we have installed the companion package Playground and together it has been fantastic to see how our developers had fun transforming models into fully working api, seriously!

The application is multilingual so we needed to make some of our model’s attribute translatable, and for that we have been using the super useful package Spatie Laravel Translatable along with Nova Translatable which in my opinion is the best of his kind.

This make translating the attributes super easy and at the same time very sexy.

One more thing was left to do, make sure that when the client app would call our GraphQL api, the translatable attributes would be returned in a specific locale, as by default Spatie Laravel Translatable returns the translated attribute of the application default locale.

What we decided to do, using a Locale middleware that would allow us to send a “Content-Language” parameter in the header to switch the default application locale.

The last thing that needed to be done, was to assign our new middleware to the schema.graphql file like in the example below

type Query @group(middleware: ["auth:api","locale"]) {
    me: User @auth
}

And voilà, a fully translatable GraphQl api with Laravel Nova, that was lot of fun, and we are planning to develop more applications with the same setup in the future.