UNPKG

5.24 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 *
8 * @format
9 */
10'use strict';
11
12/**
13 * An immutable representation of a corpus of documents being compiled together.
14 * For each document, the context stores the IR and any validation errors.
15 */
16var GraphQLCompilerContext =
17/*#__PURE__*/
18function () {
19 function GraphQLCompilerContext(serverSchema, clientSchema) {
20 this._isMutable = false;
21 this._documents = new (require("immutable").OrderedMap)();
22 this._withTransform = new WeakMap();
23 this.serverSchema = serverSchema; // If a separate client schema doesn't exist, use the server schema.
24
25 this.clientSchema = clientSchema || serverSchema;
26 }
27 /**
28 * Returns the documents for the context in the order they were added.
29 */
30
31
32 var _proto = GraphQLCompilerContext.prototype;
33
34 _proto.documents = function documents() {
35 return this._documents.toArray();
36 };
37
38 _proto.forEachDocument = function forEachDocument(fn) {
39 this._documents.forEach(fn);
40 };
41
42 _proto.replace = function replace(node) {
43 return this._update(this._documents.update(node.name, function (existing) {
44 !existing ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLCompilerContext: Expected to replace existing node %s, but' + 'one was not found in the context.', node.name) : require("fbjs/lib/invariant")(false) : void 0;
45 return node;
46 }));
47 };
48
49 _proto.add = function add(node) {
50 return this._update(this._documents.update(node.name, function (existing) {
51 !!existing ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLCompilerContext: Duplicate document named `%s`. GraphQL ' + 'fragments and roots must have unique names.', node.name) : require("fbjs/lib/invariant")(false) : void 0;
52 return node;
53 }));
54 };
55
56 _proto.addAll = function addAll(nodes) {
57 return this.withMutations(function (mutable) {
58 return nodes.reduce(function (ctx, definition) {
59 return ctx.add(definition);
60 }, mutable);
61 });
62 };
63 /**
64 * Apply a list of compiler transforms and return a new compiler context.
65 */
66
67
68 _proto.applyTransforms = function applyTransforms(transforms, reporter) {
69 var _this = this;
70
71 return require("./GraphQLCompilerProfiler").run('applyTransforms', function () {
72 return transforms.reduce(function (ctx, transform) {
73 return ctx.applyTransform(transform, reporter);
74 }, _this);
75 });
76 };
77 /**
78 * Applies a transform to this context, returning a new context.
79 *
80 * This is memoized such that applying the same sequence of transforms will
81 * not result in duplicated work.
82 */
83
84
85 _proto.applyTransform = function applyTransform(transform, reporter) {
86 var transformed = this._withTransform.get(transform);
87
88 if (!transformed) {
89 var start = process.hrtime();
90 transformed = require("./GraphQLCompilerProfiler").instrument(transform)(this);
91 var delta = process.hrtime(start);
92 var deltaMs = Math.round((delta[0] * 1e9 + delta[1]) / 1e6);
93 reporter && reporter.reportTime(transform.name, deltaMs);
94
95 this._withTransform.set(transform, transformed);
96 }
97
98 return transformed;
99 };
100
101 _proto.get = function get(name) {
102 return this._documents.get(name);
103 };
104
105 _proto.getFragment = function getFragment(name) {
106 var node = this._get(name);
107
108 if (node.kind !== 'Fragment') {
109 var childModule = name.substring(0, name.lastIndexOf('_'));
110 throw require("./GraphQLCompilerUserError").createUserError('GraphQLCompilerContext: Cannot find fragment `%s`.' + ' Please make sure the fragment exists in `%s`.', name, childModule);
111 }
112
113 return node;
114 };
115
116 _proto.getRoot = function getRoot(name) {
117 var node = this._get(name);
118
119 !(node.kind === 'Root') ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLCompilerContext: Expected `%s` to be a root, got `%s`.', name, node.kind) : require("fbjs/lib/invariant")(false) : void 0;
120 return node;
121 };
122
123 _proto.remove = function remove(name) {
124 return this._update(this._documents["delete"](name));
125 };
126
127 _proto.withMutations = function withMutations(fn) {
128 var mutableCopy = this._update(this._documents.asMutable());
129
130 mutableCopy._isMutable = true;
131 var result = fn(mutableCopy);
132 result._isMutable = false;
133 result._documents = result._documents.asImmutable();
134 return this._documents === result._documents ? this : result;
135 };
136
137 _proto._get = function _get(name) {
138 var document = this._documents.get(name);
139
140 !document ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLCompilerContext: Unknown document `%s`.', name) : require("fbjs/lib/invariant")(false) : void 0;
141 return document;
142 };
143
144 _proto._update = function _update(documents) {
145 var context = this._isMutable ? this : new GraphQLCompilerContext(this.serverSchema, this.clientSchema);
146 context._documents = documents;
147 return context;
148 };
149
150 return GraphQLCompilerContext;
151}();
152
153module.exports = GraphQLCompilerContext;
\No newline at end of file