Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL implementation Java [closed]

Tags:

java

rest

graphql

I am having 5 rest API's(java) with different endpoint URL,and each have different request,response format.So I have combined them as a single API with a common JSON complex request and response as a key value pair structure.

Now T found recently about GraphQL and interested to fit it in my requirement, I have done analysis on that and want to know how well I Could implement some of my questions:

1) Can we implement complex rest API service(post) in GRAPHQL?(while googling,got only for simple get method.Also is there only for node/javascript)

2) Is there any framework for java based graphQL implementation?

like image 501
Shivababa Avatar asked Aug 13 '16 15:08

Shivababa


People also ask

Can I use GraphQL with Java?

GraphQL is a cross-platform, open-source, data query, and manipulation language for APIs. GraphQL servers are available for multiple languages such as Java, Python, C#, PHP, R, Haskell, JavaScript, Perl, Ruby, Scala, Go, Elixir, Erlang, and Clojure, etc. So, it can be used with any programming language and framework.

Is GraphQL overkill?

GraphQL can be overkill Since GraphQL is so complex and powerful, it can commonly be overkill for smaller applications. If you have a simple/straight-forward app in front of you, it will be easier to work with a REST API.

Is GraphQL still REST?

The core difference between GraphQL and REST APIs is that GraphQL is a specification, a query language, while REST is an architectural concept for network-based software.

Is GraphQL going to replace REST?

As previously mentioned, GraphQL does not replace REST. Despite their differences, GraphQL and REST also have similarities: They can both be fetched by an HTTP GET request with a URL and return the request in JSON data. GraphQL and REST allow the specification of IDs for resources.


2 Answers

1) Can we implement complex rest API service(post) in GRAPHQL?(while googling,got only for simple get method.Also is there only for node/javascript)

Since you say you did your analysis about GraphQL, I'm assuming that by "implement complex REST API server (POST) in GraphQL", you meant how you can expose the functionality of REST API through GraphQL. Yes, you can do that by using GraphQL mutations. In your mutation implementation (resolve function), you'll invoke the REST POST operations.

swapi-graphql is an interesting project that wraps Star Wars REST API.

2) Is there any framework for java based graphQL implementation?

Check out Java section of awesome-graphql. The Java GraphQL library doesn't seem to be well-maintained though.

like image 176
Ahmad Ferdous Avatar answered Sep 22 '22 14:09

Ahmad Ferdous


Question 1) is answered in the other answer.

Question 2) Java Implementation:

  • graphql-java is a Java implementation for GraphQL.
  • graphql-java-annotations is a Java Library build on graphql-java.

Sample Code:

public class HelloWorld {
    public static void main(String[] args) {
        GraphQLObjectType queryType = newObject()
            .name("helloWorldQuery")
            .field(newFieldDefinition()
                .type(GraphQLString)
                .name("hello")
                .staticValue("world"))
            .build();

        GraphQLSchema schema = GraphQLSchema.newSchema()
            .query(queryType)
            .build();

        GraphQL graphQL = GraphQL.newGraphQL(schema).build();

        Map<String, Object> result = graphQL.execute("{hello}").getData();
        System.out.println(result);
        // Prints: {hello=world}
    }
}
like image 23
Matthias M Avatar answered Sep 23 '22 14:09

Matthias M