So I was wondering how I can convert a DocumentNode
back to a query string in graphql-js
.
I thought at first it should be a simple var.toString()
but it turns out that it is not like that at all.
So we have a utility function in graphql-js
called print
which:
Converts an AST into a string, using one set of reasonable formatting rules.
— Ref.
And here is the code that I wrote for my own purposes (striping everything except for when the DocumentNode
is an operation type):
import {
DefinitionNode,
DocumentNode,
Kind,
parse,
print,
} from 'graphql';
/**
* @description Strips the query from fragments
*/
export function fieldDepthQueryNormalizer(
query: Readonly<string>,
): string {
const ast = parse(query);
const definitionNodes: DefinitionNode[] = [];
const definitions = (ast as DocumentNode).definitions;
for (const definition of definitions) {
if (definition.kind !== Kind.OPERATION_DEFINITION) {
continue;
}
definitionNodes.push(definition);
}
return definitionNodes
.map((definitionNode) => print(definitionNode))
.join(' ');
}
And here is a simple unit test for this particular implementation but feel free to adjust it to serve your needs better:
import { fieldDepthQueryNormalizer } from './field-depth-query-normalizer.util';
describe('querySanitizerForFieldDepth', () => {
it.each([
'query {n getPosts {n idn author {n ...fn posts {n idn }n }n }n}nnfragment f on User {n idn}',
'fragment f on User {n idn}nnquery {n getPosts {n idn author {n ...fn posts {n idn }n }n }n}n',
])('should stripe all fragments', (query) => {
const stripedQueryFromOperationDefinitions =
fieldDepthQueryNormalizer(query);
expect(
stripedQueryFromOperationDefinitions.replace(/s+/g, ' '),
).toBe('{ getPosts { id author { ...f posts { id } } } }');
});
});
Caution
I just needed to remove everything but operation type queries. So keep in mind that this query string is invalid from GraphQL point of view but serves my need really well.
GH repo for this code: https://github.com/kasir-barati/graphql-js-ts/blob/main/libs/shared/src/services/complexity-plugin/utils/field-depth-query-normalizer.util.ts