Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
"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) {
node.description = undefined;
}

return node;
}

if (isSDL(doc)) {
return graphql.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 +72,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