Skip to content

Ekryd/sortgraphql

sortgraphql

Build Status Coverage Status Maven Central Quality Gate Status Licence

Maven plugin to sort GraphQL Schemas.

Description

The plugin will format and sort GraphQL schemas in a predefined way. The default sort order sections are:

  • All Directives first
  • The Query type
  • The Mutation type
  • The Subscription type
  • All Scalars
  • All Interfaces
  • All Unions
  • All Input types
  • All Types (beside Query, Mutation and Subscription)
  • All Enums

Within each section, the entities are sorted alphabetically. There are some parameters to suppress the alphabetical sorting, please submit a pull request or issue if you need more options.

The plugin should be able to handle custom directives and custom scalars. The plugin should also be able to sort multiple dependent graphql schema files as long as all entities are only defined in one schema.

Goals Overview

The SortGraphQL Plugin has one goal.

  • mvn sortgraphql:sort sorts the grapql file/files

Usage

Add the plugin to your pom-file to sort the schema each time you build the project. The plugin will execute by default in the Maven validate phase. Remember to set the src/main/resources/ path

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.github.ekryd.sortgraphql</groupId>
      <artifactId>sortgraphql-maven-plugin</artifactId>
      <version>@pom.version@</version>
      <configuration>
        <schemaFile>src/main/resources/mySchema.graphqls</schemaFile>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>sort</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

Parameters

Parameter Default value Description Example
<createBackupFile> true Should a backup copy be created for the sorted schema. <createBackupFile>false</createBackupFile>
<backupFileExtension> .bak Name of the file extension for the backup file. <backupFileExtension>.temp</backupFileExtension>
<encoding> UTF-8 Encoding for the files. <encoding>ASCII</encoding>
<individualSchemas> false By default, if multiple schema files are specified in schema files, then those schemas will be merged together during validation. This flag specifies that each schema should be validated individually. -Dsortgraphql.individualSchemas=true

<individualSchemas>true</individualSchemas>
<schemaFile> src/main/resources/schema.graphqls Location of the schema file. Remember to set the src/main/resources/ path. -Dsortgraphql.schemaFile="src/main/resources/main.graphqls"

<schemaFile>src/main/resources/main.graphqls</schemaFile>
<schemaFiles> <empty> Location of multiple graphql schema file that should be sorted. Overrides parameter schemaFile. The schema files can reference each other, but shared definitions are not allowed.
<schemaFiles>  <schemaFile>src/main/resources/queries.graphqls</schemaFile>   <schemaFile>src/main/resources/mutations.graphqls</schemaFile></schemaFiles>
<skip> false Set this to 'true' to bypass SortGraphQL plugin. -Dsortgraphql.skip=true

<skip>true</skip>
<skipFieldArgumentSorting> false Set this to 'true' to skip sorting the arguments for a field in a type. <skipFieldArgumentSorting>true</skipFieldArgumentSorting>
<skipUnionTypeSorting> false Set this to 'true' to skip sorting the types in a union. <skipUnionTypeSorting>true</skipUnionTypeSorting>
<generateHashDescriptions> false Use hash sign for descriptions, instead of keeping string literals (with quote character). <generateHashDescriptions>true</generateHashDescriptions>
<generateSchemaDefinition> false Generate the 'schema' definition element at the top of the schema, when generating the sorted schema file. <generateSchemaDefinition>true</generateSchemaDefinition>

Download

The plugin is hosted i Maven Central and will be downloaded automatically if you include it as a plugin in your pom file.

Version history

  • 2022-09-20: Released 1.1.0 Differentiate between descriptions and comments when processing files.
  • 2022-04-02: Released 1.0.0 Supports schema definitions properly.
  • 2021-10-11: Released 0.0.4 Added configuration parameters to validate individual schemas. Input types will also have directives on separate line.
  • 2021-06-06: Released 0.0.3 Added configuration parameters to customize sorting.
  • 2021-06-01: Released 0.0.2 Fully functional but with limited configuration for sorting. Try it out and tell me what you think