graphql-binding
Version:
[](https://circleci.com/gh/graphql-binding/graphql-binding) [](https://badge.fury.io/js/graphql-binding)
76 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
function isScalar(t) {
if (t instanceof graphql_1.GraphQLScalarType || t instanceof graphql_1.GraphQLEnumType) {
return true;
}
if (t instanceof graphql_1.GraphQLObjectType ||
t instanceof graphql_1.GraphQLInterfaceType ||
t instanceof graphql_1.GraphQLUnionType ||
t instanceof graphql_1.GraphQLList) {
return false;
}
var nnt = t;
if (nnt instanceof graphql_1.GraphQLNonNull) {
if (nnt.ofType instanceof graphql_1.GraphQLScalarType ||
nnt.ofType instanceof graphql_1.GraphQLEnumType) {
return true;
}
}
return false;
}
exports.isScalar = isScalar;
function getTypeForRootFieldName(rootFieldName, operation, schema) {
if (operation === 'mutation' && !schema.getMutationType()) {
throw new Error("Schema doesn't have mutation type");
}
if (operation === 'subscription' && !schema.getSubscriptionType()) {
throw new Error("Schema doesn't have subscription type");
}
var rootType = {
query: function () { return schema.getQueryType(); },
mutation: function () { return schema.getMutationType(); },
subscription: function () { return schema.getSubscriptionType(); },
}[operation]() || undefined;
var rootField = rootType.getFields()[rootFieldName];
if (!rootField) {
throw new Error("No such root field found: " + rootFieldName);
}
return rootField.type;
}
exports.getTypeForRootFieldName = getTypeForRootFieldName;
function forwardTo(bindingName) {
return function (parent, args, context, info) {
var message = "Forward to '" + bindingName + "." + info.parentType.name.toLowerCase() + "." + info.fieldName + "' failed. ";
if (context[bindingName]) {
if (context[bindingName][info.parentType.name.toLowerCase()][info.fieldName]) {
return context[bindingName][info.parentType.name.toLowerCase()][info.fieldName](args, info);
}
else {
message += "Field '" + info.parentType.name.toLowerCase() + "." + info.fieldName + "' not found on binding '" + bindingName + "'.";
}
}
else {
message += "Binding '" + bindingName + "' not found.";
}
throw new Error(message);
};
}
exports.forwardTo = forwardTo;
function printDocumentFromInfo(info) {
var fragments = Object.keys(info.fragments).map(function (fragment) { return info.fragments[fragment]; });
var doc = {
kind: 'Document',
definitions: [
{
kind: 'OperationDefinition',
operation: 'query',
selectionSet: info.fieldNodes[0].selectionSet,
}
].concat(fragments),
};
return graphql_1.print(doc);
}
exports.printDocumentFromInfo = printDocumentFromInfo;
//# sourceMappingURL=index.js.map