UNPKG

1.31 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseGraphQLJSON = void 0;
4const graphql_1 = require("graphql");
5function stripBOM(content) {
6 content = content.toString();
7 // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
8 // because the buffer-to-string conversion in `fs.readFileSync()`
9 // translates it to FEFF, the UTF-16 BOM.
10 if (content.charCodeAt(0) === 0xfeff) {
11 content = content.slice(1);
12 }
13 return content;
14}
15function parseBOM(content) {
16 return JSON.parse(stripBOM(content));
17}
18function parseGraphQLJSON(location, jsonContent, options) {
19 let parsedJson = parseBOM(jsonContent);
20 if (parsedJson.data) {
21 parsedJson = parsedJson.data;
22 }
23 if (parsedJson.kind === 'Document') {
24 return {
25 location,
26 document: parsedJson,
27 };
28 }
29 else if (parsedJson.__schema) {
30 const schema = (0, graphql_1.buildClientSchema)(parsedJson, options);
31 return {
32 location,
33 schema,
34 };
35 }
36 else if (typeof parsedJson === 'string') {
37 return {
38 location,
39 rawSDL: parsedJson,
40 };
41 }
42 throw new Error(`Not valid JSON content`);
43}
44exports.parseGraphQLJSON = parseGraphQLJSON;