How to connect graphiql to graphql-java endpoint

1,744 views
Skip to first unread message

Peace MICHAELS

unread,
Jun 2, 2016, 5:11:57 AM6/2/16
to graphql-java
i've asked in stackoverflow.com/questions/37528382/how-to-connect-graphiql-to-another-implementation-other-than-javascript simply put, how do i make graphiql work with my graphql-java endpoint? i'd appreciate if someone could point me in the right direction.

thanks
Peace.

Nikhil Agarwal

unread,
Jun 5, 2016, 12:46:28 AM6/5/16
to graphql-java
I believe that the graphiql is coded to work with graphql-js only. You'll have to fork it and modify somethings to make it accept a graphql server other than the express-graphql. The graphiql will want to be able to make an introspection query to generate a client schema that will validate the query using the browser based IDE.

Kocsis Tibor

unread,
Dec 8, 2016, 4:20:57 AM12/8/16
to graphql-java
Hi,

I found this page on google searching the same problem and found no solution. Eventually it works for me now, so maybe this post will be usefull.for others, too.

I'm using GraphiQL chrome extension, and the only thing I need to do is simple execute the introspection query with GraphQL instance.

In the following basic example I use spark framework to handle http requests and org.json json library. The code is the following:


        Spark.post("/query", (req, res) -> {
           
JSONObject query = new JSONObject(req.body());  // parse the complete request body, maybe you should print out the plain request to understand the GraphiQL query structrue
           
if (query.has("query") && query.getString("query").indexOf("IntrospectionQuery") != -1) {
               
String introSpection = query.getString("query");
               
Map<String, Object> result = new HashMap<>();
                result
.put("data", graphQLService.query(introSpection)); // you have to wrap it into a data json key!
               
return new JSONObject(result);
           
}
           
JSONObject queryResult = new JSONObject(graphQLService.query(query.getString("query")));
           
return queryResult;
       
});



My GraphSQLService just wraps the GaphQL execute command

 public Map<String, Object> query(String query) {
       
return (Map<String, Object>) new GraphQL(schema).execute(query).getData();
 
}

Hope it will help.

Regards
Tibor

gk

unread,
Apr 4, 2017, 9:57:34 PM4/4/17
to graphql-java

  Thanks a lot Tibor.  This really helped me. I just used this is my springboot application.  I created an endpoint like the following 

@RequestMapping(value = "/igraphql", method = RequestMethod.POST)

public Map<String, Object>  igraph(@RequestBody String json) throws Exception    {

logger.debug("JSON " + json);

JSONObject query = new JSONObject(json);

        if (query.has("query") && query.getString("query").indexOf("IntrospectionQuery") != -1) {

            String introSpection = query.getString("query");

            Map<String, Object> result = new HashMap<>();

            result.put("data", query(introSpection)); // you have to wrap it into a data json key!

            return result;

        }

        logger.debug("Introspect " + query.getString("query").indexOf("IntrospectionQuery"));

        

        Map<String, Object> queryResult = new HashMap<>(query(query.getString("query")));

        return queryResult;

  }


I used a chrome extension called iGraphQL where you can set the endpoint to http://<domain>/igraphql 

Ben O

unread,
May 6, 2017, 3:28:13 AM5/6/17
to graphql-java
Thanks, that helped.

Manish Maheshwari

unread,
Nov 27, 2018, 9:45:07 AM11/27/18
to graphql-java
Thanks, this helped.
Reply all
Reply to author
Forward
0 new messages