UNPKG

1.15 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
12/**
13 * Returns a GraphQLCompilerContext containing only the documents referenced
14 * by and including the provided node.
15 */
16function filterContextForNode(node, context) {
17 var queue = [node];
18 var filteredContext = new (require("./GraphQLCompilerContext"))(context.serverSchema, context.clientSchema).add(node);
19
20 var visitFragmentSpread = function visitFragmentSpread(fragmentSpread) {
21 var name = fragmentSpread.name;
22
23 if (!filteredContext.get(name)) {
24 var fragment = context.getFragment(name);
25 filteredContext = filteredContext.add(fragment);
26 queue.push(fragment);
27 }
28 };
29
30 var visitorConfig = {
31 FragmentSpread: function FragmentSpread(fragmentSpread) {
32 visitFragmentSpread(fragmentSpread);
33 }
34 };
35
36 while (queue.length) {
37 require("./GraphQLIRVisitor").visit(queue.pop(), visitorConfig);
38 }
39
40 return filteredContext;
41}
42
43module.exports = filterContextForNode;
\No newline at end of file