introspectionFromSchema.mjs 1.0 KB

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