This tutorial aims to get you started with testing your API queries against ApiTestServer, a utility provided to do initial API testing. For this, little knowledge of Scala SBT and ScalaTest would be helpful.
Setting up Prisma
- To set up Prisma Development Environment make sure you have SBT installed on your system.
- To get the code for Prisma run the following commands.
This will fetch all the required packages.
git clone https://github.com/prismagraphql/prisma.git cd prisma/server sbt
- Next thing is to setup the Prisma Configuration file location. For that, you can set environment variable
PRSISMA_CONFIG_PATH=<Path of Yaml configuration File>
. You can look here for reference. If you are an Ubuntu user, you can run the following command to setup environment variableexport PRISMA_CONFIG_PATH="<Path to Prisma Config file>"
Writing your First Test :-
-
API test files are written at location
{Prisma_Directory}/server/servers/api/src/test/scala/com/prisma/api
. Go ahead and create your test file class and follow the naming conventions already used there. For exampleJsonVariablesSpec
. -
The test class that you have created should extend traits
FlatSpec
andMatchers
which are part of ScalaTest andApiSpecBase
which is trait provided in Prisma. -
The next thing is to define a schema against which you will test your APIs. For that, we will be using
com.prisma.shared.schema_dsl.SchemaDsl
. Import this in your test file and declare your schema. There different ways to declare schema. An example would beval project = SchemaDsl.fromString() { """ | type MyRequiredJson { | id: ID! @unique | json: Json! | } """.stripMargin }
PS: If your are interested in declaring complex schema then there are a lot of test cases in package
com.prisma.api.queries
. You can check them out. They help you in setting up a complex schema. -
After that, we will set up the database with the schema that we just created. For that call
database.setup(project)
database
is an instance ofApiTestDatabase
which comes with traitApiSpecBase
and by the usage, you can guess it that it is helping as a database utility. -
Now once the schema is set up at the backend, it is time to test a query. An example would be like :
val queryString = """mutation createMyRequiredJson($json: Json!) { | createMyRequiredJson(data: {json: $json}) { | id | json | } |}"""
For a detailed explanation on making a query, please refer Prisma Documentation.
-
Testing this query is really simple. In
ApiSpecBase
we already have aserver
variable which is a type ofApiTestServer
. This variable simulates a http end point of Prisma Server. An example usage would be like:val variables = Json.parse("""{"json": {"test": 1}}""") server.query(queryString, project, variables = variables)
For a detailed explanation on making a query, please refer Prisma Documentation.
-
Running the code is also super easy. Just go back to your sbt console that we had setup in this tutorial and running the following command.
testOnly <com.prisma.api.your.test.file>
This will only test your test cases. If you want to run all the tests, run the following command.
test
-
Congratulations! You ran your first test case. s