From 38b237339fc6f22e07bbeec14b861821f2e6550e Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Thu, 2 Jul 2020 11:18:28 +0200 Subject: [PATCH 1/2] Remove descriptions and skip sources in loader --- loader.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/loader.js b/loader.js index 4ceac4d3..f720ddaa 100644 --- a/loader.js +++ b/loader.js @@ -1,8 +1,39 @@ "use strict"; const os = require('os'); +const graphql = require('graphql') const gql = require('./src'); +function isSDL(doc) { + return !doc.definitions.some(def => graphql.isExecutableDefinitionNode(def)); +} + +function removeDescriptions(doc) { + function transformNode(node) { + if (node.description) { + node.description = undefined; + } + + return node; + } + + if (isSDL(doc)) { + return visit(doc, { + ScalarTypeDefinition: transformNode, + ObjectTypeDefinition: transformNode, + InterfaceTypeDefinition: transformNode, + UnionTypeDefinition: transformNode, + EnumTypeDefinition: transformNode, + EnumValueDefinition: transformNode, + InputObjectTypeDefinition: transformNode, + InputValueDefinition: transformNode, + FieldDefinition: transformNode, + }); + } + + return doc; +} + // Takes `source` (the source GraphQL query string) // and `doc` (the parsed GraphQL document) and tacks on // the imported definitions. @@ -41,12 +72,31 @@ function expandImports(source, doc) { module.exports = function(source) { this.cacheable(); - const doc = gql`${source}`; + /** + * @type {{ + * noDescription?: boolean; + * noSource?: boolean; + * }} + */ + const options = this.query || {}; + let doc = gql`${source}`; + + // Removes descriptions from Nodes + if (options.noDescription) { + doc = removeDescriptions(doc); + } + let headerCode = ` var doc = ${JSON.stringify(doc)}; - doc.loc.source = ${JSON.stringify(doc.loc.source)}; `; + // Skip Sources on demand + if (!options.noSource) { + headerCode += ` + doc.loc.source = ${JSON.stringify(doc.loc.source)}; + ` + } + let outputCode = ""; // Allow multiple query/mutation definitions in a file. This parses out dependencies From 562b242fa883d06b1fb7dad60e469f85805ae7ba Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Thu, 2 Jul 2020 12:00:01 +0200 Subject: [PATCH 2/2] Fix visit --- loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader.js b/loader.js index f720ddaa..4209f685 100644 --- a/loader.js +++ b/loader.js @@ -18,7 +18,7 @@ function removeDescriptions(doc) { } if (isSDL(doc)) { - return visit(doc, { + return graphql.visit(doc, { ScalarTypeDefinition: transformNode, ObjectTypeDefinition: transformNode, InterfaceTypeDefinition: transformNode,