Skip to content

Commit

Permalink
Remove descriptions and skip sources in loader
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Jul 2, 2020
1 parent 9b719a7 commit b07548a
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
"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) {
return {
...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.
Expand Down Expand Up @@ -41,12 +75,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
Expand Down

0 comments on commit b07548a

Please sign in to comment.