|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | 3 | const os = require('os'); |
| 4 | +const graphql = require('graphql') |
4 | 5 | const gql = require('./src'); |
5 | 6 |
|
| 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 | + |
6 | 37 | // Takes `source` (the source GraphQL query string) |
7 | 38 | // and `doc` (the parsed GraphQL document) and tacks on |
8 | 39 | // the imported definitions. |
@@ -41,12 +72,31 @@ function expandImports(source, doc) { |
41 | 72 |
|
42 | 73 | module.exports = function(source) { |
43 | 74 | 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 | + |
45 | 89 | let headerCode = ` |
46 | 90 | var doc = ${JSON.stringify(doc)}; |
47 | | - doc.loc.source = ${JSON.stringify(doc.loc.source)}; |
48 | 91 | `; |
49 | 92 |
|
| 93 | + // Skip Sources on demand |
| 94 | + if (!options.noSource) { |
| 95 | + headerCode += ` |
| 96 | + doc.loc.source = ${JSON.stringify(doc.loc.source)}; |
| 97 | + ` |
| 98 | + } |
| 99 | + |
50 | 100 | let outputCode = ""; |
51 | 101 |
|
52 | 102 | // Allow multiple query/mutation definitions in a file. This parses out dependencies |
|
0 commit comments