Building a flexible API with PHP and GraphQL

By September 8, 2021 September 20th, 2021 GraphQl, Laravel, PHP

Table of Content:

  1. What is GraphQL?
  2. GraphQL’s implementation in PHP – Laravel
  3. GraphQL vs Rest
  4. GraphQL advantages
  5. GraphQL disadvantages
  6. GraphQL use cases

What is a GraphQL?

GraphQL is a query language and server-side runtime for application programming interfaces (APIs) that prioritizes giving clients exactly the data they request.

As an alternative to REST, GraphQL lets developers construct requests that pull data from multiple data sources in a single API call.

GraphQL Implementation in PHP – Laravel

GitHub link – https://github.com/praveensparkout/GraphQL-Laravel

Important keywords:

  1. Type
  2. Query
  3. Mutation
  4. Schema

GraphQL vs REST


REST GraphQL
You would typically gather the data by accessing multiple endpoints You could gather the data in one endpoint
There are common problems of data over- and under-fetching You could receive only what you need
Frontend development deeply connected to backend development Even huge changes on frontend side could be done without backend development

GraphQL Advantages

  • Data fetching and no more over – and under fetching
  • Easy migrating from one back-end to another
  • Fetching data with a single API call
  • Detailed Error Messages
  • Validation and Type checking out-of-the-box

GraphQL Disadvantages

  • Performance issues with complex queries
  • File upload complexity
  • GraphQL caching complexity

GraphQL Use Cases

  • Apps for devices such as mobile phones, smartwatches, and IoT devices, where bandwidth usage matters
  • Applications where nested data needs to be fetched in a single call
  • Composite pattern, where application retrieves data from multiple, different storage APIs

Building a flexible API with PHP and GraphQL watch now on YouTube:

Leave a Reply