graphql#ValidationRule TypeScript Examples

The following examples show how to use graphql#ValidationRule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: graphql-js-validation.ts    From graphql-eslint with MIT License 5 votes vote down vote up
function validateDocument(
  context: GraphQLESLintRuleContext,
  schema: GraphQLSchema | null = null,
  documentNode: DocumentNode,
  rule: ValidationRule
): void {
  if (documentNode.definitions.length === 0) {
    return;
  }
  try {
    const validationErrors = schema
      ? validate(schema, documentNode, [rule])
      : validateSDL(documentNode, null, [rule as any]);

    for (const error of validationErrors) {
      const { line, column } = error.locations[0];
      const sourceCode = context.getSourceCode();
      const { tokens } = sourceCode.ast;
      const token = tokens.find(token => token.loc.start.line === line && token.loc.start.column === column - 1);

      let loc: { line: number; column: number } | AST.SourceLocation = {
        line,
        column: column - 1,
      };
      if (token) {
        loc =
          // if cursor on `@` symbol than use next node
          (token.type as any) === '@' ? sourceCode.getNodeByRangeIndex(token.range[1] + 1).loc : token.loc;
      }

      context.report({
        loc,
        message: error.message,
      });
    }
  } catch (e) {
    context.report({
      loc: REPORT_ON_FIRST_CHARACTER,
      message: e.message,
    });
  }
}
Example #2
Source File: graphql-js-validation.ts    From graphql-eslint with MIT License 5 votes vote down vote up
validationToRule = (
  ruleId: string,
  ruleName: string,
  docs: GraphQLESLintRule['meta']['docs'],
  getDocumentNode?: GetDocumentNode,
  schema: JSONSchema4 | JSONSchema4[] = []
): Record<typeof ruleId, GraphQLESLintRule<any, true>> => {
  let ruleFn: null | ValidationRule = null;

  try {
    ruleFn = require(`graphql/validation/rules/${ruleName}Rule`)[`${ruleName}Rule`];
  } catch {
    try {
      ruleFn = require(`graphql/validation/rules/${ruleName}`)[`${ruleName}Rule`];
    } catch {
      ruleFn = require('graphql/validation')[`${ruleName}Rule`];
    }
  }
  return {
    [ruleId]: {
      meta: {
        docs: {
          recommended: true,
          ...docs,
          graphQLJSRuleName: ruleName,
          url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${ruleId}.md`,
          description: `${docs.description}\n\n> This rule is a wrapper around a \`graphql-js\` validation function.`,
        },
        schema,
      },
      create(context) {
        if (!ruleFn) {
          logger.warn(
            `Rule "${ruleId}" depends on a GraphQL validation rule "${ruleName}" but it's not available in the "graphql" version you are using. Skipping…`
          );
          return {};
        }

        return {
          Document(node) {
            const schema = docs.requiresSchema ? requireGraphQLSchemaFromContext(ruleId, context) : null;

            const documentNode = getDocumentNode
              ? getDocumentNode({ ruleId, context, node: node.rawNode() })
              : node.rawNode();

            validateDocument(context, schema, documentNode, ruleFn);
          },
        };
      },
    },
  };
}