LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Graphql Generator

These days, GraphQL is getting popular and many companies have adopted it to build their API and github.com is one of them. In most cases, developer will write their schema code with their favorite language and then wire in the business logic into the resolvers. Recently there is a IDL defined by the community; although it is not a standard yet, there are a lot of big players that supprot it. Our generator can utilize the GraphQL IDL or just scaffold a project with simple Hello World for developer to extend. Either way, you need to wire in your business logic after the project is generated.

Input

Model

There are two different ways to generate projects with light-graphql-4j generator.

  • With IDL - Pass in IDL and you need to wire in your backend logic.
  • Without IDL - A simple Hello World graphql application runnable as starting point.

NOTE: IDL is not currently part of the formal graphql spec. The implementation in this library is based off the reference implementation. However plenty of code out there is based on this IDL syntax and hence you can be fairly confident that you are building on solid technology ground.

We recommend using IDL. For more information about IDL, please check here

Config

Field NameDescription
nameused in generated pom.xml for project version
versionused in generated pom.xml for project version
groupIDused in generated pom.xml for project groupId
artifactIdused in generated pom.xml for project artifactId
schemaPackagethe package name for generated schema class
schemaClassthe generated schema class name
overwriteSchemaClasscontrols if the schema class needs to be generated or not. For new project, it should be true. If you want to upgrade the framework to a new version but don’t want to overwrite the updated schema class, then set it to false and regenerate to the same folder.
httpPortthe http port number the server is listening to if it is enabled.
enableHttpthe flag to control if http is enabled or not.
httpsPortthe https port number the server is listening to if it is enabled.
enableHttpsthe flag to control if https is enabled or not.
supportDbto control if db connection pool will be setup in service.yml and db dependencies are included in pom.xml
dbInfosection is the database connection pool configuration info.
supportH2ForTesta flag to control if H2 code is included in the test server and H2 jar is included in pom.mxl
supportClienta flag to control if client module is included in the generated project to call other services.

Here is an example of config.json for light-graphql-4j generator.

{
  "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
}

In most cases, developers will only update schema class and other depending classes in schema package.

Usage

Java Command line

You can download or build the codegen-cli command line jar.

Without IDL

First, let’s generate schema as “Hello World” as a starting point so that developers can update it to manually code their schema class.

The following command will create a sample GraphQL server project in the /tmp/graphql folder.

Working directory: light-codegen

java -jar codegen-cli/target/codegen-cli.jar -f light-graphql-4j -o /tmp/graphql -c light-graphql-4j/src/test/resources/config.json

And to build and run the server:

cd /tmp/graphql
mvn clean install exec:exec

Open your browser and point to http://localhost:8080/graphql for the graphiql interface to show up in your browser.

With IDL

Let’s generate a graphql project with IDL. There is a starwars IDL in our test folder.

Working directory: light-codegen

java -jar codegen-cli/target/codegen-cli.jar -f light-graphql-4j -o /tmp/graphql -m light-graphql-4j/src/test/resources/schema.graphqls -c light-graphql-4j/src/test/resources/config.json

The generated project can be built but not runnable as there is no backend code wired in yet. Take a look at the schema class and make the change in the commented section to complete it.

Docker Command Line

The following command is using docker to generate light-graphql-4j into /tmp/light-codegen/graphql folder

docker run -it -v ~/networknt/light-codegen/light-graphql-4j/src/test/resources:/light-api/input -v /tmp/light-codegen:/light-api/out networknt/light-codegen -f light-graphql-4j -c /light-api/input/config.json -o /light-api/out/graphql

On Linux environment, the generated code might belong to root:root and you need to change the owner to yourself before building it.

Let’s change the owner and build the service

cd /tmp/light-codegen
sudo chown -R steve:steve graphql
cd graphql
mvn clean install exec:exec

To test the server, please follow the instructions above in the utility command line.

Docker Scripting

git clone [email protected]:networknt/model-config.git
cd model-config
./generate.sh light-graphql-4j ~/networknt/model-config/graphql/helloworld /tmp/graphql

Now you should have a project generated in /tmp/graphql/generated

Codegen Site

The service API is ready. We are working on the UI with a generation wizard.

  • 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
“Graphql Generator” was last updated: July 5, 2021: fixes #275 checked and corrected grammar/spelling for majority of pages (#276) (b3bbb7b)
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