Applying GraphQL to Solve Data Customization and Multi‑Request Issues in Qunar's Node BFF Service
This article analyzes the shortcomings of the existing RESTful Node BFF service for Qunar's domestic hotel platform—such as data customization difficulty, manual documentation, and redundant fields—and demonstrates how adopting GraphQL can provide precise data fetching, automatic schema documentation, and unified request composition to improve development efficiency and system performance.
In the Qunar domestic hotel service, the Node BFF layer faces three major problems: difficulty customizing data for different clients, manual maintenance of API documentation, and transmission of redundant fields, all of which hinder development efficiency and performance.
Case 1 – Data Customization Difficulty shows how the same endpoint returns all fields for PC and APP, while the mini‑program only needs a subset, leading to conditional code that removes fields based on the source and version:
hotelInfo: {
name: "酒店",
price: 232,
imgUrl: "img.jpg",
tags: [{ name: "亲子家庭" }],
score: 4.2,
rank: "XX酒店排名第X名"
}
if (source === "XIAOCHENGXU") {
hotelInfo.tags = undefined;
hotelInfo.score = undefined;
hotelInfo.rank = undefined;
}
if (source === "XIAOCHENGXU") {
hotelInfo.tags = undefined;
if (version < 120) {
hotelInfo.score = undefined;
}
hotelInfo.rank = undefined;
}This approach quickly becomes unreadable and hard to maintain as requirements evolve.
Case 2 – Multi‑Request Splicing illustrates two ways to fetch hotel detail resources: issuing multiple RESTful requests for each resource or merging them into a single endpoint, which creates tight coupling and maintenance challenges.
// Method 1:
/api/detail {交通,点评}
/api/detailprice {房型,详情}
/api/clientShow {住客秀}
/api/recommendation {周边推荐}
// Method 2:
/api/detail {交通,点评}
/api/detailprice
if (source === "APP") {
return {房型,详情,住客秀,周边推荐}
}
return {房型,详情}Both methods have trade‑offs, but the underlying RESTful design limits flexibility.
GraphQL Introduction explains that GraphQL, created by Facebook in 2012, allows clients to request exactly the fields they need, automatically generates documentation, and enables free composition of resources through a single endpoint.
Migration Steps :
Define a GraphQL schema (SDL) for the hotel model:
type Hotel {
name: String!
imgUrl: String!
price: Int!
tags: [Tag]
score: Float
rank: String
}
type Tag {
name: String!
}Implement resolvers that fetch data from existing services or databases:
hotelResolver: (id: String) => {
return db.getHotel(id);
}Update client code to send GraphQL queries, e.g.:
query: hotel(id: "String") {
name
imgUrl
price
tags { name }
score
rank
}By defining the schema, backend developers can focus on domain logic while frontend developers query precisely the data they need, eliminating over‑fetch and under‑fetch problems.
Practical Issues identified include increased backend design complexity, potential performance impacts, migration cost, and security concerns such as query depth limiting.
The article concludes that GraphQL is not a wholesale replacement for REST but a powerful complement for BFF layers that require frequent multi‑client data customization, offering a low‑risk, incremental migration path.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
