Compressing GraphQL response

Using basic principles of compression to reduce the response size

Normalized schema

type Query {
events (
after: String
coordinates: CoordinatesInput
first: Int
fromDate: String
movieId: ID
# Used to restrict results to the set number of nearest venues (relative to the coordinates).
# Defaults to 5. Maximum 10.
nearest: Int
toDate: String
venueId: ID
): EventsConnection!
}
query getEvents($coordinates: CoordinatesInput, $fromDate: String, $toDate: String, $movieId: ID, $venueId: ID) {
events(coordinates: $coordinates, fromDate: $fromDate, toDate: $toDate, movieId: $movieId, venueId: $venueId, nearest: 10) {
edges {
node {
id
date
time
timestamp
url
displayTicketPrice
reservationIsEnabled
auditorium {
id
name
seatingPlan {
id
seatCount
__typename
}
__typename
}
movie {
id
name
runtime
synopsis
credits(first: 5) {
edges {
node {
creditOrder
character {
id
name
__typename
}
person {
id
name
headshotImageUrl
__typename
}
__typename
}
__typename
}
__typename
}
directors {
id
person {
id
name
headshotImageUrl
__typename
}
__typename
}
posterImageUrl
genres {
id
name
__typename
}
releaseYear
trailerYoutubeId
__typename
}
venue {
id
name
cinema {
id
name
url
logo {
svgUrl
__typename
}
__typename
}
coordinates {
latitude
longitude
__typename
}
address {
id
street1
postcode
__typename
}
__typename
}
attributes {
abbreviatedName
id
name
nid
__typename
}
__typename
}
__typename
}
__typename
}
}
UI used by GO2CINEMA.com to list the obtained results. User navigates by first discovering the movie, then identifying the venue and finally picking the event.

GZIP

Deduplication

events(fromDate: "2018-02-14", toDate: "2018-02-14") {
id
date
time
movie {
id
name
synopsis
__typename
}
__typename
}
{
"data": {
"events": [
{
"__typename": "Event",
"date": "2018-02-14",
"id": 1,
"movie": {
"__typename": "Movie",
"id": 1,
"name": "The Shape of Water",
"synopsis": "An other-worldly story, set against the backdrop of Cold War era America circa 1962, where a mute janitor working at a lab falls in love with an amphibious man being held captive there and devises a plan to help him escape."
},
"time": "17:05"
},
{
"__typename": "Event",
"date": "2018-02-14",
"id": 2,
"movie": {
"__typename": "Movie",
"id": 1,
"name": "The Shape of Water",
"synopsis": "An other-worldly story, set against the backdrop of Cold War era America circa 1962, where a mute janitor working at a lab falls in love with an amphibious man being held captive there and devises a plan to help him escape."
},
"time": "18:50"
},
{
"__typename": "Event",
"date": "2018-02-14",
"id": 3,
"movie": {
"__typename": "Movie",
"id": 2,
"name": "Three Billboards Outside Ebbing, Missouri",
"synopsis": "After seven months have passed without a culprit in her daughter's murder case, Mildred Hayes makes a bold move, painting three signs leading into her town with a controversial message directed at Bill Willoughby, the town's revered chief of police. When his second-in-command Officer Jason Dixon, an immature mother's boy with a penchant for violence, gets involved, the battle between Mildred and Ebbing's law enforcement is only exacerbated."
},
"time": "17:45"
},
{
"__typename": "Event",
"date": "2018-02-14",
"id": 4,
"movie": {
"__typename": "Movie",
"id": 2,
"name": "Three Billboards Outside Ebbing, Missouri",
"synopsis": "After seven months have passed without a culprit in her daughter's murder case, Mildred Hayes makes a bold move, painting three signs leading into her town with a controversial message directed at Bill Willoughby, the town's revered chief of police. When his second-in-command Officer Jason Dixon, an immature mother's boy with a penchant for violence, gets involved, the battle between Mildred and Ebbing's law enforcement is only exacerbated."
},
"time": "19:25"
}
]
}
}
{
"data": {
"events": [
{
"__typename": "Event",
"date": "2018-02-14",
"id": 1,
"movie": {
"__typename": "Movie",
"id": 1,
"name": "The Shape of Water",
"synopsis": "An other-worldly story, set against the backdrop of Cold War era America circa 1962, where a mute janitor working at a lab falls in love with an amphibious man being held captive there and devises a plan to help him escape."
},
"time": "17:05"
},
{
"__typename": "Event",
"date": "2018-02-14",
"id": 2,
"movie": {
"__typename": "Movie",
"id": 1
},
"time": "18:50"
},
{
"__typename": "Event",
"date": "2018-02-14",
"id": 3,
"movie": {
"__typename": "Movie",
"id": 2,
"name": "Three Billboards Outside Ebbing, Missouri",
"synopsis": "After seven months have passed without a culprit in her daughter's murder case, Mildred Hayes makes a bold move, painting three signs leading into her town with a controversial message directed at Bill Willoughby, the town's revered chief of police. When his second-in-command Officer Jason Dixon, an immature mother's boy with a penchant for violence, gets involved, the battle between Mildred and Ebbing's law enforcement is only exacerbated."
},
"time": "17:45"
},
{
"__typename": "Event",
"date": "2018-02-14",
"id": 4,
"movie": {
"__typename": "Movie",
"id": 2
},
"time": "19:25"
}
]
}
}

Reduplication

graphql-deduplicator

Compatibility with the GraphQL specification

app.use('/graphql', graphqlExpress((request) => {
return {
formatResponse: (response) => {
if (request.query.deduplicate && response.data && !response.data.__schema) {
return deflate(response.data);
}
return response;
}
};
}));

--

--

Founder, engineer interested in JavaScript, PostgreSQL and DevOps. Follow me on Twitter for outbursts about startups & engineering. https://twitter.com/kuizinas

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gajus Kuizinas

Founder, engineer interested in JavaScript, PostgreSQL and DevOps. Follow me on Twitter for outbursts about startups & engineering. https://twitter.com/kuizinas