提交 d27a7396 编写于 作者: B Bogdan Kobylynskyi

Remove code related to sbt plugin

上级 d2b9e752
......@@ -49,12 +49,3 @@ Please follow the steps below in order to make the changes:
```
7. Make sure that `example` projects are compiling and running.
### For the sbt plugin
1. Publish the core library locally. See the instructions at the bottom of build.gradle discussing the necessary process due to Gradle bug
2. Publish the sbt plugin locally
```shell script
cd plugins/sbt
sbt publishLocal
```
# SBT GraphQL Java codegen plugin #
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
This document describes the SBT plugin for graphql-java-codegen.
This plugin does not yet have all the features of the [Maven plugin](../maven/README.md) and [Gradle plugin](../gradle/README.md), but please feel free to send a PR to add missing options.
### Description
The GraphQL Java codegen library used by this plugin is able to generate the following classes based on your GraphQL schema:
* Interfaces for GraphQL queries, mutations and subscriptions
* Interfaces for GraphQL unions
* POJO classes for GraphQL types/inputs
* Enum classes for GraphQL enums
* Interface Resolvers for GraphQL type fields
* Client Request classes for GraphQL queries, mutations and subscriptions
### Plugin Setup and Configuration
```
addSbtPlugin("io.github.kobylynskyi" % "sbt-graphql-java-codegen" % "1.8.1-NOT-YET-RELEASED")
```
### Plugin Options
| Option | Data Type | Default value | Description |
| :---------------------------------------------: | :----------------: | :-------------------------------------------: | ----------- |
| `graphqlSchemaPaths` | List(String) | (falls back to `graphqlSchemas`) | GraphQL schema locations. You can supply multiple paths to GraphQL schemas. To include many schemas from a folder hierarchy, use the `graphqlSchemas` block instead. |
| `graphqlApiPackageName` | String | Empty | Java package for generated api classes (Query, Mutation, Subscription). |
| `graphqlModelPackageName` | String | Empty | Java package for generated model classes (type, input, interface, enum, union). |
| `graphqlGenerateBuilder` | Boolean | True | Specifies whether generated model classes should have builder. |
| `graphqlGenerateApis` | Boolean | True | Specifies whether api classes should be generated as well as model classes. |
| `graphqlGenerateAsyncApi` | Boolean | False | If true, then wrap type into `java.util.concurrent.CompletableFuture` or `subscriptionReturnType` |
| `graphqlGenerateDataFetchingEnvArgInApis` | Boolean | False | If true, then `graphql.schema.DataFetchingEnvironment env` will be added as a last argument to all methods of root type resolvers and field resolvers. |
| `graphqlGenerateEqualsAndHashCode` | Boolean | False | Specifies whether generated model classes should have equals and hashCode methods defined. |
| `graphqlGenerateToString` | Boolean | False | Specifies whether generated model classes should have toString method defined. |
| `graphqlModelNamePrefix` | String | Empty | Sets the prefix for GraphQL model classes (type, input, interface, enum, union). |
| `graphqlModelNameSuffix` | String | Empty | Sets the suffix for GraphQL model classes (type, input, interface, enum, union). |
| `graphqlModelValidationAnnotation` | String | @javax.validation.<br>constraints.NotNull | Annotation for mandatory (NonNull) fields. Can be None/empty. |
| `graphqlGenerateParameterizedFieldsResolvers` | Boolean | True | If true, then generate separate `Resolver` interface for parametrized fields. If false, then add field to the type definition and ignore field parameters. |
| `graphqlGenerateExtensionFieldsResolvers` | Boolean | False | Specifies whether all fields in extensions (<code>extend type</code> and <code>extend interface</code>) should be present in Resolver interface instead of the type class itself. |
| `graphqlGenerateRequests` | Boolean | False | Specifies whether client-side classes should be generated for each query, mutation and subscription. This includes: `Request` class (contains input data) and `ResponseProjection` class (contains response fields). |
| `graphqlRequestSuffix` | String | Request | Sets the suffix for `Request` classes. |
| `graphqlResponseProjectionSuffix` | String | ResponseProjection | Sets the suffix for `ResponseProjection` classes. |
### Different configurations for graphql schemas
Currently, if you want to have different configuration for different `.graphqls` files (e.g.: different javaPackage, outputDir, etc.), then you will need to create an SBT sub-project for each one.
sbtPlugin := true
name := "sbt-graphql-java-codegen"
organization := "io.github.kobylynskyi"
description := "Plugin for generating Java code based on GraphQL schema"
libraryDependencies += "io.github.kobylynskyi" % "graphql-java-codegen" % version.value
enablePlugins(SbtPlugin)
scriptedLaunchOpts := { scriptedLaunchOpts.value ++
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
}
scriptedBufferLog := false
licenses := Seq("MIT License" -> url("https://github.com/kobylynskyi/graphql-java-codegen/blob/master/LICENSE.md"))
bintrayOrganization := None
bintrayRepository := "sbt-plugins"
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.6")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.13")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.0")
package io.github.kobylynskyi.graphql.codegen
import com.kobylynskyi.graphql.codegen.GraphQLCodegen
import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants;
import com.kobylynskyi.graphql.codegen.model.MappingConfig
import com.kobylynskyi.graphql.codegen.supplier.MappingConfigSupplier
import com.kobylynskyi.graphql.codegen.supplier.SchemaFinder
import scala.collection.JavaConverters
import sbt._
import sbt.Keys._
object GraphQLCodegenSbtPlugin extends AutoPlugin {
// by defining autoImport, the settings are automatically imported into user's `*.sbt`
object autoImport {
// configuration points, like the built-in `version`, `libraryDependencies`, or `compile`
val graphql = taskKey[Seq[File]]("Generate GraphQL code.")
val graphqlSchemaPaths = settingKey[Seq[String]]("Locations of GraphQL schemas.")
val graphqlModelNamePrefix = settingKey[Option[String]]("Suffix to append to the model class names.")
val graphqlModelNameSuffix = settingKey[Option[String]]("Suffix to append to the model class names.")
val graphqlApiPackageName = settingKey[Option[String]]("Java package to use when generating the API classes.")
val graphqlModelPackageName = settingKey[Option[String]]("Java package to use when generating the model classes.")
val graphqlGenerateBuilder = settingKey[Boolean]("Specifies whether generated model classes should have builder.")
val graphqlGenerateApis = settingKey[Boolean]("Specifies whether api classes should be generated as well as model classes.")
val graphqlGenerateEqualsAndHashCode = settingKey[Boolean]("Specifies whether generated model classes should have equals and hashCode methods defined.")
val graphqlGenerateToString = settingKey[Boolean]("Specifies whether generated model classes should have toString method defined.")
val graphqlGenerateAsyncApi = settingKey[Boolean]("If true, then wrap type into java.util.concurrent.CompletableFuture or subscriptionReturnType")
val graphqlModelValidationAnnotation = settingKey[Option[String]]("Annotation for mandatory (NonNull) fields. Can be None/empty.")
val graphqlGenerateParameterizedFieldsResolvers = settingKey[Boolean]("If true, then generate separate Resolver interface for parametrized fields. If false, then add field to the type definition and ignore field parameters.")
val graphqlGenerateExtensionFieldsResolvers = settingKey[Boolean]("Specifies whether all fields in extensions (extend type and extend interface) should be present in Resolver interface instead of the type class itself.")
val graphqlGenerateDataFetchingEnvArgInApis = settingKey[Boolean]("If true, then graphql.schema.DataFetchingEnvironment env will be added as a last argument to all methods of root type resolvers and field resolvers.")
val graphqlGenerateRequests = settingKey[Boolean]("Specifies whether client-side classes should be generated for each query, mutation and subscription. This includes: Request class (contains input data) and ResponseProjection class (contains response fields).")
val graphqlRequestSuffix = settingKey[Option[String]]("Specifies whether client-side classes should be generated for each query, mutation and subscription. This includes: Request class (contains input data) and ResponseProjection class (contains response fields).")
val graphqlResponseProjectionSuffix = settingKey[Option[String]]("Specifies whether client-side classes should be generated for each query, mutation and subscription. This includes: Request class (contains input data) and ResponseProjection class (contains response fields).")
lazy val baseGraphQLSettings: Seq[Def.Setting[_]] = Seq(
graphql := {
Codegen(
(sourceManaged in graphql).value,
graphqlSchemaPaths.value,
Some(sourceDirectory.value / "resources"),
graphqlModelNamePrefix.value,
graphqlModelNameSuffix.value,
graphqlApiPackageName.value,
graphqlModelPackageName.value,
graphqlGenerateBuilder.value,
graphqlGenerateApis.value,
graphqlModelValidationAnnotation.value,
graphqlGenerateEqualsAndHashCode.value,
graphqlGenerateToString.value,
graphqlGenerateAsyncApi.value,
graphqlGenerateParameterizedFieldsResolvers.value,
graphqlGenerateExtensionFieldsResolvers.value,
graphqlGenerateDataFetchingEnvArgInApis.value,
graphqlGenerateRequests.value,
graphqlRequestSuffix.value,
graphqlResponseProjectionSuffix.value
)
},
graphqlSchemaPaths := Seq((sourceDirectory.value / "resources/schema.graphql").getCanonicalPath),
// This follows the output directory structure recommended by sbt team
// https://github.com/sbt/sbt/issues/1664#issuecomment-213057686
sourceManaged in graphql := crossTarget.value / "src_managed_graphql",
managedSourceDirectories += (sourceManaged in graphql).value,
sourceGenerators += graphql.taskValue
)
}
import autoImport._
override def requires = sbt.plugins.JvmPlugin
// This plugin is automatically enabled for projects which are JvmPlugin.
override def trigger = allRequirements
override val globalSettings = Seq(
graphqlModelNamePrefix := None,
graphqlModelNameSuffix := None,
graphqlApiPackageName := None,
graphqlModelPackageName := None,
graphqlGenerateBuilder := MappingConfigConstants.DEFAULT_BUILDER,
graphqlGenerateApis := MappingConfigConstants.DEFAULT_GENERATE_APIS,
graphqlModelValidationAnnotation := Some(MappingConfigConstants.DEFAULT_VALIDATION_ANNOTATION),
graphqlGenerateEqualsAndHashCode := MappingConfigConstants.DEFAULT_EQUALS_AND_HASHCODE,
graphqlGenerateToString := MappingConfigConstants.DEFAULT_TO_STRING,
graphqlGenerateAsyncApi := MappingConfigConstants.DEFAULT_GENERATE_ASYNC_APIS,
graphqlGenerateParameterizedFieldsResolvers := MappingConfigConstants.DEFAULT_GENERATE_PARAMETERIZED_FIELDS_RESOLVERS,
graphqlGenerateExtensionFieldsResolvers := MappingConfigConstants.DEFAULT_GENERATE_EXTENSION_FIELDS_RESOLVERS,
graphqlGenerateDataFetchingEnvArgInApis := MappingConfigConstants.DEFAULT_GENERATE_DATA_FETCHING_ENV,
graphqlGenerateRequests := MappingConfigConstants.DEFAULT_GENERATE_REQUESTS,
graphqlRequestSuffix := Some(MappingConfigConstants.DEFAULT_REQUEST_SUFFIX),
graphqlResponseProjectionSuffix := Some(MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_SUFFIX)
)
// a group of settings that are automatically added to projects.
override val projectSettings =
inConfig(Compile)(baseGraphQLSettings)
// For now we skip compiling schemas under src/test because the plugin will
// fail if no schemas are found at the expected location
// ++ inConfig(Test)(baseGraphQLSettings)
}
object Codegen {
def apply(
outputDir: File,
graphqlSchemaPaths: Seq[String],
schemasRootDir: Option[File],
graphqlModelNamePrefix: Option[String],
graphqlModelNameSuffix: Option[String],
graphqlApiPackageName: Option[String],
graphqlModelPackageName: Option[String],
graphqlGenerateBuilder: Boolean,
graphqlGenerateApis: Boolean,
graphqlModelValidationAnnotation: Option[String],
graphqlGenerateEqualsAndHashCode: Boolean,
graphqlGenerateToString: Boolean,
graphqlGenerateAsyncApi: Boolean,
graphqlGenerateParameterizedFieldsResolvers: Boolean,
graphqlGenerateExtensionFieldsResolvers: Boolean,
graphqlGenerateDataFetchingEnvArgInApis: Boolean,
graphqlGenerateRequests: Boolean,
graphqlRequestSuffix: Option[String],
graphqlResponseProjectionSuffix: Option[String]): Seq[File] = {
val mappingConfig = new MappingConfig();
mappingConfig.setModelNamePrefix(graphqlModelNamePrefix.getOrElse(null));
mappingConfig.setModelNameSuffix(graphqlModelNameSuffix.getOrElse(null));
mappingConfig.setApiPackageName(graphqlApiPackageName.getOrElse(null));
mappingConfig.setModelPackageName(graphqlModelPackageName.getOrElse(null));
mappingConfig.setGenerateBuilder(graphqlGenerateBuilder);
mappingConfig.setGenerateApis(graphqlGenerateApis);
mappingConfig.setModelValidationAnnotation(graphqlModelValidationAnnotation.getOrElse(null));
mappingConfig.setGenerateEqualsAndHashCode(graphqlGenerateEqualsAndHashCode);
mappingConfig.setGenerateToString(graphqlGenerateToString);
mappingConfig.setGenerateAsyncApi(graphqlGenerateAsyncApi);
mappingConfig.setGenerateParameterizedFieldsResolvers(graphqlGenerateParameterizedFieldsResolvers);
mappingConfig.setGenerateExtensionFieldsResolvers(graphqlGenerateExtensionFieldsResolvers);
mappingConfig.setGenerateDataFetchingEnvironmentArgumentInApis(graphqlGenerateDataFetchingEnvArgInApis);
mappingConfig.setGenerateRequests(graphqlGenerateRequests);
mappingConfig.setRequestSuffix(graphqlRequestSuffix.getOrElse(null));
mappingConfig.setResponseProjectionSuffix(graphqlResponseProjectionSuffix.getOrElse(null));
val mappingConfigSupplier = null;
val generatedSources = new GraphQLCodegen(getSchemas(graphqlSchemaPaths, schemasRootDir), outputDir, mappingConfig, mappingConfigSupplier).generate();
JavaConverters.collectionAsScalaIterableConverter(generatedSources).asScala.toSeq
}
private def getSchemas(graphqlSchemaPaths: Seq[String], schemasRootDir: Option[File]): java.util.List[String] = {
if (!graphqlSchemaPaths.isEmpty) {
return JavaConverters.seqAsJavaList(graphqlSchemaPaths);
}
val finder = new SchemaFinder(schemasRootDir.get.toPath);
return finder.findSchemas();
}
}
lazy val root = (project in file("."))
.settings(
version := "0.1",
scalaVersion := "2.13.2",
libraryDependencies += "javax.validation" % "validation-api" % "2.0.1.Final",
graphqlApiPackageName := Some("io.github.kobylynskyi.graphql.test.api"),
graphqlModelPackageName := Some("io.github.kobylynskyi.graphql.test.model")
)
sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("io.github.kobylynskyi" % "sbt-graphql-java-codegen" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
#
# Schemas must have at least a query root type
#
schema {
query: Query
}
# This is the type that will be the root of our query, and the
# entry point into our schema.
type Query {
getPosts: [Post]
getUser(
id : ID!
): User
getPost(
id: ID!
): Post
}
type Post {
id: ID!
title: String!
# Content if provided by author
content: String
# The URL if this is external content
url: String
author: User
}
type User {
id: ID!
username: String!
email: String!
posts: [Post]
}
# check if the file gets created
> compile
$ exists target/scala-2.13/src_managed_graphql/io/github/kobylynskyi/graphql/test/model/User.java
version in ThisBuild := "2.0.0-SNAPSHOT"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册