UNPKG

@relay-graphql-js/validation-rules

Version:
65 lines (64 loc) 2.36 kB
"use strict"; exports.__esModule = true; exports.isConnectionType = exports.getConnectionDirective = exports.makeNonNullable = exports.containsVariableNodes = exports.isFragmentDefinition = exports.isFragmentSpread = exports.findFragmentDefinitionParent = exports.findFragmentSpreadParent = void 0; var graphql_1 = require("graphql"); function findFragmentSpreadParent(nodes) { return nodes.find(isFragmentSpread); } exports.findFragmentSpreadParent = findFragmentSpreadParent; function findFragmentDefinitionParent(nodes) { return nodes.find(isFragmentDefinition); } exports.findFragmentDefinitionParent = findFragmentDefinitionParent; function isFragmentSpread(node) { return node != null && node.kind === "FragmentSpread"; } exports.isFragmentSpread = isFragmentSpread; function isFragmentDefinition(node) { return node != null && node.kind === "FragmentDefinition"; } exports.isFragmentDefinition = isFragmentDefinition; function containsVariableNodes(node) { var hasVars = false; graphql_1.visit(node, { Variable: function () { hasVars = true; return graphql_1.BREAK; } }); return hasVars; } exports.containsVariableNodes = containsVariableNodes; function makeNonNullable(type) { if (!(type instanceof graphql_1.GraphQLNonNull)) { return new graphql_1.GraphQLNonNull(type); } return type; } exports.makeNonNullable = makeNonNullable; function getConnectionDirective(fieldNode) { var directive = fieldNode.directives && fieldNode.directives.find(function (d) { return d.name.value === "connection"; }); if (!directive) { return null; } var keyArgument = directive.arguments && directive.arguments.find(function (arg) { return arg.name.value === "key"; }); if (!keyArgument || keyArgument.value.kind !== "StringValue") { return { key: null, directive: directive }; } return { key: keyArgument.value.value, directive: directive }; } exports.getConnectionDirective = getConnectionDirective; function isConnectionType(type) { var nullableType = graphql_1.getNullableType(type); if (!(nullableType instanceof graphql_1.GraphQLObjectType)) { return false; } return nullableType.name.endsWith("Connection"); } exports.isConnectionType = isConnectionType;