UNPKG

@cran/lib.gql.core

Version:

GQL Core Functionality

40 lines (39 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseLiteral = exports.parseObject = exports.parseError = void 0; const graphql_1 = require("graphql"); const ast_1 = require("graphql/language/ast"); function parseError(type, ast, reason = "input syntax") { throw new TypeError(`invalid ${reason} for type ${type}: ${(0, ast_1.isNode)(ast) ? (0, graphql_1.print)(ast) : String(ast)}`); } exports.parseError = parseError; function parseObject(type, ast, variables) { const value = Object.create(null); for (const field of ast.fields) { value[field.name.value] = parseLiteral(type, field.value, variables); } return value; } exports.parseObject = parseObject; function parseLiteral(type, ast, variables) { switch (ast.kind) { case graphql_1.Kind.STRING: case graphql_1.Kind.BOOLEAN: case graphql_1.Kind.INT: case graphql_1.Kind.FLOAT: return ast.value; case graphql_1.Kind.OBJECT: return parseObject(type, ast, variables); case graphql_1.Kind.LIST: return ast.values.map(function mapValue(value) { return parseLiteral(type, value, variables); }); case graphql_1.Kind.NULL: return null; case graphql_1.Kind.VARIABLE: return variables?.[ast.name.value]; default: return parseError(type, ast); } } exports.parseLiteral = parseLiteral;