diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/GraphQLVoidScalar.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/GraphQLVoidScalar.java new file mode 100644 index 0000000000..0ff1721c13 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/GraphQLVoidScalar.java @@ -0,0 +1,30 @@ +package com.baeldung.returnnull; + +import graphql.schema.Coercing; +import graphql.schema.GraphQLScalarType; + +public class GraphQLVoidScalar { + + public static final GraphQLScalarType Void = GraphQLScalarType.newScalar() + .name("Void") + .description("A custom scalar that represents the null value") + .coercing(new Coercing() { + + @Override + public Object serialize(Object dataFetcherResult) { + return null; + } + + @Override + public Object parseValue(Object input) { + return null; + } + + @Override + public Object parseLiteral(Object input) { + return null; + } + }) + .build(); + +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/GraphQlConfig.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/GraphQlConfig.java new file mode 100644 index 0000000000..3ba508eb90 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/GraphQlConfig.java @@ -0,0 +1,14 @@ +package com.baeldung.returnnull; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.graphql.execution.RuntimeWiringConfigurer; + +@Configuration +public class GraphQlConfig { + + @Bean + public RuntimeWiringConfigurer runtimeWiringConfigurer() { + return wiringBuilder -> wiringBuilder.scalar(GraphQLVoidScalar.Void); + } +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/Post.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/Post.java new file mode 100644 index 0000000000..e882bf6621 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/Post.java @@ -0,0 +1,51 @@ +package com.baeldung.returnnull; + +public class Post { + + private String id; + private String title; + private String text; + private String category; + private String author; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/PostController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/PostController.java new file mode 100644 index 0000000000..99ce33c230 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/PostController.java @@ -0,0 +1,28 @@ +package com.baeldung.returnnull; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.springframework.graphql.data.method.annotation.Argument; +import org.springframework.graphql.data.method.annotation.MutationMapping; +import org.springframework.stereotype.Controller; + +@Controller +public class PostController { + + List posts = new ArrayList<>(); + + @MutationMapping + public Void createPostReturnCustomScalar(@Argument String title, @Argument String text, @Argument String category, @Argument String author) { + Post post = new Post(); + post.setId(UUID.randomUUID() + .toString()); + post.setTitle(title); + post.setText(text); + post.setCategory(category); + post.setAuthor(author); + posts.add(post); + return null; + } +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/ReturnNullApp.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/ReturnNullApp.java new file mode 100644 index 0000000000..7e24b0d9d7 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/returnnull/ReturnNullApp.java @@ -0,0 +1,14 @@ +package com.baeldung.returnnull; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ReturnNullApp { + + public static void main(String[] args) { + System.setProperty("spring.profiles.default", "returnnull"); + SpringApplication.run(com.baeldung.chooseapi.ChooseApiApp.class, args); + } +} + diff --git a/spring-boot-modules/spring-boot-graphql/src/main/resources/application-returnnull.yml b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-returnnull.yml new file mode 100644 index 0000000000..eabd906273 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-returnnull.yml @@ -0,0 +1,9 @@ +server: + port: 8082 + +spring: + graphql: + graphiql: + enabled: true + schema: + locations: classpath:returnnull/ diff --git a/spring-boot-modules/spring-boot-graphql/src/main/resources/returnnull/returnnull.graphqls b/spring-boot-modules/spring-boot-graphql/src/main/resources/returnnull/returnnull.graphqls new file mode 100644 index 0000000000..2cdff0878e --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/returnnull/returnnull.graphqls @@ -0,0 +1,18 @@ +scalar Void + +type Mutation { + createPostReturnNullableType(title: String!, text: String!, category: String!, author: String!) : Int + createPostReturnCustomScalar(title: String!, text: String!, category: String!, author: String!) : Void +} + +type Post { + id: ID + title: String + text: String + category: String + author: String +} + +type Query { + recentPosts(count: Int, offset: Int): [Post]! +} diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/returnnull/PostControllerIntegrationTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/returnnull/PostControllerIntegrationTest.java new file mode 100644 index 0000000000..4d9dad0b40 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/returnnull/PostControllerIntegrationTest.java @@ -0,0 +1,48 @@ +package com.baeldung.returnnull; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest; +import org.springframework.context.annotation.Import; +import org.springframework.graphql.test.tester.GraphQlTester; +import org.springframework.test.context.ActiveProfiles; + +@GraphQlTest(PostController.class) +@ActiveProfiles("returnnull") +@Import(GraphQlConfig.class) +class PostControllerIntegrationTest { + + @Autowired + private GraphQlTester graphQlTester; + + @Test + void givenNewPostData_whenExecuteMutation_thenReturnCustomNullScalar() { + String documentName = "create_post_return_custom_scalar"; + + graphQlTester.documentName(documentName) + .variable("title", "New Post") + .variable("text", "New post text") + .variable("category", "category") + .variable("author", "Alex") + .execute() + .path("createPostReturnCustomScalar") + .valueIsNull(); + + } + + @Test + void givenNewPostData_whenExecuteMutation_thenReturnNullType() { + String documentName = "create_post_return_nullable_type"; + + graphQlTester.documentName(documentName) + .variable("title", "New Post") + .variable("text", "New post text") + .variable("category", "category") + .variable("author", "Alex") + .execute() + .path("createPostReturnNullableType") + .valueIsNull(); + + } + +} diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post_return_custom_scalar.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post_return_custom_scalar.graphql new file mode 100644 index 0000000000..9400903ce1 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post_return_custom_scalar.graphql @@ -0,0 +1,3 @@ +mutation createPostReturnCustomScalar($title: String!, $text: String!, $category: String!, $author: String!) { + createPostReturnCustomScalar(title: $title, text: $text, category: $category, author: $author) +} diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post_return_nullable_type.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post_return_nullable_type.graphql new file mode 100644 index 0000000000..f43c9bddf9 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post_return_nullable_type.graphql @@ -0,0 +1,3 @@ +mutation createPostReturnNullableType($title: String!, $text: String!, $category: String!, $author: String!) { + createPostReturnNullableType(title: $title, text: $text, category: $category, author: $author) +}