Skip to content

[Chapter 12 - MicroProfile GraphQL 2.0] Add new chapter about MicroProfile GraphQL 2.0#68

Open
ttelang wants to merge 1 commit into
microprofile:mainfrom
ttelang:mp-graphql-2.0
Open

[Chapter 12 - MicroProfile GraphQL 2.0] Add new chapter about MicroProfile GraphQL 2.0#68
ttelang wants to merge 1 commit into
microprofile:mainfrom
ttelang:mp-graphql-2.0

Conversation

@ttelang
Copy link
Copy Markdown
Contributor

@ttelang ttelang commented Mar 31, 2026

This chapter introduces MicroProfile GraphQL 2.0, covering its features, advantages over REST, and how to implement GraphQL APIs in Jakarta EE and MicroProfile applications.

It covers the following topics:

  • What is GraphQL and key differences from REST
  • Schema-first and Code-first approaches
  • Project Dependencies (Maven and Gradle)
  • Defining GraphQL service endpoints
  • Annotations for defining GraphQL operations and metadata
  • Scalar types, enumerations, and GraphQL interfaces
  • Custom Object Mapping and Field Resolution
  • Error Handling and Partial Results
  • Integration with MicroProfile Config

This chapter introduces MicroProfile GraphQL 2.0, covering its features, advantages over REST, and how to implement GraphQL APIs in Jakarta EE and MicroProfile applications. It includes topics such as defining endpoints, using annotations, error handling, and project dependencies.
@ttelang ttelang marked this pull request as ready for review March 31, 2026 16:43
@ttelang ttelang changed the title Add chapter on MicroProfile GraphQL 2.0 [Chapter 11 - MicroProfile GraphQL 2.0] Add new chapter about MicroProfile GraphQL 2.0 Apr 7, 2026
@ttelang ttelang changed the title [Chapter 11 - MicroProfile GraphQL 2.0] Add new chapter about MicroProfile GraphQL 2.0 [Chapter 12 - MicroProfile GraphQL 2.0] Add new chapter about MicroProfile GraphQL 2.0 Apr 7, 2026
Copy link
Copy Markdown

@t1 t1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! I wouldn't say that and add all this nitpicking comments, if I thought differently ;-)


== Introduction

APIs serve diverse clients including mobile applications,single-page web apps, and third-party services, each with different data requirements. REST APIs either return more data than the client needs (over-fetching) or too little, forcing multiple round trips to assemble a complete response (under-fetching). GraphQL eliminates both problems by shifting query control to the client, allowing callers to declare exactly the fields and relationships they need in a single request.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technology selection is not the main driver behind that diversity: different clients have unforeseen use-cases that need different parts of an api.

And I assume that it's hard for people to really understand over-/under-fetching from this description alone. I would expand on it in the Data Fetching section and link forward from here.

I would also add: But also the service can see, what clients actually use, down to the field, allowing to safely remove unused features.

And something about the @Source resolver mechanism. I think this is absolutely essential to appreciate the power of GraphQL in Java.


== What is GraphQL?

GraphQL is a query language for APIs and a runtime for executing those queries against a type system you define for your data. It was developed by Facebook in 2012 and open-sourced in 2015.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add: It's often received as a technology for frontends, but it's equally helpful for machine-to-machine communication.


GraphQL is a query language for APIs and a runtime for executing those queries against a type system you define for your data. It was developed by Facebook in 2012 and open-sourced in 2015.

GraphQL is self-describing: the schema that powers an API also serves as its documentation, enabling powerful developer tooling and making APIs easier to evolve without versioning.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add: It's like OpenAPI and Swagger was already built-in.


GraphQL is self-describing: the schema that powers an API also serves as its documentation, enabling powerful developer tooling and making APIs easier to evolve without versioning.

A GraphQL service exposes a strongly typed schema that declares every type, field, and operation available. Clients construct queries that mirror the shape of the data they need, and the server responds with exactly that structure. This schema-first contract gives both client and server teams a shared, unambiguous definition of the API surface.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use "schema-first" here, as that's a different aspect. Just "This contract gives..."


* *Code-First Development*: Define GraphQL schemas using Java annotations on POJOs and methods, eliminating the need to author or maintain a separate schema file
* *CDI Integration*: Seamless integration with Jakarta Contexts and Dependency Injection for lifecycle management and service injection
* *Type Safety*: Java's type system drives schema generation, catching structural errors at compile time rather than at runtime
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would already add here something like: At the same time, resolvers allow for on-demand extension of the schema (see below).

with a link to the Using @Source to Resolve Nested Types section

}
----

Most MicroProfile implementations also provide a GraphiQL or GraphQL Playground interface for interactive exploration, typically accessible at `/graphql-ui` or `/graphiql`.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Most MicroProfile implementations also provide a GraphiQL or GraphQL Playground interface for interactive exploration, typically accessible at `/graphql-ui` or `/graphiql`.
Most MicroProfile implementations also provide a GraphiQL or GraphQL Playground interface for interactive exploration (think Swagger), typically accessible at `/graphql-ui` or `/graphiql`. You can also use external tools to interactively explore a GraphQL API.

}
----

The `@NonNull` annotation indicates that a field cannot be null, which translates to the `!` operator in GraphQL schema:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show the import for @NonNull; there are too many


=== Defining GraphQL Interfaces with @Interface

Use the `@Interface` annotation on a Java interface to define a GraphQL interface type. The MicroProfile GraphQL implementation generates a GraphQL interface in the schema for every class in the application that implements the annotated Java interface:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use the `@Interface` annotation on a Java interface to define a GraphQL interface type. The MicroProfile GraphQL implementation generates a GraphQL interface in the schema for every class in the application that implements the annotated Java interface:
Use the `@Interface` annotation on a Java interface to define a GraphQL interface type. The MicroProfile GraphQL implementation generates a GraphQL interface in the schema and a GraphQL type for every class in the application that implements the annotated Java interface:


== Integration with MicroProfile Config

MicroProfile GraphQL integrates with MicroProfile Config, allowing you to externalize configuration for your GraphQL services. This is particularly useful for controlling error message visibility across environmentsfor example, exposing detailed messages in development while suppressing them in production.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MicroProfile GraphQL integrates with MicroProfile Config, allowing you to externalize configuration for your GraphQL services. This is particularly useful for controlling error message visibility across environmentsfor example, exposing detailed messages in development while suppressing them in production.
MicroProfile GraphQL integrates with MicroProfile Config, allowing you to externalize configuration for your GraphQL services. This is particularly useful for controlling error message visibility across environments, for example, exposing detailed messages in development while suppressing them in production.


=== Query Naming and Descriptions - @Name, @Description, @DefaultValue

Use `@Name` to specify the name of a field or parameter in the GraphQL schema:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add that the parameter name in the source code is the default, but this requires setting compiler options (explain which ones)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants