graphql
Version:
A Query Language and Runtime which can target any service.
140 lines (118 loc) • 4.17 kB
JavaScript
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
;
var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default'];
_Object$defineProperty(exports, '__esModule', {
value: true
});
exports.print = print;
var _visitor = require('./visitor');
/**
* Converts an AST into a string, using one set of reasonable
* formatting rules.
*/
function print(ast) {
return (0, _visitor.visit)(ast, {
leave: {
Name: function Name(node) {
return node.value;
},
Variable: function Variable(node) {
return '$' + node.name;
},
// Document
Document: function Document(node) {
return join(node.definitions, '\n\n') + '\n';
},
OperationDefinition: function OperationDefinition(node) {
var op = node.operation;
var name = node.name;
var defs = manyList('(', node.variableDefinitions, ', ', ')');
var directives = join(node.directives, ' ');
var selectionSet = node.selectionSet;
return !name ? selectionSet : join([op, join([name, defs]), directives, selectionSet], ' ');
},
VariableDefinition: function VariableDefinition(node) {
return join([node.variable + ': ' + node.type, node.defaultValue], ' = ');
},
SelectionSet: function SelectionSet(node) {
return blockList(node.selections, ',\n');
},
Field: function Field(node) {
return join([join([join([node.alias, node.name], ': '), manyList('(', node.arguments, ', ', ')')]), join(node.directives, ' '), node.selectionSet], ' ');
},
Argument: function Argument(node) {
return node.name + ': ' + node.value;
},
// Fragments
FragmentSpread: function FragmentSpread(node) {
return join(['...' + node.name, join(node.directives, ' ')], ' ');
},
InlineFragment: function InlineFragment(node) {
return join(['... on', node.typeCondition, join(node.directives, ' '), node.selectionSet], ' ');
},
FragmentDefinition: function FragmentDefinition(node) {
return join(['fragment', node.name, 'on', node.typeCondition, join(node.directives, ' '), node.selectionSet], ' ');
},
// Value
IntValue: function IntValue(node) {
return node.value;
},
FloatValue: function FloatValue(node) {
return node.value;
},
StringValue: function StringValue(node) {
return JSON.stringify(node.value);
},
BooleanValue: function BooleanValue(node) {
return node.value ? 'true' : 'false';
},
EnumValue: function EnumValue(node) {
return node.value;
},
ArrayValue: function ArrayValue(node) {
return '[' + join(node.values, ', ') + ']';
},
ObjectValue: function ObjectValue(node) {
return '{' + join(node.fields, ', ') + '}';
},
ObjectField: function ObjectField(node) {
return node.name + ': ' + node.value;
},
// Directive
Directive: function Directive(node) {
return join(['@' + node.name, node.value], ': ');
},
// Type
ListType: function ListType(node) {
return '[' + node.type + ']';
},
NonNullType: function NonNullType(node) {
return node.type + '!';
}
}
});
}
function blockList(list, separator) {
return length(list) === 0 ? null : indent('{\n' + join(list, separator)) + '\n}';
}
function indent(maybeString) {
return maybeString && maybeString.replace(/\n/g, '\n ');
}
function manyList(start, list, separator, end) {
return length(list) === 0 ? null : start + join(list, separator) + end;
}
function length(maybeArray) {
return maybeArray ? maybeArray.length : 0;
}
function join(maybeArray, separator) {
return maybeArray ? maybeArray.filter(function (x) {
return !!x;
}).join(separator || '') : '';
}