@graphql-codegen/client-preset
Version:
GraphQL Code Generator preset for client.
44 lines (43 loc) • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeAndPrintDocumentNode = exports.generateDocumentHash = void 0;
const tslib_1 = require("tslib");
const crypto = tslib_1.__importStar(require("crypto"));
const documents_1 = require("@graphql-tools/documents");
const graphql_1 = require("graphql");
/**
* This function generates a hash from a document node.
*/
function generateDocumentHash(operation) {
const shasum = crypto.createHash('sha1');
shasum.update(operation);
return shasum.digest('hex');
}
exports.generateDocumentHash = generateDocumentHash;
/**
* Normalizes and prints a document node.
*/
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 = (0, graphql_1.visit)(documentNode, {
[graphql_1.Kind.FIELD](field) {
var _a;
if ((_a = field.directives) === null || _a === void 0 ? void 0 : _a.some(directive => directive.name.value === 'client')) {
return null;
}
},
[graphql_1.Kind.DIRECTIVE](directive) {
if (directive.name.value === 'connection') {
return null;
}
},
});
return (0, documents_1.printExecutableGraphQLDocument)(sanitizedDocument);
}
exports.normalizeAndPrintDocumentNode = normalizeAndPrintDocumentNode;