Giter Club home page Giter Club logo

gqltest's Introduction

gqltest

GraphQL testing tool

Syntax draft

gqltest provides directives which let you specify the expected behaviour of your test queries directly in your graphql test query files. To accommodate for dynamic values there is support for variables and dynamic string generation.

Test Directives

enum Assertion {
  NOT_NULL
}

union DeepValue = String | Int | Float | Boolean | ID
union Expectation = DeepValue | Assertion
union Variable = DeepValue

directive @before on MUTATION
directive @after on MUTATION
directive @test on MUTATION | QUERY

directive @expect(v: Expectation!) on FIELD
directive @set(v: Variable!) on FIELD

Verify field values

query TestUserQuery @test {
    user {
        name @expect(v: "John Doe")
        email @expect(v: "[email protected]")
    }
}

Above test expects the result of the query to look like this:

{
  "data": {
    "name": "John Doe",
    "email": "[email protected]"
  },
  "errors": []
}

Hooks & Test isolation

Use the @before and @after directive to mark certain mutations as hooks. Before a test is executed, all @before hooks from the same directory and all parent directories are executed. After a test the @after mutations will be executed to do cleanup work.

Be aware that all tests are executed in parallel which means that your queries must be written to work isolated.

Imagine an API where a signup mutation exists. It creates a new user account, but will respond with an error if the email is already used for another account.

If we were to create the following hook to setup a user account before a test is executed, we would receive an error from our API. It uses the same email ("[email protected]") for every account it tries to create.

signup.graphql:

mutation CreateUser @before {
    signup(name: "John Doe", email: "[email protected]", password: "asdf") {
        name @expect(v: "John Doe")
        email @expect(v: "[email protected]")
    }
}

To solve this problem we would use a generated variable.

The email could be generated using the $i variable, which represents a number that is unique for each test.

variables.json:

{
  "email": "john{{$i}}@doe.de"
}

The email will be "[email protected]" for the first test, "[email protected]" for the second, "[email protected]" for the third,...

signup.graphql:

mutation CreateUser ($email: String!) @before {
    signup(name: "John Doe", email: $email, password: "asdf") {
        name @expect(v: "John Doe")
        email @expect(v: $email)
    }
}

# Use the same variable remove the account after the test.
mutation RemoveUser ($email: String!) @before {
    removeUser(email: $email) {
        success @expect(v: true)
    }
}

Variables

Variables are declared and initialized in a file called variables.json. They then can be accessed from the graphql files in the same directory and all subdirectories.

variables.json:

{
  "email": "[email protected]",
  "name": "John Doe",
  "password": "asdf",
  "id": ""
}

userTest.graphql:

mutation CreateUser ($email: String!, $name: String!, $password: String!, $id: String!) @test {
    signup(email: $email, name: $name, password: $password) {
        id @set(v: $id)
    }
}

mutation RemoveUser ($id: ID) @test {
    deleteAccount {
        id @expect(v: $id)
    }
}

In the CreateUser mutation definition the user is registered (and is therefore automatically logged in). Because the user ID is generated by the API, we cannot specify it as a fixed value in the variables.json file. We need the ID to make sure that the deleteAccount mutation returns the correct value.

Using the @set directive we store the id field value in the variable $id.

We can then use said variable to validate the result of the deleteAccount mutation.

gqltest's People

Contributors

robojones avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

gqltest's Issues

Graphql doesn't support union input types

Problem

The draft syntax which aims to use the @expect directive for all comparable types. This requires the usage of a union type which is not supported by the current graphql syntax.

@expect(v: "hello")
@expect(v: 5)

This syntax would need v to have a union type which contains String and Int.

There are two alternatives to work around the usage of a union type:

Solution 1: Define the expect directive with an optional parameter for each type

directive @expect(string: String, int: Int, float: Float, boolean: Boolean, id: ID)

This approach allows the user to use the directive like this:

@expect(string: "hello")

The problem is that the directive definition would allow the user to specify multiple values:

@expect(string: "hello", int: 15)

Another problem is that when using variables these could use optional types which is not intended.

Solution 2: Define separate directives for all the types

@expectString(equal: "hello")
@expectInt(equal: 15)

This syntax is less good looking but allows for easier verification. The problem here is that the user could use multiple different @expect* directives on the same field.

Does not send operation content

If a test file contains a query:

query testUserName @test {
  user {
    name @expect(v: "Frank")
  }
}

The only thing that is actually sent to the API is query everything else is omitted.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.