introspectionFromSchema.js.flow 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. // @flow strict
  2. import invariant from '../jsutils/invariant';
  3. import isPromise from '../jsutils/isPromise';
  4. import { parse } from '../language/parser';
  5. import { execute } from '../execution/execute';
  6. import { type GraphQLSchema } from '../type/schema';
  7. import {
  8. type IntrospectionQuery,
  9. type IntrospectionOptions,
  10. getIntrospectionQuery,
  11. } from './introspectionQuery';
  12. /**
  13. * Build an IntrospectionQuery from a GraphQLSchema
  14. *
  15. * IntrospectionQuery is useful for utilities that care about type and field
  16. * relationships, but do not need to traverse through those relationships.
  17. *
  18. * This is the inverse of buildClientSchema. The primary use case is outside
  19. * of the server context, for instance when doing schema comparisons.
  20. */
  21. export function introspectionFromSchema(
  22. schema: GraphQLSchema,
  23. options?: IntrospectionOptions,
  24. ): IntrospectionQuery {
  25. const queryAST = parse(getIntrospectionQuery(options));
  26. const result = execute(schema, queryAST);
  27. invariant(!isPromise(result) && !result.errors && result.data);
  28. return (result.data: any);
  29. }