GithubHelp home page GithubHelp logo

prodanovic / graphql-spqr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leangen/graphql-spqr

0.0 1.0 0.0 738 KB

Java 8+ API for rapid development of GraphQL services

License: Apache License 2.0

Java 100.00%

graphql-spqr's Introduction

GraphQL SPQR

GraphQL SPQR (GraphQL Schema Publisher & Query Resolver, pronounced like speaker) is a simple-to-use library for rapid development of GraphQL APIs in Java.

Join the chat at https://gitter.im/leangen/graphql-spqr Maven Central Javadoc Build Status Hex.pm Semver

Intro

GraphQL SPQR aims to make it dead simple to add a GraphQL API to any Java project. It works by dynamically generating a GraphQL schema from Java code.

  • Requires minimal setup (~3 lines of code)
  • Deeply configurable and extensible (not opinionated)
  • Allows rapid prototyping and iteration (no boilerplate)
  • Easily used in legacy projects with no changes to the existing code base
  • Has very few dependencies

Code-first approach

When developing GraphQL-enabled applications it is common to define the schema first and hook up the business logic later. This is known as the schema-first style. While it has its advantages, in strongly and statically typed languages, like Java, it leads to a lot of duplication.

For example, a schema definition of a simple GraphQL type could like this:

type Link {
    id: ID!
    url: String!
    description: String
}

and, commonly, a corresponding Java type would exist in the system, similar to the following:

public class Link {
    
    private final String id;
    private final String url;
    private final String description;
    
    //constructors, getters and setters
    //...
}

Both of these blocks contain the exact same information. Worse yet, changing one requires an immediate change to the other. This makes refactoring risky and cumbersome. On the other hand, if you’re trying to introduce a GraphQL API into an existing project, writing the schema practically means re-describing the entire existing model. This is both expensive and error-prone, and still suffers from duplication.

Instead, GraphQL SPQR takes the code-first approach, by generating the schema from the existing model. This keeps the schema and the model in sync, easing refactoring. It also works well in projects where GraphQL is introduced on top of an existing codebase.

Installation

GraphQL SPQR is deployed to Maven Central.

Maven

<dependency>
    <groupId>io.leangen.graphql</groupId>
    <artifactId>spqr</artifactId>
    <version>0.9.6</version>
</dependency>

Gradle

compile 'io.leangen.graphql:spqr:0.9.6'

Hello world

The example will use annotations provided by GraphQL SPQR itself, but these are optional and the mapping is completely configurable, enabling existing services to be exposed through GraphQL without modification.

Service class:

class UserService {

    @GraphQLQuery(name = "user")
    public User getById(@GraphQLArgument(name = "id") Integer id) {
      ...
    }
}

Domain class:

public class User {

    private String name;
    private Integer id;
    private Date registrationDate;

    @GraphQLQuery(name = "name", description = "A person's name")
    public String getName() {
        return name;
    }

    @GraphQLQuery(name = "id", description = "A person's id")
    public Integer getId() {
        return id;
    }

    @GraphQLQuery(name = "regDate", description = "Date of registration")
    public Date getRegistrationDate() {
        return registrationDate;
    }
}

Exposing the service with graphql-spqr:

UserService userService = new UserService(); //instantiate the service (or inject by Spring or another framework)
GraphQLSchemaGenerator schema = new GraphQLSchemaGenerator()
    .withOperationsFromSingleton(userService) //register the service
    .generate(); //done ;)
GraphQL graphQL = new GraphQL(schema);

//keep the reference to GraphQL instance and execute queries against it.
//this operation selects a user by ID and requests name and regDate fields only
ExecutionResult result = graphQL.execute(   
    "{ user (id: 123) {
        name,
        regDate
    }}");

Full Tutorial

Work in progress

Full example

See more complete examples using Spring Boot at https://github.com/leangen/graphql-spqr-samples

graphql-spqr's People

Contributors

baldylocks avatar ellebrecht avatar kaqqao avatar miguelocarvajal avatar vojtechhabarta avatar

Watchers

 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.