NoSchemaIntrospectionCustomRule.mjs 997 B

12345678910111213141516171819202122232425
  1. import { GraphQLError } from "../../../error/GraphQLError.mjs";
  2. import { getNamedType } from "../../../type/definition.mjs";
  3. import { isIntrospectionType } from "../../../type/introspection.mjs";
  4. /**
  5. * Prohibit introspection queries
  6. *
  7. * A GraphQL document is only valid if all fields selected are not fields that
  8. * return an introspection type.
  9. *
  10. * Note: This rule is optional and is not part of the Validation section of the
  11. * GraphQL Specification. This rule effectively disables introspection, which
  12. * does not reflect best practices and should only be done if absolutely necessary.
  13. */
  14. export function NoSchemaIntrospectionCustomRule(context) {
  15. return {
  16. Field: function Field(node) {
  17. var type = getNamedType(context.getType());
  18. if (type && isIntrospectionType(type)) {
  19. context.reportError(new GraphQLError("GraphQL introspection has been disabled, but the requested query contained the field \"".concat(node.name.value, "\"."), node));
  20. }
  21. }
  22. };
  23. }