parse-graphql-json.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseGraphQLJSON = void 0;
  4. const graphql_1 = require("graphql");
  5. function 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. }
  15. function parseBOM(content) {
  16. return JSON.parse(stripBOM(content));
  17. }
  18. function 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. }
  44. exports.parseGraphQLJSON = parseGraphQLJSON;