From c02a0eadf2d129967a23baa01912ae0c210fecf3 Mon Sep 17 00:00:00 2001 From: maocq Date: Tue, 7 May 2024 10:15:43 -0500 Subject: [PATCH 1/3] fix(graphql): Graphql update to use spring boot version and support reactive and imperative projects --- .../entrypoints/EntryPointGraphql.java | 12 +++----- .../graphql-api/api-mutations.java.mustache | 28 +++++++++++++------ .../api-mutations.test.java.mustache | 16 +++++++++++ .../graphql-api/api-queries.java.mustache | 28 ++++++++++++++----- .../api-queries.test.java.mustache | 16 +++++++++++ .../graphql-api/build.gradle.mustache | 2 +- .../entry-point/graphql-api/definition.json | 2 -- 7 files changed, 77 insertions(+), 27 deletions(-) diff --git a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGraphql.java b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGraphql.java index 61de9d26..6147600f 100644 --- a/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGraphql.java +++ b/src/main/java/co/com/bancolombia/factory/entrypoints/EntryPointGraphql.java @@ -6,27 +6,23 @@ import co.com.bancolombia.exceptions.CleanException; import co.com.bancolombia.factory.ModuleBuilder; import co.com.bancolombia.factory.ModuleFactory; -import co.com.bancolombia.factory.validations.ReactiveTypeValidation; import java.io.IOException; public class EntryPointGraphql implements ModuleFactory { @Override public void buildModule(ModuleBuilder builder) throws IOException, CleanException { - builder.runValidations(ReactiveTypeValidation.class); String path = builder.getStringParam("task-param-pathgql"); if (!path.startsWith("/")) { throw new IllegalArgumentException("The path must start with /"); } builder.appendToSettings("graphql-api", "infrastructure/entry-points"); - builder.appendToProperties("graphql.servlet").put("enabled", true).put("mapping", path); - builder - .appendToProperties("graphql.playground") - .put("mapping", "/playground") - .put("endpoint", path) - .put("enabled", true); + builder.appendToProperties("spring.graphql.graphiql").put("enabled", false); + builder.addParam("reactive", builder.isReactive()); + String dependency = buildImplementationFromProject(builder.isKotlin(), ":graphql-api"); builder.appendDependencyToModule(APP_SERVICE, dependency); + builder.setupFromTemplate("entry-point/graphql-api"); } } diff --git a/src/main/resources/entry-point/graphql-api/api-mutations.java.mustache b/src/main/resources/entry-point/graphql-api/api-mutations.java.mustache index 05a44a7c..ab8c59cd 100644 --- a/src/main/resources/entry-point/graphql-api/api-mutations.java.mustache +++ b/src/main/resources/entry-point/graphql-api/api-mutations.java.mustache @@ -1,22 +1,22 @@ package {{package}}.graphqlapi; -import graphql.kickstart.tools.GraphQLMutationResolver; {{#lombok}} import lombok.RequiredArgsConstructor; {{/lombok}} +import org.springframework.graphql.data.method.annotation.Argument; +import org.springframework.graphql.data.method.annotation.MutationMapping; import org.springframework.stereotype.Controller; +{{#reactive}} +import reactor.core.publisher.Mono; +{{/reactive}} {{#lombok}} @RequiredArgsConstructor {{/lombok}} @Controller -/** -* To interact with the API make use of Playground in the "/playground" path, but remember, -* Playground ONLY must be used in dev or qa environments, not in production. -*/ -public class ApiMutations implements GraphQLMutationResolver { +public class ApiMutations { -// private final MyUseCase useCase; + //private final MyUseCase useCase; {{^lombok}} //public ApiMutations(MyUseCase useCase){ @@ -24,8 +24,18 @@ public class ApiMutations implements GraphQLMutationResolver { //} {{/lombok}} - public String addSomething(String objRequest/* change for object request */) { -// return useCase.doAction(objRequest); +{{#reactive}} + @MutationMapping + public Mono addSomething(@Argument("objectRequest") String objRequest/* change for object request */) { + //return useCase.doAction(objRequest); + return Mono.just("Hello world from graphql-api mutations " + objRequest); + } +{{/reactive}} +{{^reactive}} + @MutationMapping + public String addSomething(@Argument("objectRequest") String objRequest/* change for object request */) { + //return useCase.doAction(objRequest); return "Hello world from graphql-api mutations " + objRequest; } +{{/reactive}} } \ No newline at end of file diff --git a/src/main/resources/entry-point/graphql-api/api-mutations.test.java.mustache b/src/main/resources/entry-point/graphql-api/api-mutations.test.java.mustache index 9670ae79..ebcbe427 100644 --- a/src/main/resources/entry-point/graphql-api/api-mutations.test.java.mustache +++ b/src/main/resources/entry-point/graphql-api/api-mutations.test.java.mustache @@ -2,9 +2,24 @@ package {{package}}.graphqlapi; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +{{#reactive}} +import reactor.test.StepVerifier; +{{/reactive}} public class ApiMutationsTest { +{{#reactive}} + @Test + void addSomethingTest(){ + ApiMutations apiMutations = new ApiMutations(); + var objRequest = "objRequest"; + apiMutations.addSomething(objRequest) + .as(StepVerifier::create) + .assertNext(Assertions::assertNotNull) + .verifyComplete(); + } +{{/reactive}} +{{^reactive}} @Test void addSomethingTest(){ ApiMutations apiMutations = new ApiMutations(); @@ -13,4 +28,5 @@ public class ApiMutationsTest { Assertions.assertNotNull(addSomething); } +{{/reactive}} } \ No newline at end of file diff --git a/src/main/resources/entry-point/graphql-api/api-queries.java.mustache b/src/main/resources/entry-point/graphql-api/api-queries.java.mustache index e42e920e..b2739e48 100644 --- a/src/main/resources/entry-point/graphql-api/api-queries.java.mustache +++ b/src/main/resources/entry-point/graphql-api/api-queries.java.mustache @@ -1,20 +1,24 @@ package {{package}}.graphqlapi; -import graphql.kickstart.tools.GraphQLQueryResolver; {{#lombok}} import lombok.RequiredArgsConstructor; {{/lombok}} +import org.springframework.graphql.data.method.annotation.Argument; +import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.stereotype.Controller; +{{#reactive}} +import reactor.core.publisher.Mono; +{{/reactive}} {{#lombok}} - @RequiredArgsConstructor +@RequiredArgsConstructor {{/lombok}} @Controller /** -* To interact with the API make use of Playground in the "/playground" path, but remember, +* To interact with the API make use of Playground in the "/graphiql" path, but remember, * Playground ONLY must be used in dev or qa environments, not in production. */ -public class ApiQueries implements GraphQLQueryResolver { +public class ApiQueries { // private final MyUseCase useCase; @@ -24,8 +28,18 @@ public class ApiQueries implements GraphQLQueryResolver { //} {{/lombok}} - public String getSomething(String objRequest/* change for object request */) { -// return useCase.doAction(objRequest); - return "Hello world from graphql-api queries " + objRequest; +{{#reactive}} + @QueryMapping + public Mono getSomething(@Argument("id") String id/* change for object request */) { + //return useCase.doAction(objRequest); + return Mono.just("Hello world from graphql-api queries " + id); } +{{/reactive}} +{{^reactive}} + @QueryMapping + public String getSomething(@Argument("id") String id/* change for object request */) { + //return useCase.doAction(objRequest); + return "Hello world from graphql-api queries " + id; + } +{{/reactive}} } \ No newline at end of file diff --git a/src/main/resources/entry-point/graphql-api/api-queries.test.java.mustache b/src/main/resources/entry-point/graphql-api/api-queries.test.java.mustache index 207ee51c..9ba7b6d4 100644 --- a/src/main/resources/entry-point/graphql-api/api-queries.test.java.mustache +++ b/src/main/resources/entry-point/graphql-api/api-queries.test.java.mustache @@ -2,9 +2,24 @@ package {{package}}.graphqlapi; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +{{#reactive}} +import reactor.test.StepVerifier; +{{/reactive}} public class ApiQueriesTest { +{{#reactive}} + @Test + void addSomethingTest(){ + ApiQueries apiQueries = new ApiQueries(); + var objRequest = "objRequest"; + apiQueries.getSomething(objRequest) + .as(StepVerifier::create) + .assertNext(Assertions::assertNotNull) + .verifyComplete(); + } +{{/reactive}} +{{^reactive}} @Test void addSomethingTest(){ ApiQueries apiQueries = new ApiQueries(); @@ -13,4 +28,5 @@ public class ApiQueriesTest { Assertions.assertNotNull(addSomething); } +{{/reactive}} } \ No newline at end of file diff --git a/src/main/resources/entry-point/graphql-api/build.gradle.mustache b/src/main/resources/entry-point/graphql-api/build.gradle.mustache index e3f95b5d..b50a7a6a 100644 --- a/src/main/resources/entry-point/graphql-api/build.gradle.mustache +++ b/src/main/resources/entry-point/graphql-api/build.gradle.mustache @@ -2,5 +2,5 @@ dependencies { implementation project(':model') implementation project(':usecase') implementation 'org.springframework.boot:spring-boot-starter-webflux' - implementation 'com.graphql-java-kickstart:graphql-spring-boot-starter:{{GRAPHQL_KICKSTART_VERSION}}' + implementation 'org.springframework.boot:spring-boot-starter-graphql' } diff --git a/src/main/resources/entry-point/graphql-api/definition.json b/src/main/resources/entry-point/graphql-api/definition.json index cd1d8bca..2a3f3fd2 100644 --- a/src/main/resources/entry-point/graphql-api/definition.json +++ b/src/main/resources/entry-point/graphql-api/definition.json @@ -1,7 +1,5 @@ { "folders": [ - "infrastructure/entry-points/graphql-api/src/main/{{language}}/{{packagePath}}/graphql-controllers", - "infrastructure/entry-points/graphql-api/src/test/{{language}}/{{packagePath}}/graphql-controllers", "applications/app-service/src/main/resources/graphql" ], "files": {}, From cd5d3355dad29ed9c423051f6fff73652078726c Mon Sep 17 00:00:00 2001 From: maocq Date: Tue, 7 May 2024 10:56:11 -0500 Subject: [PATCH 2/3] fix(graphql): Graphql imperative integration tests --- sh_generate_project.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sh_generate_project.sh b/sh_generate_project.sh index e568a11b..04b538b1 100755 --- a/sh_generate_project.sh +++ b/sh_generate_project.sh @@ -40,7 +40,7 @@ else ./gradlew gda --type $adapter done - for entry in "mq" "restmvc" "sqs" + for entry in "mq" "restmvc" "sqs" "graphql" do ./gradlew gep --type $entry done From 80ed489e052ae073647969ea2656edb06e9dcc4c Mon Sep 17 00:00:00 2001 From: Juan C Galvis <8420868+juancgalvis@users.noreply.github.com> Date: Tue, 7 May 2024 11:33:19 -0500 Subject: [PATCH 3/3] ci(dir): Fix project generation, cannot delete .git --- .github/workflows/gradle.yml | 8 ++++---- sh_generate_project.sh | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 04c0cf83..f7db7ae8 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -74,12 +74,12 @@ jobs: run: ./sh_generate_project.sh reactive - name: Scan generated reactive project dependencies if: steps.changes.outputs.templates == 'true' - working-directory: ./build/toscan + working-directory: ./build/toscan/reactive run: ./gradlew build it # run: ./gradlew build dependencyCheckAnalyze && ./gradlew it && cat applications/app-service/build/reports/dependency-check-sonar.json - name: Sonar analysis for generated reactive project if: github.event.pull_request.head.repo.fork == false && steps.changes.outputs.templates == 'true' - working-directory: ./build/toscan + working-directory: ./build/toscan/reactive run: ./gradlew sonar --stacktrace -Dsonar.token=${{ secrets.SONAR_TOKEN_GENERATED }} env: @@ -90,12 +90,12 @@ jobs: run: ./sh_generate_project.sh imperative - name: Scan generated imperative project dependencies if: steps.changes.outputs.templates == 'true' - working-directory: ./build/toscan + working-directory: ./build/toscan/imperative run: ./gradlew build it # run: ./gradlew build dependencyCheckAnalyze && ./gradlew it && cat applications/app-service/build/reports/dependency-check-sonar.json - name: Sonar analysis for generated imperative project if: github.event.pull_request.head.repo.fork == false && steps.changes.outputs.templates == 'true' - working-directory: ./build/toscan + working-directory: ./build/toscan/imperative run: ./gradlew sonar --stacktrace -Dsonar.token=${{ secrets.SONAR_TOKEN_GENERATED_I }} env: diff --git a/sh_generate_project.sh b/sh_generate_project.sh index 04b538b1..876057b8 100755 --- a/sh_generate_project.sh +++ b/sh_generate_project.sh @@ -1,10 +1,11 @@ #!/bin/bash set -e TYPE=$1 -echo "Generating project with type $TYPE" +MY_DIR="build/toscan/$TYPE" +echo "Generating project with type $TYPE in $MY_DIR" -rm -rf build/toscan -mkdir build/toscan +rm -rf $MY_DIR +mkdir -p $MY_DIR echo "buildscript { repositories { mavenLocal() @@ -17,9 +18,9 @@ echo "buildscript { } } - apply plugin: 'co.com.bancolombia.cleanArchitecture'" >> build/toscan/build.gradle + apply plugin: 'co.com.bancolombia.cleanArchitecture'" >> $MY_DIR/build.gradle -cd build/toscan || exit +cd $MY_DIR || exit gradle ca --metrics false --example true --type $TYPE gradle wrapper