未验证 提交 57086a81 编写于 作者: B Bogdan Kobylynskyi 提交者: GitHub

Revert "Add ability to generate particular types as Java interfaces #606 (#610)" (#613)

This reverts commit 994f6fee.
上级 994f6fee
......@@ -54,7 +54,6 @@ See [DirectiveAnnotationsMapping](#option-directiveannotationsmapping)* |
| `responseProjectionMaxDepth` | Integer | 3 | Sets max depth when use `all$()` which for facilitating the construction of projection automatically, the fields on all projections are provided when it be invoked. This is a global configuration, of course, you can use `all$(max)` to set for each method. For self recursive types, too big depth may result in a large number of returned data!|
| `generatedLanguage` | Enum | GeneratedLanguage.JAVA | Choose which language you want to generate, Java,Scala,Kotlin were supported. Note that due to language features, there are slight differences in default values between languages.|
| `generateModelOpenClasses` | Boolean | false | The class type of the generated model. If true, generate normal classes, else generate data classes. It only support in kotlin(```data class```) and scala(```case class```). Maybe we will consider to support Java ```record``` in the future.|
| `typesAsInterfaces` | Set(String) | Empty | Types that must generated as interfaces should be defined here in format: `TypeName` or `@directive`. E.g.: `User`, `@asInterface`. |
### Option `graphqlSchemas`
......
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
......@@ -82,7 +82,6 @@ esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
......@@ -130,7 +129,6 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
......
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
......@@ -179,9 +179,6 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
@Parameter
private String[] useObjectMapperForRequestSerialization;
@Parameter
private String[] typesAsInterfaces;
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH_STRING)
private int responseProjectionMaxDepth;
......@@ -250,7 +247,6 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
mappingConfig.setParametrizedInputSuffix(parametrizedInputSuffix);
mappingConfig.setResponseProjectionMaxDepth(responseProjectionMaxDepth);
mappingConfig.setUseObjectMapperForRequestSerialization(mapToHashSet(useObjectMapperForRequestSerialization));
mappingConfig.setTypesAsInterfaces(mapToHashSet(typesAsInterfaces));
mappingConfig.setResolverParentInterface(getResolverParentInterface());
mappingConfig.setQueryResolverParentInterface(getQueryResolverParentInterface());
......@@ -550,11 +546,6 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
return mapToHashSet(useObjectMapperForRequestSerialization);
}
@Override
public Set<String> getTypesAsInterfaces() {
return mapToHashSet(typesAsInterfaces);
}
@Override
public String getQueryResolverParentInterface() {
return parentInterfaces.getQueryResolver();
......
......@@ -454,22 +454,8 @@ public abstract class GraphQLCodegen {
List<File> generatedFiles = new ArrayList<>();
Map<String, Object> dataModel = dataModelMapperFactory.getTypeDefinitionMapper()
.map(mappingContext, definition);
boolean typeAsInterface = mappingConfig.getTypesAsInterfaces().contains(definition.getName());
if (!typeAsInterface) {
typeAsInterface = definition.getDirectiveNames().stream().anyMatch(directiveName ->
mappingConfig.getTypesAsInterfaces().contains("@" + directiveName)
);
}
if (typeAsInterface) {
generatedFiles.add(GraphQLCodegenFileCreator.generateFile(mappingContext,
FreeMarkerTemplateType.INTERFACE, dataModel, outputDir));
} else {
generatedFiles.add(GraphQLCodegenFileCreator.generateFile(mappingContext,
FreeMarkerTemplateType.TYPE, dataModel, outputDir));
}
generatedFiles.add(GraphQLCodegenFileCreator.generateFile(mappingContext,
FreeMarkerTemplateType.TYPE, dataModel, outputDir));
if (Boolean.TRUE.equals(mappingConfig.getGenerateClient())) {
Map<String, Object> responseProjDataModel = dataModelMapperFactory.getRequestResponseDefinitionMapper()
......
......@@ -401,21 +401,6 @@ public interface GraphQLCodegenConfiguration {
*/
Set<String> getUseObjectMapperForRequestSerialization();
/**
* Types that must generated as interfaces.
*
* <p>Values should be defined here in format: TypeName, @directive
*
* <p>E.g.:
* <ul>
* <li>{@code Person}</li>
* <li>{@code @asInterface}</li>
* </ul>
*
* @return Set of types that should generated as interfaces.
*/
Set<String> getTypesAsInterfaces();
/**
* Generate code with lang
*
......
......@@ -73,8 +73,6 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
private Integer responseProjectionMaxDepth;
private Set<String> useObjectMapperForRequestSerialization = new HashSet<>();
private Set<String> typesAsInterfaces = new HashSet<>();
private boolean generateModelOpenClasses;
private GeneratedLanguage generatedLanguage;
......@@ -175,7 +173,6 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
GraphQLCodegenConfiguration::getResponseProjectionMaxDepth);
useObjectMapperForRequestSerialization = combineSet(useObjectMapperForRequestSerialization,
source.useObjectMapperForRequestSerialization);
typesAsInterfaces = combineSet(typesAsInterfaces, source.typesAsInterfaces);
generatedLanguage = getValueOrDefaultToThis(source, GraphQLCodegenConfiguration::getGeneratedLanguage);
generateModelOpenClasses = getValueOrDefaultToThis(source,
GraphQLCodegenConfiguration::isGenerateModelOpenClasses);
......@@ -605,15 +602,6 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
this.useObjectMapperForRequestSerialization = useObjectMapperForRequestSerialization;
}
@Override
public Set<String> getTypesAsInterfaces() {
return typesAsInterfaces;
}
public void setTypesAsInterfaces(Set<String> typesAsInterfaces) {
this.typesAsInterfaces = typesAsInterfaces;
}
@Override
public GeneratedLanguage getGeneratedLanguage() {
return generatedLanguage;
......
......@@ -277,11 +277,6 @@ public class MappingContext implements GraphQLCodegenConfiguration {
return config.getUseObjectMapperForRequestSerialization();
}
@Override
public Set<String> getTypesAsInterfaces() {
return config.getTypesAsInterfaces();
}
public ExtendedDocument getDocument() {
return document;
}
......
package com.kobylynskyi.graphql.codegen;
import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen;
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
class GraphQLCodegenTypesAsInterfacesTest {
private final File outputBuildDir = new File("build/generated");
private final File outputJavaClassesDir = new File("build/generated/com/github/graphql");
private final MappingConfig mappingConfig = new MappingConfig();
@BeforeEach
void init() {
mappingConfig.setPackageName("com.github.graphql");
mappingConfig.setFieldsWithResolvers(Collections.singleton("@customResolver"));
}
@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
}
@Test
void generate_typesAsInterfaces() throws Exception {
mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface", "Order")));
new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/types-as-interfaces.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/types-as-interfaces/" +
"Order.java.txt"), getFileByName(files, "Order.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/types-as-interfaces/" +
"QueryResolver.java.txt"), getFileByName(files, "QueryResolver.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/types-as-interfaces/" +
"User.java.txt"), getFileByName(files, "User.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/types-as-interfaces/" +
"UserCurrentQueryResolver.java.txt"), getFileByName(files, "UserCurrentQueryResolver.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/types-as-interfaces/" +
"UserResolver.java.txt"), getFileByName(files, "UserResolver.java"));
}
@Test
void generate_typesAsInterfacesExtendsInterface() throws Exception {
mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface")));
new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/" +
"types-as-interfaces-extends-interface.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/Node.java.txt"), getFileByName(files, "Node.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/Profile.java.txt"),
getFileByName(files, "Profile.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/QueryResolver.java.txt"),
getFileByName(files, "QueryResolver.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/User.java.txt"), getFileByName(files, "User.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/UserCurrentQueryResolver.java.txt"),
getFileByName(files, "UserCurrentQueryResolver.java"));
}
}
package com.kobylynskyi.graphql.codegen.kotlin;
import com.kobylynskyi.graphql.codegen.TestUtils;
import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
class GraphQLCodegenTypesAsInterfacesTest {
private final File outputBuildDir = new File("build/generated");
private final File outputJavaClassesDir = new File("build/generated/com/github/graphql");
private final MappingConfig mappingConfig = new MappingConfig();
@BeforeEach
void init() {
mappingConfig.setPackageName("com.github.graphql");
mappingConfig.setFieldsWithResolvers(Collections.singleton("@customResolver"));
mappingConfig.setGeneratedLanguage(GeneratedLanguage.KOTLIN);
}
@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
}
@Test
void generate_typesAsInterfaces() throws Exception {
mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface", "Order")));
new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/types-as-interfaces.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/types-as-interfaces/" +
"Order.kt.txt"), getFileByName(files, "Order.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/types-as-interfaces/" +
"QueryResolver.kt.txt"), getFileByName(files, "QueryResolver.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/types-as-interfaces/" +
"User.kt.txt"), getFileByName(files, "User.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/types-as-interfaces/" +
"UserCurrentQueryResolver.kt.txt"), getFileByName(files, "UserCurrentQueryResolver.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/types-as-interfaces/" +
"UserResolver.kt.txt"), getFileByName(files, "UserResolver.kt"));
}
@Test
void generate_typesAsInterfacesExtendsInterface() throws Exception {
mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface")));
new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/" +
"types-as-interfaces-extends-interface.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/" +
"types-as-interfaces-extends-interface/Node.kt.txt"), getFileByName(files, "Node.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/" +
"types-as-interfaces-extends-interface/Profile.kt.txt"), getFileByName(files, "Profile.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/" +
"types-as-interfaces-extends-interface/QueryResolver.kt.txt"),
getFileByName(files, "QueryResolver.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/" +
"types-as-interfaces-extends-interface/User.kt.txt"), getFileByName(files, "User.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/" +
"types-as-interfaces-extends-interface/UserCurrentQueryResolver.kt.txt"),
getFileByName(files, "UserCurrentQueryResolver.kt"));
}
}
......@@ -61,7 +61,6 @@ class MappingConfigTest {
config.setParametrizedInputSuffix("9");
config.setTypeResolverPrefix("11");
config.setTypeResolverSuffix("12");
config.setTypesAsInterfaces(new HashSet<>(singletonList("User")));
RelayConfig relayConfig = new RelayConfig();
relayConfig.setDirectiveArgumentName("key");
config.setRelayConfig(relayConfig);
......@@ -105,7 +104,6 @@ class MappingConfigTest {
config.setParametrizedInputSuffix("99");
config.setTypeResolverPrefix("1111");
config.setTypeResolverSuffix("1212");
config.setTypesAsInterfaces(new HashSet<>(singletonList("User2")));
RelayConfig relayConfig = new RelayConfig();
relayConfig.setDirectiveArgumentName("for");
config.setRelayConfig(relayConfig);
......@@ -221,7 +219,6 @@ class MappingConfigTest {
assertEquals("11", mappingConfig.getTypeResolverPrefix());
assertEquals("12", mappingConfig.getTypeResolverSuffix());
assertEquals("key", mappingConfig.getRelayConfig().getDirectiveArgumentName());
assertEquals(singleton("User"), mappingConfig.getTypesAsInterfaces());
}
@Test
......@@ -265,7 +262,6 @@ class MappingConfigTest {
assertEquals("11", mappingConfig.getTypeResolverPrefix());
assertEquals("12", mappingConfig.getTypeResolverSuffix());
assertEquals("key", mappingConfig.getRelayConfig().getDirectiveArgumentName());
assertEquals(singleton("User"), mappingConfig.getTypesAsInterfaces());
}
@Test
......@@ -314,7 +310,6 @@ class MappingConfigTest {
assertEquals("1111", mappingConfig.getTypeResolverPrefix());
assertEquals("1212", mappingConfig.getTypeResolverSuffix());
assertEquals("for", mappingConfig.getRelayConfig().getDirectiveArgumentName());
assertEquals(new HashSet<>(Arrays.asList("User", "User2")), mappingConfig.getTypesAsInterfaces());
}
}
package com.kobylynskyi.graphql.codegen.scala;
import com.kobylynskyi.graphql.codegen.TestUtils;
import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
class GraphQLCodegenTypesAsInterfacesTest {
private final File outputBuildDir = new File("build/generated");
private final File outputJavaClassesDir = new File("build/generated/com/github/graphql");
private final MappingConfig mappingConfig = new MappingConfig();
@BeforeEach
void init() {
mappingConfig.setPackageName("com.github.graphql");
mappingConfig.setFieldsWithResolvers(Collections.singleton("@customResolver"));
mappingConfig.setGeneratedLanguage(GeneratedLanguage.SCALA);
}
@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
}
@Test
void generate_typeAsInterface() throws Exception {
mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface", "Order")));
new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/types-as-interfaces.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/types-as-interfaces/" +
"Order.scala.txt"), getFileByName(files, "Order.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/types-as-interfaces/" +
"QueryResolver.scala.txt"), getFileByName(files, "QueryResolver.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/types-as-interfaces/" +
"User.scala.txt"), getFileByName(files, "User.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/types-as-interfaces/" +
"UserCurrentQueryResolver.scala.txt"), getFileByName(files, "UserCurrentQueryResolver.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/types-as-interfaces/" +
"UserResolver.scala.txt"), getFileByName(files, "UserResolver.scala"));
}
@Test
void generate_typeAsInterfaceExtendsInterface() throws Exception {
mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface")));
new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/" +
"types-as-interfaces-extends-interface.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/" +
"types-as-interfaces-extends-interface/Node.scala.txt"), getFileByName(files, "Node.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/" +
"types-as-interfaces-extends-interface/Profile.scala.txt"),
getFileByName(files, "Profile.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/" +
"types-as-interfaces-extends-interface/QueryResolver.scala.txt"),
getFileByName(files, "QueryResolver.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/" +
"types-as-interfaces-extends-interface/User.scala.txt"), getFileByName(files, "User.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/" +
"types-as-interfaces-extends-interface/UserCurrentQueryResolver.scala.txt"),
getFileByName(files, "UserCurrentQueryResolver.scala"));
}
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface Node {
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
data class Profile(
val firstName: String,
val lastName: String
) {
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface QueryResolver {
@Throws(Exception::class)
fun userCurrent(): User?
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface User : Node {
val username: String
val email: String
val profile: Profile?
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface UserCurrentQueryResolver {
@Throws(Exception::class)
fun userCurrent(): User?
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface Order {
val number: String
val price: String
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface QueryResolver {
@Throws(Exception::class)
fun userCurrent(): User?
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface User {
val username: String
val email: String
}
package com.github.graphql
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface UserCurrentQueryResolver {
@Throws(Exception::class)
fun userCurrent(): User?
}
package com.github.graphql
/**
* Resolver for User
*/
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface UserResolver {
@Throws(Exception::class)
fun orders(user: User): List<Order>
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait Node {
}
package com.github.graphql
import scala.collection.JavaConverters._
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class Profile(
@javax.validation.constraints.NotNull
firstName: String,
@javax.validation.constraints.NotNull
lastName: String
) {
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait QueryResolver {
@throws[Exception]
def userCurrent(): User
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait User extends Node {
@javax.validation.constraints.NotNull
val username: String
@javax.validation.constraints.NotNull
val email: String
val profile: Profile
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait UserCurrentQueryResolver {
@throws[Exception]
def userCurrent(): User
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait Order {
@javax.validation.constraints.NotNull
val number: String
@javax.validation.constraints.NotNull
val price: String
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait QueryResolver {
@throws[Exception]
def userCurrent(): User
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait User {
@javax.validation.constraints.NotNull
val username: String
@javax.validation.constraints.NotNull
val email: String
}
package com.github.graphql
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait UserCurrentQueryResolver {
@throws[Exception]
def userCurrent(): User
}
package com.github.graphql
/**
* Resolver for User
*/
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait UserResolver {
@javax.validation.constraints.NotNull
@throws[Exception]
def orders(user: User): scala.Seq[Order]
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface Node {
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public class Profile implements java.io.Serializable {
@javax.validation.constraints.NotNull
private String firstName;
@javax.validation.constraints.NotNull
private String lastName;
public Profile() {
}
public Profile(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public static Profile.Builder builder() {
return new Profile.Builder();
}
public static class Builder {
private String firstName;
private String lastName;
public Builder() {
}
public Builder setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Builder setLastName(String lastName) {
this.lastName = lastName;
return this;
}
public Profile build() {
return new Profile(firstName, lastName);
}
}
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface QueryResolver {
User userCurrent() throws Exception;
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface User extends Node{
@javax.validation.constraints.NotNull
String getUsername();
@javax.validation.constraints.NotNull
String getEmail();
Profile getProfile();
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface UserCurrentQueryResolver {
User userCurrent() throws Exception;
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface Order {
@javax.validation.constraints.NotNull
String getNumber();
@javax.validation.constraints.NotNull
String getPrice();
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface QueryResolver {
User userCurrent() throws Exception;
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface User {
@javax.validation.constraints.NotNull
String getUsername();
@javax.validation.constraints.NotNull
String getEmail();
}
package com.github.graphql;
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface UserCurrentQueryResolver {
User userCurrent() throws Exception;
}
package com.github.graphql;
/**
* Resolver for User
*/
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface UserResolver {
@javax.validation.constraints.NotNull
java.util.List<Order> orders(User user) throws Exception;
}
# A GraphQL schema provides a root type for each kind of operation.
schema {
# The query root.
query: Query
}
type Query {
userCurrent: User
}
type User implements Node @asInterface {
username: String!
email: String!
profile: Profile
}
type Profile {
firstName: String!
lastName: String!
}
interface Node {
}
directive @customResolver on FIELD_DEFINITION
directive @asInterface on OBJECT
# A GraphQL schema provides a root type for each kind of operation.
schema {
# The query root.
query: Query
}
type Query {
userCurrent: User
}
type User @asInterface {
username: String!
email: String!
orders: [Order!]! @customResolver
}
type Order {
number: String!
price: String!
}
directive @customResolver on FIELD_DEFINITION
directive @asInterface on OBJECT
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册