UNPKG

2.65 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * strict-local
8 * @format
9 */
10'use strict';
11
12var _toConsumableArray2 = require("@babel/runtime/helpers/interopRequireDefault")(require("@babel/runtime/helpers/toConsumableArray"));
13
14var parseGraphQL = require("./GraphQLCompilerProfiler").instrument(require("graphql").parse, 'GraphQL.parse');
15
16var FIND_OPTIONS = {
17 validateNames: true
18};
19
20module.exports = function (tagFinder) {
21 var memoizedTagFinder = require("./RelayFindGraphQLTags").memoizedFind.bind(null, tagFinder);
22
23 function parseFile(baseDir, file) {
24 var result = parseFileWithSources(baseDir, file);
25
26 if (result) {
27 return result.document;
28 }
29 } // Throws an error if parsing the file fails
30
31
32 function parseFileWithSources(baseDir, file) {
33 var text = require("fs").readFileSync(require("path").join(baseDir, file.relPath), 'utf8');
34
35 !(text.indexOf('graphql') >= 0) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelaySourceModuleParser: Files should be filtered before passed to the ' + 'parser, got unfiltered file `%s`.', file.relPath) : require("fbjs/lib/invariant")(false) : void 0;
36 var astDefinitions = [];
37 var sources = [];
38 memoizedTagFinder(text, baseDir, file, FIND_OPTIONS).forEach(function (template) {
39 var source = new (require("graphql").Source)(template, file.relPath);
40 var ast = parseGraphQL(source);
41 !ast.definitions.length ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelaySourceModuleParser: Expected GraphQL text to contain at least one ' + 'definition (fragment, mutation, query, subscription), got `%s`.', template) : require("fbjs/lib/invariant")(false) : void 0;
42 sources.push(source.body);
43 astDefinitions.push.apply(astDefinitions, (0, _toConsumableArray2["default"])(ast.definitions));
44 });
45 return {
46 document: {
47 kind: 'Document',
48 definitions: astDefinitions
49 },
50 sources: sources
51 };
52 }
53
54 function getParser(baseDir) {
55 return new (require("./ASTCache"))({
56 baseDir: baseDir,
57 parse: parseFile
58 });
59 }
60
61 function getFileFilter(baseDir) {
62 return function (file) {
63 var text = require("fs").readFileSync(require("path").join(baseDir, file.relPath), 'utf8');
64
65 return text.indexOf('graphql') >= 0;
66 };
67 }
68
69 return {
70 getParser: getParser,
71 getFileFilter: getFileFilter,
72 parseFile: parseFile,
73 parseFileWithSources: parseFileWithSources
74 };
75};
\No newline at end of file