Test a Spring Boot GraphQL API
Test GraphQL API with GraphiQL tool
GraphQL is a query language for APIs or a syntax that describes how to ask for data and is generally used to load data from a server to a client. GraphQL offers maximum efficiency and flexibility because it allows the calling client to specify exactly what data it needs.
We will use the same example in the previous article, if you didn’t read it, take a look at this link.
GraphiQL tool:
GraphiQL is a simple web app that is able to communicate with any GraphQL Server and execute queries and mutations against it.
Add the following dependency in pom.xml:
To enable the GraphiQL tool, add the following lines to the application.properties file:
Note that graphql.servlet.mapping, graphiql.endpoint, and graphiql.mapping doesn’t have to be the same as in this example, but it will affect where you go in the future. However, graphql.servlet.mapping and graphiql.endpoint, do need to match, as that is how GraphQL and GraphiQL will communicate with each other.
Now run the project as a spring boot application, then go to localhost:8080/graphiql , The following interface will open.
In right, you will see the schema as defined in location.graphqls in the project.
Query request:
On the left side of the interface, it’s where we write the queries.
You can customize the query to fetch data that you need, for example, if you want just names.
Mutation request:
For mutations, we defined three mutations in schema and there is an example for each one:
You can try other requests in the same way.
Resources: