gajus / graphql-deduplicator Goto Github PK
View Code? Open in Web Editor NEWA GraphQL response deduplicator. Removes duplicate entities from the GraphQL response.
License: Other
A GraphQL response deduplicator. Removes duplicate entities from the GraphQL response.
License: Other
example: [String]
"example": [
"SETUP",
"MOBILE"
],
"example": [
{
"0": "S",
"1": "E",
"2": "T",
"3": "U",
"4": "P"
},
{
"0": "M",
"1": "O",
"2": "B",
"3": "I",
"4": "L",
"5": "E"
}
Hi @gajus. Firstly, thanks - this is a great package that we've been using for a while! Recently we noticed that we're hitting the bug where inner objects are not inflated which is fixed by #26 but which has not been released on npm. Would you be able to push a new version to npm which includes this fix? Cheers
This is what deduplicator returns in case of introspectionSchema request.
I guess you need to check if this is a __schema
request then do nothing and return everything as is
The deflate
function, given something like [[{a: 1}]]
, will transform it into something like [{0: {a: 1}}]
Hey, I'm a little confused... I've seen and used the __typename field but I can't get graphql-js to spit out a __datatype field. Also can't find it anywhere on google or the github repos for graphql or apollo. Plenty of __typename though :)
Where is this mystical __datatype documented?
I'm curious to know if a traditional compression method, like gzip, gives similar benefits? Or does this offer potential performance improvements on top of traditional compression? I'm not familiar with how those algorithms work, but would imagine they de-dupe? Or not?
If you have a query that return an array of primitive values like [Float!]!, It still returns an empty object for each item in the array even after the fix in #21
To fix it I added this code to check if the node is neither an array or an object in both deflate and inflate functions
if (!Array.isArray(node) && !isObject(node)) { return node; }
After taking a look at the built source code, you can see that arrow functions still exist in the output. This doesn't bode well for slightly older versions of uglify.
Would be great to consider adding an additional babel plugin to ensure that the artifacts can be safely minified.
I developed a library to normalize/denormalize any graphql response which is available here: graphql-norm. If you want a more compact response, I guess normalizing it before returning it and then denormalizing on the client could be an alternative too or does graphql-deduplicator do some more tricks to cut the size down?
apollo-client
is able to interpret graphql-deduplicator
response out of the box. Other clients might not me. Therefore, it would be useful to provide a request middleware that denormalizes the response body.
When using with mongodb ObjectId
, the field _id of type ObjectId
gets transformed into JSON as follows:
{
"0": 99,
"1": 219,
"2": 161,
"3": 49,
"4": 150,
"5": 77,
"6": 124,
"7": 0,
"8": 19,
"9": 11,
"10": 192,
"11": 172
}
expected: I expect _id to be string equivalent! Otherwise there is overhead of converting these _id to hex string
Instead of recommending to use a query param, why don't we recommend using a custom HTTP header to request deduplicated data?
inflate
is a noop if the data wasn't deflated it doesn't matter whether the backend ends up sending deduplicated data or notI imagine something like this:
const httpLink = new HttpLink({
credentials: 'include',
uri: '/api',
headers: {
'X-GraphQL-Deduplicate': true
}
});
app.use('/graphql', graphqlExpress((request) => {
return {
formatResponse: (response) => {
if (request.headers['X-GraphQL-Deduplicate'] && response.data && !response.data.__schema) {
return deflate(response.data);
}
return response;
}
};
}));
I have some objects which don't use id
as the key but some other fields and some objects where the id is not unique so I would like to disable de-duplication for them.
Is there a way to support a similar config as InMemoryCache to disable the de-duplication for them based on __typename?
Also Apollo supports both id
and _id
by default, can this module match the behaviour or atleast provide an option for default key name?
Any idea how I could use graphql-deduplicator
conditionally in Apollo Server?
ApolloServer:formatResponse
doesn't support access to the request object (or context) so I tried by doing a GraphQLExtension
like this:
class DeduplicateResponseExtension extends GraphQLExtension<Context> {
public willSendResponse(o: {
graphqlResponse: GraphQLResponse;
context: Context;
}): { graphqlResponse: GraphQLResponse; context: Context } {
const { context, graphqlResponse } = o
if (context.req.query.deduplicate && graphqlResponse.data && !graphqlResponse.data.__schema) {
const newResponse = deflate(graphqlResponse)
return {
...o,
graphqlResponse: newResponse,
}
}
return o
}
}
However, when using it I receive an error from Apollo Server:
{
"error": {
"errors": [
{
"message": "response.http.headers is not iterable",
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: response.http.headers is not iterable",
" at [..]/node_modules/apollo-server-core/dist/runHttpQuery.js:164:55",
" at Generator.next (<anonymous>)",
" at fulfilled ([..]/node_modules/apollo-server-core/dist/runHttpQuery.js:4:54)"
]
}
}
}
]
}
}
This issue might belong in apollo-server, asking here first to see if anyone knows of a setup that works with Apollo Server.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.