LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Hello World Tutorial

Like light-rest-4j we have light-codegen to generate a project from either Swagger 2.0 or OpenAPI 3.0 specification, we can generate GraphQL project from GraphQL IDL. Or generate without providing any model. For this Hello World example, we are going to generate it with only a config.json.

Prepare environment

First, we need to clone light-codegen, model-config, and light-example-4j into your workspace. I am using networknt as workspace in my home directory.

cd ~/networknt
git clone https://github.com/networknt/light-codegen.git
git clone https://github.com/networknt/light-example-4j.git
git clone https://github.com/networknt/model-config.git

Let’s build light-codegen so that we can use the command line to generate the code.

cd ~/networknt/light-codegen
mvn clean install -DskipTests

Config

Let’s take a look at the config.json that control how the project is going to be generated. It can be found at https://github.com/networknt/model-config/blob/master/graphql/helloworld/config.json

Here is the content.

{
  "name": "starwars",
  "version": "1.0.1",
  "groupId": "com.networknt",
  "artifactId": "starwars",
  "schemaPackage": "com.networknt.starwars.schema",
  "schemaClass": "StarWarsSchema",
  "overwriteSchemaClass": true,
  "httpPort": 8080,
  "enableHttp": true,
  "httpsPort": 8443,
  "enableHttps": false,
  "enableRegistry": false,
  "supportDb": true,
  "dbInfo": {
    "name": "mysql",
    "driverClassName": "com.mysql.jdbc.Driver",
    "jdbcUrl": "jdbc:mysql://mysqldb:3306/oauth2?useSSL=false",
    "username": "root",
    "password": "my-secret-pw"
  },
  "supportH2ForTest": false,
  "supportClient": false
}

Generate

Given the light-codegen is built, let’s generate the project from the config file only without IDL. For more detail about the light-codegen please refer to light-codegen graphql.

We are going to generate the code to the same folder on light-example-4j/graphql/helloworld, let’s rename the existing folder so that you can compare after you follow the tutorial.

cd ~/networknt/light-example-4j/graphql
mv helloworld helloworld.bak

Now let’s run the command line in ~/networknt folder.

cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-graphql-4j -o light-example-4j/graphql/helloworld -c model-config/graphql/helloworld/config.json

Build and Start

Now let’s build and start the server.

cd ~/networknt/light-example-4j/graphql/helloworld
mvn clean install exec:exec

Test

To test the server:

curl -k -H 'Content-Type:application/json' -X POST https://localhost:8443/graphql -d '{"query":"{ hello }"}'

and the result is:

{"data":{"hello":"world"}}

To access GraphiQL, you need to put the following url into your browser’s address.

https://localhost:8443/graphql

On the left panel, enter the following to test the result.

{ hello }

Result

{
  "data": {
    "hello": "world"
  }
}

Rest Endpoints

With the handler.yml configuration for the middleware handler chain, we can add some Rest handlers to the mix so that the same server can serve both GraphQL and Restful endpoints.

In the generated GraphQL project, we have server/info and health endpoints built in. To access these endpoint.

curl -k https://localhost:8443//health/com.networknt.starwars-1.0.1

And the result is

OK
curl -k https://localhost:8443/server/info

And the result is

{"deployment":{"apiVersion":"SNAPSHOT","frameworkVersion":"2.0.5-SNAPSHOT"},"environment":{"host":{"ip":"127.0.0.1","hostname":"freedom","dns":"localhost"},"runtime":{"availableProcessors":16,"freeMemory":485311088,"totalMemory":528482304,"maxMemory":8432648192},"system":{"javaVendor":"Azul Systems, Inc.","javaVersion":"11.0.3","osName":"Linux","osVersion":"4.15.0-58-generic","userTimezone":"America/Toronto"}},"security":{"serverFingerPrint":"873b92f50afe99e9c5af38f1c865987ac113191d"},"specification":null,"component":{"com.networknt.correlation.CorrelationHandler":{"enabled":true,"autogenCorrelationID":true},"com.networknt.graphql.validator.ValidatorHandler":{},"com.networknt.traceability.TraceabilityHandler":{"enabled":true},"com.networknt.exception.ExceptionHandler":{"description":"Exception handler for runtime exception and ApiException if it is not handled by other handlers in the chain","enabled":true}}
  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Hello World Tutorial” was last updated: August 27, 2019: fixes #136 update the graphql tutorials to Java 11 (3e937d3)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute