Skip to content

kirpi4ik/graphql-java-linter

Repository files navigation

License

License

Build project GitHub Release Maven Central
Nexus snapshot

Usage

  1. Create yaml configuration file E.g: linter.yaml
# GraphQL Linter Configuration
level: WARN
failWhenExceedWarningMax: true
registry:
  dsl:
    uri: src/test/resources/rules/
schema:
  # local schema folder
  local:
    uri: /<schema folder>/
    extensions:
      - "graphql"
      - "graphqls"
  # OR you can use remote location(if local exist remote will be ignored)
  endpoint:
    url: "http://localhost:8181/graphql"
    headers:
      user-agent: "Linter"
      Authorization: "Bearer <token>"
rules:
  field_name_first_lowercase:
    level: WARN
    failWhenExceed: 2
  defined_types_are_used:
    level: WARN
    disable: true
    failWhenExceed: 10

Command line

Run from command line:

Download from packages the latest version.

java -jar graphql-java-linter-1.2-cli.jar linter.yaml

In CI/CD flow via unit tests

Add dependency

build.gradle

dependencies {
    test 'org.myhab.tools:graphql-java-linter:1.2'
}

pom.xml

<dependency>
    <groupId>org.myhab.tools</groupId>
    <artifactId>graphql-java-linter</artifactId>
    <version>1.2</version>
    <scope>test</scope>
</dependency>

junit

import graphql.linter.LintRunner;
import org.junit.Test;

import java.nio.file.Paths;

public class GraphQLLinterTest {
    @Test
    public void lint() {
        LintRunner runner = new LintRunner(Paths.get("linter.yaml").toUri());
        runner.run();
    }
}

DSL support

Besides linter's rules which are embedded, you can define own set of rules using custom groovy DSL

arguments_has_descriptions

rule(["FIELD"]) {
    node.children.each { schemaElement ->
        if (schemaElement instanceof GraphQLArgument) {
            if (schemaElement.description == null) {
                fail(parent, node, "Argument `${parent.name}.${node.name}(${schemaElement.name})` missing description.")
            }
        }

    }
}

File name will be used as the rule name. Most important object/methods used during validation and available also in DSL are :

  • parent - Parent element [GraphQLNamedSchemaElement], in case of type - it will be null
  • node - Current element [GraphQLNamedSchemaElement]
  • fail(parent, node, message) - Record the failure into execution context(validation queue will be not interrupted).

In configuration file you will have to specify folder location which contains the dsl rules

registry:
  dsl:
    uri: src/test/resources/rules/