Git Product home page Git Product logo

graphql_java_gen's Introduction

GraphQLJavaGen

Build Status

Generate code for a specific GraphQL schema that provides query builders and response classes.

Installation

The code generator requires ruby version 2.1 or later.

It is recommended to use bundler to install the code generators ruby package.

Add this line to your application's Gemfile:

gem 'graphql_java_gen'

And then execute:

$ bundle

The generated code depends on the com.shopify.graphql.support java package. This can be added to a gradle project by adding the following jCenter dependancy to you build.gradle file:

compile 'com.shopify.graphql.support:support:0.1.0'

Usage

Create a script that generates the code for a GraphQL API

require 'graphql_java_gen'
require 'graphql_schema'
require 'json'

introspection_result = File.read("graphql_schema.json")
schema = GraphQLSchema.new(JSON.parse(introspection_result))

GraphQLJavaGen.new(schema,
  package_name: "com.example.MyApp",
  nest_under: 'ExampleSchema',
  custom_scalars: [
    GraphQLJavaGen::Scalar.new(
      type_name: 'Decimal',
      java_type: 'BigDecimal',
      deserialize_expr: ->(expr) { "new BigDecimal(jsonAsString(#{expr}, key))" },
      imports: ['java.math.BigDecimal'],
    ),
  ]
).save("#{Dir.pwd}/../MyApp/src/main/java/com/example/MyApp/ExampleSchema.java")

That generated code depends on the com.shopify.graphql.support package.

The generated code includes a query builder that can be used to create a GraphQL query in a type-safe manner.

String queryString = ExampleSchema.query(query -> query
    .user(user -> user
        .firstName()
        .lastName()
    )
).toString();

The generated code also includes response classes that will deserialize the response and provide methods for accessing the field data with it already coerced to the correct type.

ExampleSchema.QueryResponse response = ExampleSchema.QueryResponse.fromJson(responseJson);
if (!response.getErrors().isEmpty()) {
    for (Error error : response.getErrors()) {
        System.err.println("GraphQL error: " + error.message());
    }
}
if (data != null) {
    ExampleSchema.User user = data.getUser();
    System.out.println("user.firstName() + " " + user.lastName());
}

Lambda Expressions

The java 8 lambda expressions feature is essential for having a nice syntax for the query builder. This feature is also available for Android apps by using the Jack toolchain.

If an older version of java must be used, then retrolambda can be used to backport this feature to java 5-7.

Development

After checking out the repo, run bundle to install ruby dependencies. Then, run rake test to run the tests.

To install this gem onto your local machine, run bundle exec rake install or reference it from a Gemfile using the path option (e.g. gem 'graphql_java_gen', path: '~/src/graphql_java_gen').

Contributing

See our contributing guidelines for more information.

License

The gem is available as open source under the terms of the MIT License.

graphql_java_gen's People

Contributors

eapache avatar dylanahsmith avatar benemdon avatar sav007 avatar henrytao-me avatar caiopia avatar danieljette avatar

Watchers

James Cloos avatar Bhumit Patel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.