How to manually generate hash from query for PQL manifest

I’m building a custom JavaScript pipeline that needs to take a collection of .graphql files and generate a manifest.json file to publish to a PQL. Is there an Apollo library I can import that has an API I can use to generate a hash based on a query?

Something like this?

Some time ago, I was wondering if something like this already existed, because the query that Apollo Client uses to make the request is a bit different from the one we actually write in the client. It performs some sanitizations and adds __typename to all types. At the time, I saw something about the manifest file, but I didn’t dive too deep into it. What I ended up finding was the function Apollo Client uses to add the typenames, and I copied it into my other project. In my case, I cloned the Apollo Client repo and copied the addTypenameToDocument function.

My code was something like that:

import { parse, print } from 'graphql'

const query = readFileSync(path, 'utf-8');
const queryWithTypename = addTypenameToDocument(parse(data));
const key = await sha256(print(queryWithTypename))

parse: converts query string to AST
print: convert AST to query string already sanitized

2 Likes

Thanks! Where are you importing the sha256 function from?

From crypto-hash

import { sha256 } from 'crypto-hash';