buildClientSchema.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.buildClientSchema = buildClientSchema;
  6. var _objectValues = _interopRequireDefault(require("../polyfills/objectValues"));
  7. var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
  8. var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
  9. var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap"));
  10. var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
  11. var _parser = require("../language/parser");
  12. var _directives = require("../type/directives");
  13. var _scalars = require("../type/scalars");
  14. var _introspection = require("../type/introspection");
  15. var _schema = require("../type/schema");
  16. var _definition = require("../type/definition");
  17. var _valueFromAST = require("./valueFromAST");
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. /**
  20. * Build a GraphQLSchema for use by client tools.
  21. *
  22. * Given the result of a client running the introspection query, creates and
  23. * returns a GraphQLSchema instance which can be then used with all graphql-js
  24. * tools, but cannot be used to execute a query, as introspection does not
  25. * represent the "resolver", "parse" or "serialize" functions or any other
  26. * server-internal mechanisms.
  27. *
  28. * This function expects a complete introspection result. Don't forget to check
  29. * the "errors" field of a server response before calling this function.
  30. */
  31. function buildClientSchema(introspection, options) {
  32. (0, _isObjectLike.default)(introspection) && (0, _isObjectLike.default)(introspection.__schema) || (0, _devAssert.default)(0, 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ' + (0, _inspect.default)(introspection)); // Get the schema from the introspection result.
  33. var schemaIntrospection = introspection.__schema; // Iterate through all types, getting the type definition for each.
  34. var typeMap = (0, _keyValMap.default)(schemaIntrospection.types, function (typeIntrospection) {
  35. return typeIntrospection.name;
  36. }, function (typeIntrospection) {
  37. return buildType(typeIntrospection);
  38. });
  39. for (var _i2 = 0, _ref2 = [].concat(_scalars.specifiedScalarTypes, _introspection.introspectionTypes); _i2 < _ref2.length; _i2++) {
  40. var stdType = _ref2[_i2];
  41. if (typeMap[stdType.name]) {
  42. typeMap[stdType.name] = stdType;
  43. }
  44. } // Get the root Query, Mutation, and Subscription types.
  45. var queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null;
  46. var mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null;
  47. var subscriptionType = schemaIntrospection.subscriptionType ? getObjectType(schemaIntrospection.subscriptionType) : null; // Get the directives supported by Introspection, assuming empty-set if
  48. // directives were not queried for.
  49. var directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : []; // Then produce and return a Schema with these types.
  50. return new _schema.GraphQLSchema({
  51. query: queryType,
  52. mutation: mutationType,
  53. subscription: subscriptionType,
  54. types: (0, _objectValues.default)(typeMap),
  55. directives: directives,
  56. assumeValid: options && options.assumeValid,
  57. allowedLegacyNames: options && options.allowedLegacyNames
  58. }); // Given a type reference in introspection, return the GraphQLType instance.
  59. // preferring cached instances before building new instances.
  60. function getType(typeRef) {
  61. if (typeRef.kind === _introspection.TypeKind.LIST) {
  62. var itemRef = typeRef.ofType;
  63. if (!itemRef) {
  64. throw new Error('Decorated type deeper than introspection query.');
  65. }
  66. return (0, _definition.GraphQLList)(getType(itemRef));
  67. }
  68. if (typeRef.kind === _introspection.TypeKind.NON_NULL) {
  69. var nullableRef = typeRef.ofType;
  70. if (!nullableRef) {
  71. throw new Error('Decorated type deeper than introspection query.');
  72. }
  73. var nullableType = getType(nullableRef);
  74. return (0, _definition.GraphQLNonNull)((0, _definition.assertNullableType)(nullableType));
  75. }
  76. if (!typeRef.name) {
  77. throw new Error('Unknown type reference: ' + (0, _inspect.default)(typeRef));
  78. }
  79. return getNamedType(typeRef.name);
  80. }
  81. function getNamedType(typeName) {
  82. var type = typeMap[typeName];
  83. if (!type) {
  84. throw new Error("Invalid or incomplete schema, unknown type: ".concat(typeName, ". Ensure that a full introspection query is used in order to build a client schema."));
  85. }
  86. return type;
  87. }
  88. function getInputType(typeRef) {
  89. var type = getType(typeRef);
  90. if ((0, _definition.isInputType)(type)) {
  91. return type;
  92. }
  93. throw new Error('Introspection must provide input type for arguments, but received: ' + (0, _inspect.default)(type) + '.');
  94. }
  95. function getOutputType(typeRef) {
  96. var type = getType(typeRef);
  97. if ((0, _definition.isOutputType)(type)) {
  98. return type;
  99. }
  100. throw new Error('Introspection must provide output type for fields, but received: ' + (0, _inspect.default)(type) + '.');
  101. }
  102. function getObjectType(typeRef) {
  103. var type = getType(typeRef);
  104. return (0, _definition.assertObjectType)(type);
  105. }
  106. function getInterfaceType(typeRef) {
  107. var type = getType(typeRef);
  108. return (0, _definition.assertInterfaceType)(type);
  109. } // Given a type's introspection result, construct the correct
  110. // GraphQLType instance.
  111. function buildType(type) {
  112. if (type && type.name && type.kind) {
  113. switch (type.kind) {
  114. case _introspection.TypeKind.SCALAR:
  115. return buildScalarDef(type);
  116. case _introspection.TypeKind.OBJECT:
  117. return buildObjectDef(type);
  118. case _introspection.TypeKind.INTERFACE:
  119. return buildInterfaceDef(type);
  120. case _introspection.TypeKind.UNION:
  121. return buildUnionDef(type);
  122. case _introspection.TypeKind.ENUM:
  123. return buildEnumDef(type);
  124. case _introspection.TypeKind.INPUT_OBJECT:
  125. return buildInputObjectDef(type);
  126. }
  127. }
  128. throw new Error('Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema:' + (0, _inspect.default)(type));
  129. }
  130. function buildScalarDef(scalarIntrospection) {
  131. return new _definition.GraphQLScalarType({
  132. name: scalarIntrospection.name,
  133. description: scalarIntrospection.description
  134. });
  135. }
  136. function buildObjectDef(objectIntrospection) {
  137. if (!objectIntrospection.interfaces) {
  138. throw new Error('Introspection result missing interfaces: ' + (0, _inspect.default)(objectIntrospection));
  139. }
  140. return new _definition.GraphQLObjectType({
  141. name: objectIntrospection.name,
  142. description: objectIntrospection.description,
  143. interfaces: function interfaces() {
  144. return objectIntrospection.interfaces.map(getInterfaceType);
  145. },
  146. fields: function fields() {
  147. return buildFieldDefMap(objectIntrospection);
  148. }
  149. });
  150. }
  151. function buildInterfaceDef(interfaceIntrospection) {
  152. return new _definition.GraphQLInterfaceType({
  153. name: interfaceIntrospection.name,
  154. description: interfaceIntrospection.description,
  155. fields: function fields() {
  156. return buildFieldDefMap(interfaceIntrospection);
  157. }
  158. });
  159. }
  160. function buildUnionDef(unionIntrospection) {
  161. if (!unionIntrospection.possibleTypes) {
  162. throw new Error('Introspection result missing possibleTypes: ' + (0, _inspect.default)(unionIntrospection));
  163. }
  164. return new _definition.GraphQLUnionType({
  165. name: unionIntrospection.name,
  166. description: unionIntrospection.description,
  167. types: function types() {
  168. return unionIntrospection.possibleTypes.map(getObjectType);
  169. }
  170. });
  171. }
  172. function buildEnumDef(enumIntrospection) {
  173. if (!enumIntrospection.enumValues) {
  174. throw new Error('Introspection result missing enumValues: ' + (0, _inspect.default)(enumIntrospection));
  175. }
  176. return new _definition.GraphQLEnumType({
  177. name: enumIntrospection.name,
  178. description: enumIntrospection.description,
  179. values: (0, _keyValMap.default)(enumIntrospection.enumValues, function (valueIntrospection) {
  180. return valueIntrospection.name;
  181. }, function (valueIntrospection) {
  182. return {
  183. description: valueIntrospection.description,
  184. deprecationReason: valueIntrospection.deprecationReason
  185. };
  186. })
  187. });
  188. }
  189. function buildInputObjectDef(inputObjectIntrospection) {
  190. if (!inputObjectIntrospection.inputFields) {
  191. throw new Error('Introspection result missing inputFields: ' + (0, _inspect.default)(inputObjectIntrospection));
  192. }
  193. return new _definition.GraphQLInputObjectType({
  194. name: inputObjectIntrospection.name,
  195. description: inputObjectIntrospection.description,
  196. fields: function fields() {
  197. return buildInputValueDefMap(inputObjectIntrospection.inputFields);
  198. }
  199. });
  200. }
  201. function buildFieldDefMap(typeIntrospection) {
  202. if (!typeIntrospection.fields) {
  203. throw new Error('Introspection result missing fields: ' + (0, _inspect.default)(typeIntrospection));
  204. }
  205. return (0, _keyValMap.default)(typeIntrospection.fields, function (fieldIntrospection) {
  206. return fieldIntrospection.name;
  207. }, function (fieldIntrospection) {
  208. if (!fieldIntrospection.args) {
  209. throw new Error('Introspection result missing field args: ' + (0, _inspect.default)(fieldIntrospection));
  210. }
  211. return {
  212. description: fieldIntrospection.description,
  213. deprecationReason: fieldIntrospection.deprecationReason,
  214. type: getOutputType(fieldIntrospection.type),
  215. args: buildInputValueDefMap(fieldIntrospection.args)
  216. };
  217. });
  218. }
  219. function buildInputValueDefMap(inputValueIntrospections) {
  220. return (0, _keyValMap.default)(inputValueIntrospections, function (inputValue) {
  221. return inputValue.name;
  222. }, buildInputValue);
  223. }
  224. function buildInputValue(inputValueIntrospection) {
  225. var type = getInputType(inputValueIntrospection.type);
  226. var defaultValue = inputValueIntrospection.defaultValue ? (0, _valueFromAST.valueFromAST)((0, _parser.parseValue)(inputValueIntrospection.defaultValue), type) : undefined;
  227. return {
  228. description: inputValueIntrospection.description,
  229. type: type,
  230. defaultValue: defaultValue
  231. };
  232. }
  233. function buildDirective(directiveIntrospection) {
  234. if (!directiveIntrospection.args) {
  235. throw new Error('Introspection result missing directive args: ' + (0, _inspect.default)(directiveIntrospection));
  236. }
  237. if (!directiveIntrospection.locations) {
  238. throw new Error('Introspection result missing directive locations: ' + (0, _inspect.default)(directiveIntrospection));
  239. }
  240. return new _directives.GraphQLDirective({
  241. name: directiveIntrospection.name,
  242. description: directiveIntrospection.description,
  243. locations: directiveIntrospection.locations.slice(),
  244. args: buildInputValueDefMap(directiveIntrospection.args)
  245. });
  246. }
  247. }