Skip to content

Commit b07548a

Browse files
committed
Remove descriptions and skip sources in loader
1 parent 9b719a7 commit b07548a

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

loader.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
11
"use strict";
22

33
const os = require('os');
4+
const graphql = require('graphql')
45
const gql = require('./src');
56

7+
function isSDL(doc) {
8+
return !doc.definitions.some(def => graphql.isExecutableDefinitionNode(def));
9+
}
10+
11+
function removeDescriptions(doc) {
12+
function transformNode(node) {
13+
if (node.description) {
14+
return {
15+
...node,
16+
description: undefined,
17+
};
18+
}
19+
20+
return node;
21+
}
22+
23+
if (isSDL(doc)) {
24+
return visit(doc, {
25+
ScalarTypeDefinition: transformNode,
26+
ObjectTypeDefinition: transformNode,
27+
InterfaceTypeDefinition: transformNode,
28+
UnionTypeDefinition: transformNode,
29+
EnumTypeDefinition: transformNode,
30+
EnumValueDefinition: transformNode,
31+
InputObjectTypeDefinition: transformNode,
32+
InputValueDefinition: transformNode,
33+
FieldDefinition: transformNode,
34+
});
35+
}
36+
37+
return doc;
38+
}
39+
640
// Takes `source` (the source GraphQL query string)
741
// and `doc` (the parsed GraphQL document) and tacks on
842
// the imported definitions.
@@ -41,12 +75,31 @@ function expandImports(source, doc) {
4175

4276
module.exports = function(source) {
4377
this.cacheable();
44-
const doc = gql`${source}`;
78+
/**
79+
* @type {{
80+
* noDescription?: boolean;
81+
* noSource?: boolean;
82+
* }}
83+
*/
84+
const options = this.query || {};
85+
let doc = gql`${source}`;
86+
87+
// Removes descriptions from Nodes
88+
if (options.noDescription) {
89+
doc = removeDescriptions(doc);
90+
}
91+
4592
let headerCode = `
4693
var doc = ${JSON.stringify(doc)};
47-
doc.loc.source = ${JSON.stringify(doc.loc.source)};
4894
`;
4995

96+
// Skip Sources on demand
97+
if (!options.noSource) {
98+
headerCode += `
99+
doc.loc.source = ${JSON.stringify(doc.loc.source)};
100+
`
101+
}
102+
50103
let outputCode = "";
51104

52105
// Allow multiple query/mutation definitions in a file. This parses out dependencies

0 commit comments

Comments
 (0)