BAEL-7255 - Implementing GraphQL Mutation without Returning Data (#15820)

This commit is contained in:
Alexandru Borza 2024-02-21 12:26:19 +02:00 committed by GitHub
parent c8c29a7806
commit d8e2c61003
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 218 additions and 0 deletions

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<Post> 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;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,9 @@
server:
port: 8082
spring:
graphql:
graphiql:
enabled: true
schema:
locations: classpath:returnnull/

View File

@ -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]!
}

View File

@ -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();
}
}

View File

@ -0,0 +1,3 @@
mutation createPostReturnCustomScalar($title: String!, $text: String!, $category: String!, $author: String!) {
createPostReturnCustomScalar(title: $title, text: $text, category: $category, author: $author)
}

View File

@ -0,0 +1,3 @@
mutation createPostReturnNullableType($title: String!, $text: String!, $category: String!, $author: String!) {
createPostReturnNullableType(title: $title, text: $text, category: $category, author: $author)
}