How to get started with GraphQL testing?

Available in Samurai

What is GraphQL?


  • GraphQL is a syntax that describes how to ask for data, and is generally used to load data from a server to a client.
  • A GraphQL query is a string that is sent to a server to be interpreted and fulfilled, which then returns JSON back to the client.

How GraphQL queries are different from usual rest call statements?


With GraphQL, the user is able to make a single call to fetch the required information rather than to construct several REST requests to fetch the same.

image

Sample GraphQL queries


Query (Select)

A sample query to select a movie named Toy Story from a movie database can be queried as below. This is equivalent to a GET call and is always prefixed by the keyword query
query
{
Movie(title: "Toy Story")
{
title
year
imdbRating
}
}

Mutation (Create/Update)

The below gql creates a movie named Toy Story, a person named Kevin Bacon and an association between the two
mutation {
pp: createMovie(title:"Toy Story", released: 2018)
lw: createPerson(name:"Kevin Bacon", born: 1993)
cast: addMovieActors(title: "Toy Story",
actors:["Chadwick Boseman","Kevin Bacon"])
}

Schema

The SDL or popularly called the Schema Definition Language is what is required for a graphql server to interpret our gql queries. Below is a sample example of one such schema for a Movie and a Person graph
type Movie  {
title: String!
released: Int
actors: [Person] @relation(name:"ACTED_IN",direction:IN)
}
type Person {
name: String!
born: Int
movies: [Movie] @relation(name:"ACTED_IN")
}

Automated Testing Challenges


  • There is a lot of boilerplate code that goes into it so that the GraphQL server can understand the query definition
  • The conventional way of approaching to test integration requires wiring a custom HTTP client and dealing with desired headers, payload
  • This code demands a bespoke framework that needs to be maintained by the team of automation testers which becomes overhead in the long run and gets entangled with the test scripts/code

Automated Testing Using Samurai


  • The Samurai GraphQL automated testing provides out of the box approach to directly use the GraphQL queries in the DSL
  • It saves a significant amount of time for the tester and makes the maintenance of the tests very very easy
  • The testing can be broken down into these simple steps-
    1. Bring up the docker container service from docker/graphql/docker_image/docker-compose.yml using docker-compose up command
    2. Then user needs to dump his GraphQL data either as a gql query in the graphql folder (refer: graphql/add_movies.gql) or directly in the containerized graphdb available at http://localhost:7474. (Use 'neo4j' and 'test' as credentials)
    3. Or Configure the host to a hosted GraphQL server in an on-premise env or cloud
    4. Run the test from a usual IDE or from CLI in CI CD pipeline.
    5. Sample test for help can be found at graphql/GraphQLTest.java

Conclusion


  • For any other information related to graphql visit its official website- https://graphql.org/
  • A sample example is already provided in Samurai Hello Maven   project which can be referred for usage