NoSchemaIntrospectionCustomRule.js 1.2 KB

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