graphql java 125 lines · 3 tabs

GraphQL API with Spring Boot

David Kumar Jan 2026
3 tabs
type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]!
    createdAt: String!
}

type Post {
    id: ID!
    title: String!
    content: String!
    author: User!
    comments: [Comment!]!
    createdAt: String!
}

type Comment {
    id: ID!
    content: String!
    author: User!
    post: Post!
    createdAt: String!
}

type Query {
    user(id: ID!): User
    users(page: Int = 0, size: Int = 10): [User!]!
    post(id: ID!): Post
    posts(userId: ID): [Post!]!
}

type Mutation {
    createUser(name: String!, email: String!, password: String!): User!
    updateUser(id: ID!, name: String, email: String): User!
    deleteUser(id: ID!): Boolean!
    createPost(userId: ID!, title: String!, content: String!): Post!
}

type Subscription {
    postCreated: Post!
}
3 files · graphql, java Explain with highlit

GraphQL provides flexible APIs where clients specify exact data requirements. Spring for GraphQL integrates GraphQL Java with Spring Boot. Schema-first approach defines types in .graphqls files. Resolvers map schema fields to Java methods using @QueryMapping, @MutationMapping. DataLoader prevents N+1 queries through batching. GraphQL enables fetching related data in single request, reducing over-fetching and under-fetching. Subscriptions support real-time updates via WebSocket. Error handling uses DataFetcherExceptionHandler. Pagination uses cursor-based approach. GraphQL excels for complex data requirements, mobile apps, and evolving APIs. The self-documenting schema simplifies frontend development. Spring's annotation-based approach makes GraphQL accessible to Java developers familiar with REST.


Related snips

ruby
payload = {
  sub: user.id,
  iss: 'https://auth.example.com',
  aud: 'codesnips-api',
  exp: 15.minutes.from_now.to_i,
  iat: Time.now.to_i,

JWT issuance and verification without common footguns

jwt authentication api
by Kai Nakamura 2 tabs
ruby
module Api
  module V1
    class UsersController < BaseController
      def show
        user = User.includes(:profile).find(params[:id])

ETags for conditional requests and caching

rails caching http-caching
by Alex Kumar 1 tab
java
package com.example.starter.config;

import com.example.starter.properties.CustomProperties;
import com.example.starter.service.CustomService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

Custom Spring Boot starters

java spring-boot starter
by David Kumar 4 tabs
typescript
import axios, { AxiosError } from 'axios'
import { v4 as uuidv4 } from 'uuid'

const api = axios.create({
  baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3000/api/v1',
  timeout: 15000,

Axios API client with interceptors

react axios api
by Maya Patel 1 tab
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment


class PostType(DjangoObjectType):

Django GraphQL with Graphene

django python graphql
by Priya Sharma 2 tabs
ruby
class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :excerpt, :body, :published_at, :views, :likes_count, :comments_count

  attribute :can_edit, if: :current_user_can_edit?

  belongs_to :author, serializer: UserSummarySerializer

Serializers with ActiveModel::Serializers

rails api serialization
by Alex Kumar 2 tabs

Share this code

Here's the card — post it anywhere.

GraphQL API with Spring Boot — share card
Link copied