Search code examples
graphqlquarkussmallrye

Quarkus SmallRye Graphql-Client Mutation Query


I try to execute a Graphql Client Query. Sadly I am not able to find any kind of documentation or examples on how to do a simple Mutation using the Dynamic Graph QL Client. Here is the documentation https://quarkus.io/guides/smallrye-graphql-client.

mutation mut {
  add(p: {
    amount: {0},
    fromCurrencyId: {1},
    reference: {2},
    terminalKey: {3},
    toCurrencyId: {4}
  }) {
    address
    toCurrencyAmount
    rate
    createdAt
    expireAt
  }
}

{0}..{4} are variable place holder. Does someone know how to execute this query with the DynamicGraphlQlClient?

Thanks!


Solution

  • Having declared your server side mutation following the Eclipse MicroProfile API as follows:

    @GraphQLApi
    @ApplicationScoped
    public class MyGraphQLApi {
    
        @Mutation
        public OutputType add(@Name("p") InputType p)) {
            // perform your mutation and return result
        }
    
    }
    

    You can then use the DynamicGraphQLClient declaratively to perform the mutation using the DynamicGraphQLClient#executeSync method with a io.smallrye.graphql.client.core.Document constructed after your mutation structure:

    @Inject
    private DynamicGraphQLClient client;
    
    public void add() {
        Document document = Document.document(
            operation(
                OperationType.MUTATION,
                "mut",
                field(
                    "add",
                    arg(
                        "p",
                        inputObject(
                            prop("amount", "amountValue"),
                            prop("fromCurrencyId", "fromCurrencyIdValue"),
                            prop("reference", "referenceValue"),
                            prop("terminalKey", "terminalKeyValue"),
                            prop("toCurrencyId", "toCurrencyIdValue")
                        )
                    )),
                field("address"),
                field("toCurrencyAmount"),
                field("rate"),
                field("createdAt"),
                field("expireAt")
            )
        );
    
        JsonObject data = client.executeSync(document).getData();
        System.out.println(data.getString("address"));
    }