@graphql-codegen/client-preset
Version:
GraphQL Code Generator preset for client.
38 lines (37 loc) • 1.28 kB
JavaScript
import * as crypto from 'crypto';
import { printExecutableGraphQLDocument } from '@graphql-tools/documents';
import { Kind, visit } from 'graphql';
/**
* This function generates a hash from a document node.
*/
export function generateDocumentHash(operation, algorithm) {
const shasum = crypto.createHash(algorithm);
shasum.update(operation);
return shasum.digest('hex');
}
/**
* Normalizes and prints a document node.
*/
export function normalizeAndPrintDocumentNode(documentNode) {
/**
* This removes all client specific directives/fields from the document
* that the server does not know about.
* In a future version this should be more configurable.
* If you look at this and want to customize it.
* Send a PR :)
*/
const sanitizedDocument = visit(documentNode, {
[Kind.FIELD](field) {
var _a;
if ((_a = field.directives) === null || _a === void 0 ? void 0 : _a.some(directive => directive.name.value === 'client')) {
return null;
}
},
[Kind.DIRECTIVE](directive) {
if (directive.name.value === 'connection') {
return null;
}
},
});
return printExecutableGraphQLDocument(sanitizedDocument);
}