What is query_hash in instagram?

Viewed 15727

I was working for the first time on graphql, and I saw that Instagram hash their queries.

I searched something, but I don't know if it is correct. The hash is like a persistedquery stored in a cache memory?

Or am I wrong?

Example: this is my request payload

{
 "operationName":"user",
 "variables":{},
 "query":"query user {\n  users {\n    username\n    createdAt\n    _id\n  }\n}\n"
}

this is instagram:

 query_hash: 60b755363b5c230111347a7a4e242001
 variables: %7B%22only_stories%22%3Atrue%7D

(it is in urlencode mode).

Now, how could I hash my query? I'm using NodeJS as backend and react js as frontend. I would like to understand how it works x)! Thank you guys!

1 Answers

The persisted query is used to improve GraphQL network performance by reducing the request size.

Instead of sending a full query which could be very long, you send a hash to the GraphQL server which will retrieve the full query from the key-value store using the hash as the key.

The key value store can be memcached, redis, etc

The Apollo Server comes with automated persisted queries out of the box. I recommended gives it a try. They have publish a blog about it. https://blog.apollographql.com/automatic-persisted-queries-and-cdn-caching-with-apollo-server-2-0-bf42b3a313de

If you want to build your own solution, you can use this package to do the hashing yourself https://www.npmjs.com/package/hash.js

Related