buildClientSchema.js.flow 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // @flow strict
  2. import objectValues from '../polyfills/objectValues';
  3. import inspect from '../jsutils/inspect';
  4. import devAssert from '../jsutils/devAssert';
  5. import keyValMap from '../jsutils/keyValMap';
  6. import isObjectLike from '../jsutils/isObjectLike';
  7. import { parseValue } from '../language/parser';
  8. import { GraphQLDirective } from '../type/directives';
  9. import { specifiedScalarTypes } from '../type/scalars';
  10. import { introspectionTypes, TypeKind } from '../type/introspection';
  11. import {
  12. type GraphQLSchemaValidationOptions,
  13. GraphQLSchema,
  14. } from '../type/schema';
  15. import {
  16. type GraphQLType,
  17. type GraphQLInputType,
  18. type GraphQLOutputType,
  19. type GraphQLNamedType,
  20. isInputType,
  21. isOutputType,
  22. GraphQLScalarType,
  23. GraphQLObjectType,
  24. GraphQLInterfaceType,
  25. GraphQLUnionType,
  26. GraphQLEnumType,
  27. GraphQLInputObjectType,
  28. GraphQLList,
  29. GraphQLNonNull,
  30. assertNullableType,
  31. assertObjectType,
  32. assertInterfaceType,
  33. } from '../type/definition';
  34. import { valueFromAST } from './valueFromAST';
  35. import {
  36. type IntrospectionQuery,
  37. type IntrospectionType,
  38. type IntrospectionScalarType,
  39. type IntrospectionObjectType,
  40. type IntrospectionInterfaceType,
  41. type IntrospectionUnionType,
  42. type IntrospectionEnumType,
  43. type IntrospectionInputObjectType,
  44. type IntrospectionTypeRef,
  45. type IntrospectionInputTypeRef,
  46. type IntrospectionOutputTypeRef,
  47. type IntrospectionNamedTypeRef,
  48. } from './introspectionQuery';
  49. type Options = {|
  50. ...GraphQLSchemaValidationOptions,
  51. |};
  52. /**
  53. * Build a GraphQLSchema for use by client tools.
  54. *
  55. * Given the result of a client running the introspection query, creates and
  56. * returns a GraphQLSchema instance which can be then used with all graphql-js
  57. * tools, but cannot be used to execute a query, as introspection does not
  58. * represent the "resolver", "parse" or "serialize" functions or any other
  59. * server-internal mechanisms.
  60. *
  61. * This function expects a complete introspection result. Don't forget to check
  62. * the "errors" field of a server response before calling this function.
  63. */
  64. export function buildClientSchema(
  65. introspection: IntrospectionQuery,
  66. options?: Options,
  67. ): GraphQLSchema {
  68. devAssert(
  69. isObjectLike(introspection) && isObjectLike(introspection.__schema),
  70. 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ' +
  71. inspect(introspection),
  72. );
  73. // Get the schema from the introspection result.
  74. const schemaIntrospection = introspection.__schema;
  75. // Iterate through all types, getting the type definition for each.
  76. const typeMap = keyValMap(
  77. schemaIntrospection.types,
  78. typeIntrospection => typeIntrospection.name,
  79. typeIntrospection => buildType(typeIntrospection),
  80. );
  81. for (const stdType of [...specifiedScalarTypes, ...introspectionTypes]) {
  82. if (typeMap[stdType.name]) {
  83. typeMap[stdType.name] = stdType;
  84. }
  85. }
  86. // Get the root Query, Mutation, and Subscription types.
  87. const queryType = schemaIntrospection.queryType
  88. ? getObjectType(schemaIntrospection.queryType)
  89. : null;
  90. const mutationType = schemaIntrospection.mutationType
  91. ? getObjectType(schemaIntrospection.mutationType)
  92. : null;
  93. const subscriptionType = schemaIntrospection.subscriptionType
  94. ? getObjectType(schemaIntrospection.subscriptionType)
  95. : null;
  96. // Get the directives supported by Introspection, assuming empty-set if
  97. // directives were not queried for.
  98. const directives = schemaIntrospection.directives
  99. ? schemaIntrospection.directives.map(buildDirective)
  100. : [];
  101. // Then produce and return a Schema with these types.
  102. return new GraphQLSchema({
  103. query: queryType,
  104. mutation: mutationType,
  105. subscription: subscriptionType,
  106. types: objectValues(typeMap),
  107. directives,
  108. assumeValid: options && options.assumeValid,
  109. allowedLegacyNames: options && options.allowedLegacyNames,
  110. });
  111. // Given a type reference in introspection, return the GraphQLType instance.
  112. // preferring cached instances before building new instances.
  113. function getType(typeRef: IntrospectionTypeRef): GraphQLType {
  114. if (typeRef.kind === TypeKind.LIST) {
  115. const itemRef = typeRef.ofType;
  116. if (!itemRef) {
  117. throw new Error('Decorated type deeper than introspection query.');
  118. }
  119. return GraphQLList(getType(itemRef));
  120. }
  121. if (typeRef.kind === TypeKind.NON_NULL) {
  122. const nullableRef = typeRef.ofType;
  123. if (!nullableRef) {
  124. throw new Error('Decorated type deeper than introspection query.');
  125. }
  126. const nullableType = getType(nullableRef);
  127. return GraphQLNonNull(assertNullableType(nullableType));
  128. }
  129. if (!typeRef.name) {
  130. throw new Error('Unknown type reference: ' + inspect(typeRef));
  131. }
  132. return getNamedType(typeRef.name);
  133. }
  134. function getNamedType(typeName: string): GraphQLNamedType {
  135. const type = typeMap[typeName];
  136. if (!type) {
  137. throw new Error(
  138. `Invalid or incomplete schema, unknown type: ${typeName}. Ensure that a full introspection query is used in order to build a client schema.`,
  139. );
  140. }
  141. return type;
  142. }
  143. function getInputType(typeRef: IntrospectionInputTypeRef): GraphQLInputType {
  144. const type = getType(typeRef);
  145. if (isInputType(type)) {
  146. return type;
  147. }
  148. throw new Error(
  149. 'Introspection must provide input type for arguments, but received: ' +
  150. inspect(type) +
  151. '.',
  152. );
  153. }
  154. function getOutputType(
  155. typeRef: IntrospectionOutputTypeRef,
  156. ): GraphQLOutputType {
  157. const type = getType(typeRef);
  158. if (isOutputType(type)) {
  159. return type;
  160. }
  161. throw new Error(
  162. 'Introspection must provide output type for fields, but received: ' +
  163. inspect(type) +
  164. '.',
  165. );
  166. }
  167. function getObjectType(
  168. typeRef: IntrospectionNamedTypeRef<IntrospectionObjectType>,
  169. ): GraphQLObjectType {
  170. const type = getType(typeRef);
  171. return assertObjectType(type);
  172. }
  173. function getInterfaceType(
  174. typeRef: IntrospectionTypeRef,
  175. ): GraphQLInterfaceType {
  176. const type = getType(typeRef);
  177. return assertInterfaceType(type);
  178. }
  179. // Given a type's introspection result, construct the correct
  180. // GraphQLType instance.
  181. function buildType(type: IntrospectionType): GraphQLNamedType {
  182. if (type && type.name && type.kind) {
  183. switch (type.kind) {
  184. case TypeKind.SCALAR:
  185. return buildScalarDef(type);
  186. case TypeKind.OBJECT:
  187. return buildObjectDef(type);
  188. case TypeKind.INTERFACE:
  189. return buildInterfaceDef(type);
  190. case TypeKind.UNION:
  191. return buildUnionDef(type);
  192. case TypeKind.ENUM:
  193. return buildEnumDef(type);
  194. case TypeKind.INPUT_OBJECT:
  195. return buildInputObjectDef(type);
  196. }
  197. }
  198. throw new Error(
  199. 'Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema:' +
  200. inspect(type),
  201. );
  202. }
  203. function buildScalarDef(
  204. scalarIntrospection: IntrospectionScalarType,
  205. ): GraphQLScalarType {
  206. return new GraphQLScalarType({
  207. name: scalarIntrospection.name,
  208. description: scalarIntrospection.description,
  209. });
  210. }
  211. function buildObjectDef(
  212. objectIntrospection: IntrospectionObjectType,
  213. ): GraphQLObjectType {
  214. if (!objectIntrospection.interfaces) {
  215. throw new Error(
  216. 'Introspection result missing interfaces: ' +
  217. inspect(objectIntrospection),
  218. );
  219. }
  220. return new GraphQLObjectType({
  221. name: objectIntrospection.name,
  222. description: objectIntrospection.description,
  223. interfaces: () => objectIntrospection.interfaces.map(getInterfaceType),
  224. fields: () => buildFieldDefMap(objectIntrospection),
  225. });
  226. }
  227. function buildInterfaceDef(
  228. interfaceIntrospection: IntrospectionInterfaceType,
  229. ): GraphQLInterfaceType {
  230. return new GraphQLInterfaceType({
  231. name: interfaceIntrospection.name,
  232. description: interfaceIntrospection.description,
  233. fields: () => buildFieldDefMap(interfaceIntrospection),
  234. });
  235. }
  236. function buildUnionDef(
  237. unionIntrospection: IntrospectionUnionType,
  238. ): GraphQLUnionType {
  239. if (!unionIntrospection.possibleTypes) {
  240. throw new Error(
  241. 'Introspection result missing possibleTypes: ' +
  242. inspect(unionIntrospection),
  243. );
  244. }
  245. return new GraphQLUnionType({
  246. name: unionIntrospection.name,
  247. description: unionIntrospection.description,
  248. types: () => unionIntrospection.possibleTypes.map(getObjectType),
  249. });
  250. }
  251. function buildEnumDef(
  252. enumIntrospection: IntrospectionEnumType,
  253. ): GraphQLEnumType {
  254. if (!enumIntrospection.enumValues) {
  255. throw new Error(
  256. 'Introspection result missing enumValues: ' +
  257. inspect(enumIntrospection),
  258. );
  259. }
  260. return new GraphQLEnumType({
  261. name: enumIntrospection.name,
  262. description: enumIntrospection.description,
  263. values: keyValMap(
  264. enumIntrospection.enumValues,
  265. valueIntrospection => valueIntrospection.name,
  266. valueIntrospection => ({
  267. description: valueIntrospection.description,
  268. deprecationReason: valueIntrospection.deprecationReason,
  269. }),
  270. ),
  271. });
  272. }
  273. function buildInputObjectDef(
  274. inputObjectIntrospection: IntrospectionInputObjectType,
  275. ): GraphQLInputObjectType {
  276. if (!inputObjectIntrospection.inputFields) {
  277. throw new Error(
  278. 'Introspection result missing inputFields: ' +
  279. inspect(inputObjectIntrospection),
  280. );
  281. }
  282. return new GraphQLInputObjectType({
  283. name: inputObjectIntrospection.name,
  284. description: inputObjectIntrospection.description,
  285. fields: () => buildInputValueDefMap(inputObjectIntrospection.inputFields),
  286. });
  287. }
  288. function buildFieldDefMap(typeIntrospection) {
  289. if (!typeIntrospection.fields) {
  290. throw new Error(
  291. 'Introspection result missing fields: ' + inspect(typeIntrospection),
  292. );
  293. }
  294. return keyValMap(
  295. typeIntrospection.fields,
  296. fieldIntrospection => fieldIntrospection.name,
  297. fieldIntrospection => {
  298. if (!fieldIntrospection.args) {
  299. throw new Error(
  300. 'Introspection result missing field args: ' +
  301. inspect(fieldIntrospection),
  302. );
  303. }
  304. return {
  305. description: fieldIntrospection.description,
  306. deprecationReason: fieldIntrospection.deprecationReason,
  307. type: getOutputType(fieldIntrospection.type),
  308. args: buildInputValueDefMap(fieldIntrospection.args),
  309. };
  310. },
  311. );
  312. }
  313. function buildInputValueDefMap(inputValueIntrospections) {
  314. return keyValMap(
  315. inputValueIntrospections,
  316. inputValue => inputValue.name,
  317. buildInputValue,
  318. );
  319. }
  320. function buildInputValue(inputValueIntrospection) {
  321. const type = getInputType(inputValueIntrospection.type);
  322. const defaultValue = inputValueIntrospection.defaultValue
  323. ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type)
  324. : undefined;
  325. return {
  326. description: inputValueIntrospection.description,
  327. type,
  328. defaultValue,
  329. };
  330. }
  331. function buildDirective(directiveIntrospection) {
  332. if (!directiveIntrospection.args) {
  333. throw new Error(
  334. 'Introspection result missing directive args: ' +
  335. inspect(directiveIntrospection),
  336. );
  337. }
  338. if (!directiveIntrospection.locations) {
  339. throw new Error(
  340. 'Introspection result missing directive locations: ' +
  341. inspect(directiveIntrospection),
  342. );
  343. }
  344. return new GraphQLDirective({
  345. name: directiveIntrospection.name,
  346. description: directiveIntrospection.description,
  347. locations: directiveIntrospection.locations.slice(),
  348. args: buildInputValueDefMap(directiveIntrospection.args),
  349. });
  350. }
  351. }