Getting Started with the Code Generator
This guide walks you through creating your first GraphQL API using the Code Generator with a simple "Hello World" example.
Prerequisites
- Java 11 or higher
- Basic understanding of GraphQL schemas
Step 1: Create Your Schema
Create a file hello.graphql with your GraphQL schema:
1"""Root query type."""2type Query {3"""Say hello to someone."""4hello(name: String!): String!5}
This minimal schema defines a single query that takes a name and returns a greeting.
Step 2: Run the Code Generator
You can run the code generator using the Builder API. Create HelloGraphQLCodeGenerator.java:
1package com.example.codegen;23import com.psddev.graphql.schema.codegen.GraphQLCodeGenerator;45public class HelloWorldCodeGenerator {6public static void main(String[] args) {7new GraphQLCodeGenerator.Builder()8.sdlPath("src/main/resources/hello.graphql")9.generatedSourcesPackageName("com.example.generated")10.generatedSourcesOutputDir("build/generated/graphql")11.classNamePrefix("Hello")12.templatedSourcesOutputDir("src/main/java")13.templatedSourcesPackageName("com.example.graphql")14.apiEndpointDisplayName("Hello API")15.apiEndpointPath("/hello")16.build()17.generate();18}19}
What Gets Generated
Running the generator creates generated sources in build/generated/graphql (regenerated on every run—don't edit these):
HelloSchemaContext.java— abstract base class you'll extend with your business logicHelloQuery.java— abstract model class for your schema'sQuerytypeHelloQueryTypeGenerator.java— registers the type with the schema engineHelloSchemaLoader.java— loads your schema and discovers your context implementationHelloSealedClass.java,HelloSealedInterface.java,HelloSealedClassException.java,HelloUtil.java— sealing and utility support
It also scaffolds a templated source into src/main/java (written once, never overwritten—it's yours to edit):
HelloEndpoint.java— the API endpoint serving your schema
Step 3: Implement the Schema Context
Create your implementation in src/main/java/com/example/graphql/HelloWorldSchemaContextImpl.java:
1package com.example.graphql;23import com.example.generated.HelloQuery;4import com.example.generated.HelloSchemaContext;56public class HelloWorldSchemaContextImpl extends HelloSchemaContext {78@Override9public HelloQuery query() {10return new HelloQuery() {11@Override12public String hello(String name) {13return "Hello, " + name + "!";14}15};16}17}
This is where you provide the actual business logic. The generated HelloSchemaLoader automatically discovers your concrete HelloSchemaContext implementation and calls your query() method to resolve GraphQL queries.
Step 4: Review the Endpoint
Because the generator was configured with a templatedSourcesOutputDir, it already scaffolded the endpoint that exposes your API—review and customize it as needed:
1package com.example.graphql;23import java.util.Set;45import com.example.generated.HelloSchemaLoader;6import com.psddev.dari.db.Singleton;7import com.psddev.graphql.AbstractGraphQLApiEndpoint;8import com.psddev.graphql.GraphQLSchemaLoader;9import com.psddev.graphql.schema.GraphQLSchemaSettings;1011@com.psddev.dari.db.Recordable.DisplayName("Hello API")12public final class HelloEndpoint extends AbstractGraphQLApiEndpoint implements Singleton {13public static final String PATH = "/hello";1415@Override16public Set<String> getPaths() {17return Set.of(PATH);18}1920@Override21protected GraphQLSchemaLoader getSchemaLoader() {22return new HelloSchemaLoader(GraphQLSchemaSettings.newBuilder()23.build());24}25}
Step 5: Test Your API
Start your application and test the endpoint with this query:
1query {2hello(name: "World")3}
Expected response:
1{2"data": {3"hello": "Hello, World!"4}5}
What You've Learned
You've successfully:
✅ Defined a GraphQL schema ✅ Generated type-safe Java code from the schema ✅ Implemented the business logic ✅ Exposed a working GraphQL API
Next Steps
- CLI Reference - Learn all code generator command-line options
- Gradle Integration - Integrate code generation into your build process
- Implementation Patterns - Best practices for implementing generated code
- Advanced Examples - Complex real-world scenarios