Skip to content

Commit 38b2373

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

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

loader.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
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+
node.description = undefined;
15+
}
16+
17+
return node;
18+
}
19+
20+
if (isSDL(doc)) {
21+
return visit(doc, {
22+
ScalarTypeDefinition: transformNode,
23+
ObjectTypeDefinition: transformNode,
24+
InterfaceTypeDefinition: transformNode,
25+
UnionTypeDefinition: transformNode,
26+
EnumTypeDefinition: transformNode,
27+
EnumValueDefinition: transformNode,
28+
InputObjectTypeDefinition: transformNode,
29+
InputValueDefinition: transformNode,
30+
FieldDefinition: transformNode,
31+
});
32+
}
33+
34+
return doc;
35+
}
36+
637
// Takes `source` (the source GraphQL query string)
738
// and `doc` (the parsed GraphQL document) and tacks on
839
// the imported definitions.
@@ -41,12 +72,31 @@ function expandImports(source, doc) {
4172

4273
module.exports = function(source) {
4374
this.cacheable();
44-
const doc = gql`${source}`;
75+
/**
76+
* @type {{
77+
* noDescription?: boolean;
78+
* noSource?: boolean;
79+
* }}
80+
*/
81+
const options = this.query || {};
82+
let doc = gql`${source}`;
83+
84+
// Removes descriptions from Nodes
85+
if (options.noDescription) {
86+
doc = removeDescriptions(doc);
87+
}
88+
4589
let headerCode = `
4690
var doc = ${JSON.stringify(doc)};
47-
doc.loc.source = ${JSON.stringify(doc.loc.source)};
4891
`;
4992

93+
// Skip Sources on demand
94+
if (!options.noSource) {
95+
headerCode += `
96+
doc.loc.source = ${JSON.stringify(doc.loc.source)};
97+
`
98+
}
99+
50100
let outputCode = "";
51101

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

0 commit comments

Comments
 (0)