introspectionFromSchema.js.flow 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. specifiedByUrl: true,
  26. directiveIsRepeatable: true,
  27. schemaDescription: true,
  28. inputValueDeprecation: true,
  29. ...options,
  30. };
  31. const document = parse(getIntrospectionQuery(optionsWithDefaults));
  32. const result = executeSync({ schema, document });
  33. invariant(!result.errors && result.data);
  34. return (result.data: any);
  35. }