简体   繁体   中英

How to parse and modify a hardcoded GraphQL query in Java to enable pagination?

I have a hardcoded GraphQL query and a Java application. I read the query from a file and then send it as payload of a POST request constructed using HttpRequest and HttpClient .

This works fine as long as the resulting JSON contains all the data that I need.

When it doesn't, the resulting JSON contains a cursor value that I can use for getting the next page of data.

Now, in order to use the cursor value and request the next page, I need to add the value to my GraphQL query. This means I need to somehow parse the query, traverse it, locate where to insert a new argument and pass the cursor value with it.

Trying to use Gson and parsing the query fails because GraphQL is not a subset of JSON.

The list of GraphQL tooling for Java doesn't seem to contain any libraries that would parse raw GraphQL queries. In general it looks like devs only write GraphQL servers in Java but not clients.

What are my options aside from just treating the GraphQL query as an unparsed text file using something like Scanner ?

(Disclaimer: I'm fairly new to Java.)

With graphql-java you can parse the query with the Parser without requiring any Schema definition.

Imports:

import graphql.parser.Parser;
import graphql.language.Document;
import graphql.language.Field;
import graphql.language.OperationDefinition;
import graphql.language.AstPrinter;
String query = "{ Book(id: \"1\") { name, genre } }";
Parser parser = new Parser(); 
Document document = parser.parseDocument(query);
System.out.println(document);

Output:

Document{
  definitions=[
    OperationDefinition{
      name='null',
      operation=QUERY,
      variableDefinitions=[],
      directives=[],
      selectionSet=SelectionSet{
        selections=[
          Field{
            name='Book',
            alias='null',
            arguments=[
              Argument{
                name='id',
                value=StringValue{value='1'}
              }
            ],
            directives=[],
            selectionSet=SelectionSet{
              selections=[
                Field{
                  name='name',
                  alias='null',
                  arguments=[],
                  directives=[],
                  selectionSet=null
                },
                Field{
                  name='genre',
                  alias='null',
                  arguments=[],
                  directives=[],
                  selectionSet=null
                }
              ]
            }
          }
        ]
      }
    }
  ]
}
*/

To get id arguments passed to book below code can be used

System.out.println(
    (
     (Field)
     ((OperationDefinition)document.getDefinitions().get(0))
     .getSelectionSet()
     .getSelections()
     .get(0)
    )
    .getArguments()
);

Output:

[Argument{name='id', value=StringValue{value='1'}}]

To convert Document back to query you can use AstPrinter

System.out.println(AstPrinter.printAst(document));

Output:

query {
  Book(id: "1") {
    name
    genre
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM