Base Format for GraphQL trong Drupal 9

6th May 2022
Table of contents

REST vs GraphQL

The same route handler function is called in REST whereas in GraphQL query can be called to form nested response with multiple resources. GraphQL execution library is used to construct response structures.

PURPOSE

  • Decoupled Drupal applications with a javascript front-end (React, Angular, Ember, etc)
  • Providing more flexibility and efficiency as compared to REST endpoint by exposing API endpoints using GraphQL (Query language).

For Query Structure

type Car {

  id: ID

  name: String

  launched: Date

  price: String

  brand: Brand

}

type Brand {

  id: ID

  country: String

  cars: [car]

}

Base Format for GraphQL

type Query {

 car(id: CID!): Car

 brand(id: BID!): Brand

}

type Mutation {

  getTestDrive(input: AddRequestInput): TestDrive

}

type Car { ... }

type Brand { ... }

type TestDrive { ... }

input AddRequestInput { ... }

Request Format

GET /graphql?query={ car(id: "1") { name, brand { country } } }

Integration in Drupal 8

  • Module: graphql
  • Library : graphql-php

Advantage of GraphQL

  • Reduce the number of API calls to the server by the ability to created nested API calls.
  • It uses strong type system to define the capabilities of an API. These types are written in the schema using GraphQL Schema definition (SDL) which is serves as a contract between the Client and Server.
  • Once the schema is defined front-end and back-end teams know the structure of the data and can, therefore, work independently.
  • No more Over- and Under fetching of attributes that may be one of the concerns in REST
  • GraphQL uses the concept of resolver functions to collect the data that’s requested by a client. With the help of resolvers, we can identify issues/errors in the schema definition
  • Changes in the Frontend design can be addressed without any extra work on the server. Since Client-side application (React, Angular, etc) can specify their exact data requirements, no backend engineer needs to make adjustments in API.
Bạn thấy bài viết này như thế nào?
0 reactions

Add new comment

Image CAPTCHA
Enter the characters shown in the image.
Câu nói tâm đắc: “Điều tuyệt với nhất trong cuộc sống là làm được những việc mà người khác tin là không thể!”

Related Articles

There’s no denying that GraphQL is the latest addition for API development. For front end frameworks many GraphQL clients are available and Apollo

However, there is one case that the Graphql module does not cover: building a Graphql schema for data that is not represented as a Drupal entity.

Over the past decade, REST has become the standard (yet a fuzzy one) for designing web APIs

This session will introduce GraphQL queries and demonstrate the advantages of changing the Drupal push model to a pull model by letting the template define its data requirements

Drupal is a renowned name when it comes to website development. The kind of features and control it allows you to have on your content is quite impressive.