graphql-java-kickstart / graphql-spring-boot

GraphQL and GraphiQL Spring Framework Boot Starters - Forked from oembedler/graphql-spring-boot due to inactivity.
https://www.graphql-java-kickstart.com/spring-boot/
MIT License
1.5k stars 326 forks source link

How to programmatically create a GraphQl request #442

Closed andreienache97 closed 4 years ago

andreienache97 commented 4 years ago

Hi, Currently I am trying to make a graphql request in Spring Boot. This is my request :

mutation { update(bank: {id : "c0ad2522-c40c-4478-8b6f-b0261eab1116", bank : "bankExample", IBAN : "1234", currency : "GBP" }){ id } }

I’ve found this class GraphQLRequest which I can send as a POST request but my question is how can I programatically create the above mentioned query in Java. Since the syntax is not a json is there a builder or an object mapper for this? I’ve tried to check everywhere but could not find an example or documentation. Thank you for you time.

laszlopalfi commented 4 years ago

I'm having the same issue, can anybody from the team help out with some documentation?

oliemansm commented 4 years ago

This library is for creating a GraphQL server, not for use as GraphQL client which is what you seem to be looking for.

neerajsu commented 4 years ago

@andreienache97 and @laszlopalfi

Assuming your schema based on your question

mutation {
  update(bank: Bank!)
}

input Bank {
 id: String!
 bankName: String!
 iban: String!
 currency: String!
}

Your mutation would be (This would be the string that you would pass to GraphQLRequest query)

mutation updateBankMutation($bank: Bank!) {
  update(bank: $bank) {
     id
  }
}

Your input variables would be (Note: - since this is a json, you can programatically create/change it using jackson or any other pojo < - > json library)

{
  "bank" : {
       "id": "c0ad2522-c40c-4478-8b6f-b0261eab1116",
       "iban": "1234",
       "bankName" : "bankExample",
       "currency" : "GBP"
   }
}

To programmatically send this request using GraphQLRequest you would do the following.

Create GraphQLRequest

If you want to send another request with different variables, just send another request with different set of bank input variables

You can do all of this programatically. That is how you do testing anyway.

See how GraphQLTestTemplate is implemented. You can just use the code there to create your request. Instead of TestRestTemplate you can use RestTemplate