introspectionFromSchema.js.flow 1.2 KB

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