19

I am new to GraphQL. I know it is very basic question. But tried spending lot of time and i couldn't make it.

My requirement is i need to send GraphQL query by using graphql-java api methods from a java class.

Here is the query:

{
  contentItem(itemId: 74152479) {
    slug
    updatedAt
    contributors {
      id
      isFreeForm
      name
    }
  }
}
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
HemaSundar
  • 1,263
  • 3
  • 17
  • 28
  • 4
    were you able to send the query. If you were can you please provide the code you used to send the `graphql` query in java. It may help quick visitors of this question – Kasun Siyambalapitiya Feb 02 '17 at 12:57
  • 2
    graphql-java is for GraphQL server development, not a client for sending queries – kaqqao Aug 08 '19 at 02:56

3 Answers3

7

First, you have to illustrate more on your problem, from your sample query I can't actually see which part you are having problem, it could be in argument, nested object, or data fetcher

I'm new to GraphQL(java) as well, instead of sharing the direct answer with you, I intended to show you how I resolve the similar problem.

graphql-java actually did a great job in their test cases. You can refer here: src/test/groovy/graphql to get some ideas on how to create and query GraphQL schema.

Arguments

I found a similar case like yours in here: StarWarsSchema.java#L137

newFieldDefinition()
    .name("human")
    .type(humanType)
    .argument(newArgument()
        .name("id")
        .description("id of the human")
        .type(new GraphQLNonNull(GraphQLString))
        .build())
    .dataFetcher(StarWarsData.getHumanDataFetcher())
    .build())

In this case, only one argument is defined, which is id. new GraphQLNonNull(GraphQLString) tells us this is is a mandatory string argument.

Fields

For fields, it is defining in humanType, you can refer to StarWarsSchema.java#L61. Nested fields is just another type with some fields, eg, .type(nestedHumanType)

Data Fetcher

After all, you might to process the argument id and return some data. You can refer to the example here: StarWarsData.groovy#L89

To make my code looks cleaner, normally I will create a separate class for DataFetcher, eg:

public class HumanDataFetcher implements DataFetcher {
    @Override
    public Object get(DataFetchingEnvironment environment) {
        String id = (String)environment.get("id");
        // Your code here
    }
}

Hope this helps.

Dawngerpony
  • 3,288
  • 2
  • 34
  • 32
ch33hau
  • 2,811
  • 1
  • 15
  • 15
  • can you please help me? i need a mutation query like this mutation{ Author(id:2,name:"ajmal",posts:[{id:1,title:"hello"}]){ id } } – Ajmal Sha May 19 '17 at 10:24
  • That's a very vague question. However the answer describes exactly how you should go about executing this query. Start with building the model first wherein you define all the fields along with their types. Then `@Override` the `get()` method in the `DataFetcher` interface, this method should return whatever data you want to return to the user. The `DataFetchingEnvironment` argument essentially contains all the fields provided by the user mutation as long as they are defined in your model. – sbrk Aug 04 '17 at 19:32
  • The question is about querying a GraphQL server, not about developing one. – kaqqao Aug 08 '19 at 02:57
4

Firstly, make sure that you can get result by curling like so. if this works (make sure to change the verb to GET depending on what verb server expects) then all you need to do is send the query part in http request body with content-type as application/json.

curl \
    -X POST \
    -H "Content-Type: application/json" \
    --data '{ "query": "{ contentItem(itemId: \"74152479\") { slug updatedAt    contributors { id isFreeForm name } } }" }' \
    http://www.yoursite.com/your/graphql/api

once successful you can build the query and send the request using Java http clients. I have successfully done so using Jersey client. only challenge is building the query if you want to have some generic query builder. but there are query builder on github like this ONE, and you can tweak it to suit your needs.

All in all, following is what you need to send in http body. I put his in one liner,which is exactly how the request body looks like. you have to remove any extra format like new line etc.

{ "query": "{ contentItem(itemId: \"74152479\") { slug updatedAt    contributors { id isFreeForm name } } }" }
yantaq
  • 3,968
  • 2
  • 33
  • 34
2

I've got a solution which is implemented in vertx-graphql-client.

The process to universally make a GraphQL query is:

  1. Rewriting your query using variables

So your query may look like:

query contentItem($itemId: Int){
  contentItem(itemId: $itemId) {
    slug
    updatedAt
    contributors {
      id
      isFreeForm
      name
    }
  }
}
  1. Send your query via HTTP POST requests with

    • header: set content-type to application/json

    • body: the body is set by serializing following JSON data:

{
    "query": "the-templated-query-above",
    "operationName": "contentItem",
    "variables": {
        "itemId": 74152479
    }
}

With curl, it's easy as:

curl \
    -X POST \
    -H "Content-Type: application/json" \
    --data '{ "query": "the-templated-query-above", "operationName": "contentItem", "variables": {  "itemId": 74152479 }}' \
    http://www.yoursite.com/your/graphql/api
anhldbk
  • 4,559
  • 5
  • 29
  • 37