{"id":134682,"date":"2025-06-27T17:28:00","date_gmt":"2025-06-27T14:28:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=134682"},"modified":"2025-06-27T11:56:47","modified_gmt":"2025-06-27T08:56:47","slug":"return-anydata-from-graphql-mutation","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html","title":{"rendered":"Return Anydata From GraphQL Mutation"},"content":{"rendered":"<p>In GraphQL, a mutation is used to modify server-side data, such as creating, updating or deleting resources. In many cases, you may want to return arbitrary or flexible data structures in response, which could include custom objects, nested fields, status messages or error details. Designing mutations to return such diverse outputs enhances API usability, especially when communicating both success and failure scenarios to clients in a structured way.<\/p>\n<p>This article explores how to implement flexible return types in GraphQL mutations by defining and using union types for dynamic response handling.<\/p>\n<h2 class=\"wp-block-heading\">1. Using Union Types for Mutation Responses<\/h2>\n<p>In GraphQL, mutations often need to return different types of responses depending on whether the operation was successful or not. Rather than using a generic response object with optional fields, GraphQL provides union types as a clean and type-safe way to handle this variability.<\/p>\n<p>A union type allows a field to return one of several distinct object types. This is useful when the possible response types do not share the same fields. In the example from this article, the <code>removeCustomer<\/code> mutation can return either a <code>RemoveCustomerSuccess<\/code> object if the operation is successful, or an <code>ErrorResponse<\/code> object if the customer ID is not found..<\/p>\n<pre class=\"brush:plain\">\nunion RemoveCustomerResult = RemoveCustomerSuccess | ErrorResponse\n<\/pre>\n<p>This union type allows the mutation to return one of the two types, preserving flexibility and type safety.<\/p>\n<h2 class=\"wp-block-heading\">2. GraphQL Schema Definition \u2013 <code>schema.graphqls<\/code><\/h2>\n<p>We start by defining the GraphQL schema. This schema introduces a <code>Mutation<\/code> for removing a customer and uses a union type called <code>RemoveCustomerResult<\/code> to represent multiple return possibilities.<\/p>\n<p>Create the file <code>src\/main\/resources\/graphql\/schema.graphqls<\/code>:<\/p>\n<pre class=\"brush:plain\">\ntype Query {\n  allCustomers: [Customer!]!\n}\n\ntype Mutation {\n  removeCustomer(id: ID!): RemoveCustomerResult!\n}\n\ntype Customer {\n  id: ID!\n  name: String!\n}\n\ntype RemoveCustomerSuccess {\n  message: String!\n  deletedCustomerId: ID!\n}\n\ntype ErrorResponse {\n  errorCode: String!\n  message: String!\n}\n\nunion RemoveCustomerResult = RemoveCustomerSuccess | ErrorResponse\n\t\n<\/pre>\n<p>The <code>removeCustomer<\/code> mutation accepts a customer ID and returns a <code>RemoveCustomerResult<\/code>, which is a union of <code>RemoveCustomerSuccess<\/code> and <code>ErrorResponse<\/code>. This means the mutation can return either a success or an error depending on the outcome of the operation.<\/p>\n<h2 class=\"wp-block-heading\">3. DTO and Model Classes<\/h2>\n<p>To support the union types in the schema, we need to define the corresponding Java classes that represent each possible return type from the mutation. Each class should match the structure defined in the GraphQL schema.<\/p>\n<p>The class below represents the successful outcome of a customer removal operation.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">\npublic class RemoveCustomerSuccess {\n\n    private String message;\n    private int deletedCustomerId;\n\n    public RemoveCustomerSuccess() {\n    }\n\n    public RemoveCustomerSuccess(String message, int deletedCustomerId) {\n        this.message = message;\n        this.deletedCustomerId = deletedCustomerId;\n    }\n\n    public String getMessage() {\n        return message;\n    }\n\n    public void setMessage(String message) {\n        this.message = message;\n    }\n\n    public int getDeletedCustomerId() {\n        return deletedCustomerId;\n    }\n\n    public void setDeletedCustomerId(int deletedCustomerId) {\n        this.deletedCustomerId = deletedCustomerId;\n    }\n\n}\n<\/pre>\n<p>This POJO is mapped to the <code>RemoveCustomerSuccess<\/code> GraphQL type. It carries a message and the ID of the deleted customer.<\/p>\n<p>In case the mutation fails, an error object is returned using this class.<\/p>\n<pre class=\"brush:java\">\npublic class ErrorResponse {\n    private String errorCode;\n    private String message;\n\n    \/\/ Constructors, getters, setters\n}\n<\/pre>\n<p>Next, we define a simple POJO to represent the <code>Customer<\/code> entity.<\/p>\n<pre class=\"brush:java\">\npublic class Customer {\n\n    private int id;\n    private String name;\n    \n    public Customer() {\n    }\n\n    public Customer(int id, String name) {\n        this.id = id;\n        this.name = name;\n    }\n\n    public int getId() {\n        return id;\n    }\n\n    public void setId(int id) {\n        this.id = id;\n    }\n    \n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n    \n}\n<\/pre>\n<h2 class=\"wp-block-heading\">4. GraphQL Configuration <\/h2>\n<p>To make GraphQL aware of how to resolve the union type <code>RemoveCustomerResult<\/code>, we configure a custom type resolver.<\/p>\n<pre class=\"brush:java\">\n@Configuration\npublic class GraphQLConfig {\n\n    @Bean\n    public RuntimeWiringConfigurer unionConfigurer() {\n        return builder -&gt; builder\n            .type(\"RemoveCustomerResult\", typeWiring -&gt; typeWiring\n                .typeResolver(env -&gt; {\n                    Object obj = env.getObject();\n                    if (obj instanceof RemoveCustomerSuccess) {\n                        return env.getSchema().getObjectType(\"RemoveCustomerSuccess\");\n                    } else if (obj instanceof ErrorResponse) {\n                        return env.getSchema().getObjectType(\"ErrorResponse\");\n                    }\n                    return null;\n                })\n            );\n    }\n}\n<\/pre>\n<p>This configuration registers a resolver for the <code>RemoveCustomerResult<\/code> union type. At runtime, GraphQL uses this logic to determine whether to treat the result as a <code>RemoveCustomerSuccess<\/code> or <code>ErrorResponse<\/code> based on the Java object\u2019s class.<\/p>\n<p><strong>The CustomerController Class<\/strong><\/p>\n<p>This controller contains the query and mutation logic for customer operations.<\/p>\n<pre class=\"brush:java\">\n@Controller\nclass CustomerController {\n\n    private static final Map&lt;Integer, Customer&gt; customers = new HashMap&lt;&gt;(Map.of(\n            1, new Customer(1, \"John Gibbs\"),\n            2, new Customer(2, \"Fred Joe\"),\n            3, new Customer(3, \"Thomas Rice\"),\n            4, new Customer(4, \"James Brown\")\n    ));\n\n    @QueryMapping\n    public List&lt;Customer&gt; allCustomers() {\n        return new ArrayList&lt;&gt;(customers.values());\n    }\n\n    @SchemaMapping(typeName = \"Mutation\", field = \"removeCustomer\")\n    public Object removeCustomer(@Argument Integer id) {\n        Customer removed = customers.remove(id);\n        if (removed != null) {\n            return new RemoveCustomerSuccess(\"Customer deleted successfully\", id);\n        } else {\n            return new ErrorResponse(\"NOT_FOUND\", \"Customer with ID \" + id + \" does not exist\");\n        }\n    }\n}\n<\/pre>\n<p>This controller defines the GraphQL resolver methods. The <code>removeCustomer<\/code> mutation checks whether a customer exists. If found, it removes the customer and returns a success object; otherwise, it returns an error response. The return type is <code>Object<\/code> so it can hold either class, which is resolved using the union type logic defined earlier.<\/p>\n<p><strong>application.yml Configuration<\/strong><\/p>\n<pre class=\"brush:yaml,yml\">\nspring:\n  graphql:\n    graphiql:\n      enabled: true\n\n<\/pre>\n<p>This configuration enables the GraphiQL interface, allowing developers to interact with and test GraphQL queries and mutations via a web browser at <code>\/graphiql<\/code>.<\/p>\n<p><strong>Example GraphQL Mutation Call<\/strong><\/p>\n<p>Start the application, navigate to <a class=\"\" href=\"http:\/\/localhost:8080\/graphiql\">http:\/\/localhost:8080\/graphiql<\/a>, enter the query in the editor, and click the play button at the top of the window to execute it. You can test the mutation using the following query in the GraphiQL interface:<\/p>\n<pre class=\"brush:plain\">\nmutation {\n  removeCustomer(id: 1) {\n    ... on RemoveCustomerSuccess {\n      message\n      deletedCustomerId\n    }\n    ... on ErrorResponse {\n      errorCode\n      message\n    }\n  }\n}\n<\/pre>\n<p>You will receive a response similar to this.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/graphql_example_output.png\"><img decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/graphql_example_output-1024x640.png\" alt=\"Java GraphQL Mutation Example Returning Multiple Data Types \u2013 Screenshot Output\" class=\"wp-image-135074\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/graphql_example_output-1024x640.png 1024w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/graphql_example_output-300x188.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/graphql_example_output-768x480.png 768w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/graphql_example_output.png 1440w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<p><strong>Or Error:<\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/error-code-graphql.png\"><img decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/error-code-graphql-1024x640.png\" alt=\"Java GraphQL mutation example returning multiple data types \u2013 screenshot showing error output.\" class=\"wp-image-135076\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/error-code-graphql-1024x640.png 1024w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/error-code-graphql-300x188.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/error-code-graphql-768x480.png 768w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2025\/06\/error-code-graphql.png 1440w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">5. Conclusion<\/h2>\n<p>In this article, we demonstrated how to implement a GraphQL mutation in Java that can return different response types using a union type. By defining separate classes for success and error responses and configuring a type resolver in Spring Boot, we created a flexible mutation that communicates meaningful results to the client. This pattern is useful in applications where mutations may lead to different execution paths, providing a clean way to handle such cases.<\/p>\n<h2 class=\"wp-block-heading\">6. Download the Source Code<\/h2>\n<p>This article covered how to return anydata from a GraphQL mutation in Java.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/12\/jacoco-parent-project.zip\"><strong>java return anydata graphql mutation<\/strong><\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In GraphQL, a mutation is used to modify server-side data, such as creating, updating or deleting resources. In many cases, you may want to return arbitrary or flexible data structures in response, which could include custom objects, nested fields, status messages or error details. Designing mutations to return such diverse outputs enhances API usability, especially &hellip;<\/p>\n","protected":false},"author":128888,"featured_media":117826,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[1458,4130,4129,4133,4132,4131],"class_list":["post-134682","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-graphql","tag-graphql-java","tag-graphql-mutation","tag-graphql-schema","tag-spring-graphql","tag-union-types"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Return Anydata From GraphQL Mutation - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Learn how to implement Java return anydata GraphQL mutation with flexible, type-safe responses using union types and Spring Boot.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Return Anydata From GraphQL Mutation - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Learn how to implement Java return anydata GraphQL mutation with flexible, type-safe responses using union types and Spring Boot.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/web.facebook.com\/omos.aziegbe\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-27T14:28:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/06\/graphql-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Omozegie Aziegbe\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/OAziegbe\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Omozegie Aziegbe\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html\"},\"author\":{\"name\":\"Omozegie Aziegbe\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/7d3eac6e45542536e961129ae0fb453e\"},\"headline\":\"Return Anydata From GraphQL Mutation\",\"datePublished\":\"2025-06-27T14:28:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html\"},\"wordCount\":684,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/graphql-logo.jpg\",\"keywords\":[\"GraphQL\",\"graphql-java\",\"graphql-mutation\",\"graphql-schema\",\"spring-graphql\",\"union-types\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html\",\"name\":\"Return Anydata From GraphQL Mutation - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/graphql-logo.jpg\",\"datePublished\":\"2025-06-27T14:28:00+00:00\",\"description\":\"Learn how to implement Java return anydata GraphQL mutation with flexible, type-safe responses using union types and Spring Boot.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/graphql-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/graphql-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/return-anydata-from-graphql-mutation.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Return Anydata From GraphQL Mutation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/7d3eac6e45542536e961129ae0fb453e\",\"name\":\"Omozegie Aziegbe\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-jcg_profile_pic-96x96.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-jcg_profile_pic-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/cropped-jcg_profile_pic-96x96.jpg\",\"caption\":\"Omozegie Aziegbe\"},\"description\":\"Omos Aziegbe is a technical writer and web\\\/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.\",\"sameAs\":[\"https:\\\/\\\/web.facebook.com\\\/omos.aziegbe\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/omosaziegbe\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/OAziegbe\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/omozegie-aziegbe\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Return Anydata From GraphQL Mutation - Java Code Geeks","description":"Learn how to implement Java return anydata GraphQL mutation with flexible, type-safe responses using union types and Spring Boot.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html","og_locale":"en_US","og_type":"article","og_title":"Return Anydata From GraphQL Mutation - Java Code Geeks","og_description":"Learn how to implement Java return anydata GraphQL mutation with flexible, type-safe responses using union types and Spring Boot.","og_url":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/web.facebook.com\/omos.aziegbe","article_published_time":"2025-06-27T14:28:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/06\/graphql-logo.jpg","type":"image\/jpeg"}],"author":"Omozegie Aziegbe","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/OAziegbe","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Omozegie Aziegbe","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html"},"author":{"name":"Omozegie Aziegbe","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/7d3eac6e45542536e961129ae0fb453e"},"headline":"Return Anydata From GraphQL Mutation","datePublished":"2025-06-27T14:28:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html"},"wordCount":684,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/06\/graphql-logo.jpg","keywords":["GraphQL","graphql-java","graphql-mutation","graphql-schema","spring-graphql","union-types"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html","url":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html","name":"Return Anydata From GraphQL Mutation - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/06\/graphql-logo.jpg","datePublished":"2025-06-27T14:28:00+00:00","description":"Learn how to implement Java return anydata GraphQL mutation with flexible, type-safe responses using union types and Spring Boot.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/06\/graphql-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/06\/graphql-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/return-anydata-from-graphql-mutation.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Return Anydata From GraphQL Mutation"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/7d3eac6e45542536e961129ae0fb453e","name":"Omozegie Aziegbe","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/12\/cropped-jcg_profile_pic-96x96.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/12\/cropped-jcg_profile_pic-96x96.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2023\/12\/cropped-jcg_profile_pic-96x96.jpg","caption":"Omozegie Aziegbe"},"description":"Omos Aziegbe is a technical writer and web\/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.","sameAs":["https:\/\/web.facebook.com\/omos.aziegbe","https:\/\/www.linkedin.com\/in\/omosaziegbe\/","https:\/\/x.com\/https:\/\/twitter.com\/OAziegbe"],"url":"https:\/\/www.javacodegeeks.com\/author\/omozegie-aziegbe"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/134682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/128888"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=134682"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/134682\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/117826"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=134682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=134682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=134682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}