在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):gajus/graphql-deduplicator开源软件地址(OpenSource Url):https://github.com/gajus/graphql-deduplicator开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):graphql-deduplicatorA GraphQL response deduplicator. Removes duplicate entities from the GraphQL response. Client support
How does it work?
Motivation
Real-life exampleConsider the following schema: interface Node {
id: ID!
}
type Movie implements Node {
id: ID!
name: String!
synopsis: String!
}
type Event implements Node {
id: ID!
movie: Movie!
date: String!
time: String!
}
type Query {
events (
date: String
): [Event!]!
}
Using this schema, you can query events for a particular date, e.g. {
events (date: "2017-05-19") {
__typename
id
date
time
movie {
__typename
id
name
synopsis
}
}
}
Note: If you are using The result of the above query will contain a lot of duplicate information. {
"data": {
"events": [
{
"__typename": "Event",
"id": "1669971",
"date": "2017-05-19",
"time": "17:25",
"movie": {
"__typename": "Movie",
"id": "1198359",
"name": "King Arthur: Legend of the Sword",
"synopsis": "When the child Arthur’s father is murdered, Vortigern, Arthur’s uncle, seizes the crown. Robbed of his birthright and with no idea who he truly is, Arthur comes up the hard way in the back alleys of the city. But once he pulls the sword Excalibur from the stone, his life is turned upside down and he is forced to acknowledge his true legacy... whether he likes it or not."
}
},
{
"__typename": "Event",
"id": "1669972",
"date": "2017-05-19",
"time": "20:30",
"movie": {
"__typename": "Movie",
"id": "1198359",
"name": "King Arthur: Legend of the Sword",
"synopsis": "When the child Arthur’s father is murdered, Vortigern, Arthur’s uncle, seizes the crown. Robbed of his birthright and with no idea who he truly is, Arthur comes up the hard way in the back alleys of the city. But once he pulls the sword Excalibur from the stone, his life is turned upside down and he is forced to acknowledge his true legacy... whether he likes it or not."
}
},
// ...
]
}
}
I've run into this situation when building https://applaudience.co.uk. A query retrieving 300 events produced a response of 1.5MB. When gziped, that number dropped to 100KB. However, the problem is that upon receiving the response, the browser needs to parse the entire JSON document. Parsing 1.5MB JSON string is (a) time consuming and (b) memory expensive. The good news is that we do not need to return body of duplicate records (see How does it work?). For all duplicate records we only need to return In case of the earlier example, the response becomes: {
"data": {
"events": [
{
"__typename": "Event",
"id": "1669971",
"date": "2017-05-19",
"time": "17:25",
"movie": {
"__typename": "Movie",
"id": "1198359",
"name": "King Arthur: Legend of the Sword",
"synopsis": "When the child Arthur’s father is murdered, Vortigern, Arthur’s uncle, seizes the crown. Robbed of his birthright and with no idea who he truly is, Arthur comes up the hard way in the back alleys of the city. But once he pulls the sword Excalibur from the stone, his life is turned upside down and he is forced to acknowledge his true legacy... whether he likes it or not."
}
},
{
"__typename": "Event",
"id": "1669972",
"date": "2017-05-19",
"time": "20:30",
"movie": {
"__typename": "Movie",
"id": "1198359"
}
},
// ...
]
}
}
The UsageServer-sideYou need to format the final result of the query. If you are using import express from 'express';
import {
graphqlExpress
} from 'graphql-server-express';
import {
deflate
} from 'graphql-deduplicator';
const app = express();
app.use('/graphql', graphqlExpress(() => {
return {
formatResponse: (response) => {
if (response.data && !response.data.__schema) {
return deflate(response);
}
return response;
}
};
}));
app.listen(3000); Client-side
Example usage with |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论