How to perform GraphQL integration testing?

Available in Samurai

Getting Started


Before continuing this page, it is advised that you visit GraphQL Getting Started page.

DSL for the Test


This test does 3 things:-

  • It creates a Graphql schema
  • Adds a few entities to the graphdb
  • Get person & movies for a particular test scenario and asserts the test results
{
"scenarioName": "GraphQL with query as POST - Movies",
"steps": [
{
"name": "create_schema",
"url": "http://localhost:7474/graphql/idl",
"method": "POST",
"request": {
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic bmVvNGo6dGVzdA=="
},
"body": {
"query": "${GQL.FILE:graphql/schema.gql}"
}
},
"assertions": {
"status": 200,
"body": {
"Movie": "$NOT.NULL"
}
}
},
{
// Adds a new movie every, person time
"name": "add_movies",
"url": "http://localhost:7474/graphql/",
"method": "POST",
"request": {
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic bmVvNGo6dGVzdA=="
},
"body": {
"query": "${GQL.FILE:graphql/add_movies.gql}"
}
},
"assertions": {
"status": 200,
"body": {
"data": {
"pp": "$NOT.NULL",
"lw": "$NOT.NULL",
"cast": "$NOT.NULL"
}
}
}
},
{
"name": "get_person_movies",
"url": "http://localhost:7474/graphql/",
"method": "POST",
"request": {
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic bmVvNGo6dGVzdA=="
},
"body": {
"query": "${GQL.FILE:graphql/query_movies.gql}"
}
},
"verify": {
"status": 200,
"body": {
"data": {
"Person[0].name": "Kevin Bacon",
"Person[0].movies[0].actors[0].born": 1993
}
}
}
}
]
}
Field Value
url This is the URL of your graphQL server. This remains same, does not often change like REST apis.
See the url in the create_schema step, It's slightly different url.
query "GQL.FILE:" Point to your GraphQL query file in the test/resources folder or sub-folder

Other fields are the same as REST API automation steps.

GraphQL Query


{
Person(name: "Kevin Bacon") {
name
born
movies(title: "Toy Story") {
title
actors {
_id
born
name
}
}
}
}

GraphQL Mutation


Create Node

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"])
}

GraphQL Create Schema


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")
}

GraphQL Delete Node(s)


{
mutation {
deleteMovie(title: "Toy Story")
deletePerson(name: "Kevin Bacon")
}
}

Creating a Graphql JUnit test


@TargetEnv("graphql/graphql_docker_host.properties")
@RunWith(Samurai.class)
public class GraphQLTest {

@Test
@Scenario("graphql/graphql_docker_query_movies_test.json")
public void testGraphQL_queryMovies() throws Exception {
}

}

Graphql Server Host Properties


# GraphQL host and port
web.application.endpoint.host=http://localhost
web.application.endpoint.port=8888