|
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 | + 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 | + |
6 | 40 | // Takes `source` (the source GraphQL query string) |
7 | 41 | // and `doc` (the parsed GraphQL document) and tacks on |
8 | 42 | // the imported definitions. |
@@ -41,12 +75,31 @@ function expandImports(source, doc) { |
41 | 75 |
|
42 | 76 | module.exports = function(source) { |
43 | 77 | 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 | + |
45 | 92 | let headerCode = ` |
46 | 93 | var doc = ${JSON.stringify(doc)}; |
47 | | - doc.loc.source = ${JSON.stringify(doc.loc.source)}; |
48 | 94 | `; |
49 | 95 |
|
| 96 | + // Skip Sources on demand |
| 97 | + if (!options.noSource) { |
| 98 | + headerCode += ` |
| 99 | + doc.loc.source = ${JSON.stringify(doc.loc.source)}; |
| 100 | + ` |
| 101 | + } |
| 102 | + |
50 | 103 | let outputCode = ""; |
51 | 104 |
|
52 | 105 | // Allow multiple query/mutation definitions in a file. This parses out dependencies |
|
0 commit comments