npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-service-tester

v1.4.1

Published

Tests GraphQL service queries and mutations via schema introspection

Downloads

155

Readme

GraphQL Service Tester

GraphQL Service Tester (a fork of graphql-query-generator) uses schema introspection to create smoke tests automatically from the comments on the query and mutation! This can provide a lot of test coverage without having to write any lines of code. And give you some peace of mind when making changes to the service to help prevent regressions. This is a great tool to have run as part of your CI build.

Example

By adding comments in this format you allow the tester to create a query that will test all the Playlist fields.

type Query {
  """
  Examples:
  playlist(id: "aihSOj7Qs0yzd6Kfc4x7Bg")
  """
  playlist(id: ID!): Playlist
}

Getting Started

To get a local copy up and running follow these simple example steps.

Installation

  1. Install NPM packages
npm install graphql-service-tester

Usage

Execute the following commands to get this tool running.

graphql-service-tester http://<your-server-address>:<your-server-port>
graphql-service-tester --help # for more information

Passing Headers

You can pass multiple headers by using the -H or --header flag and passing in a header in the format of key:value.

graphql-service-tester http://<your-server-address>:<your-server-port> -H "X-My-Header: my-value" -H "X-Second-Header: my-other-value"

Example:

graphql-service-tester http://localhost:3000/graphql -H "Authorization: eyJraWQiOiJUU25QbjM...."

Features

  • Test Queries And Mutations
  • Response Time SLA Testing
  • Data Passing Between Tests
  • Dependency Ordering
  • Query Directives
  • Define Cleanup Tests That Run Last
  • Array Assertions
  • Opt-Out of Certain Queries

Query And Mutations Testing

Both queries and mutations are supported.

type Query {
  """
  Examples:
  playlist(id: "aihSOj7Qs0yzd6Kfc4x7Bg")
  """
  playlist(id: ID!): Playlist
}

type Mutation {
  """
  Examples:
  createPlaylist(name: "Summer Mix")
  """
  createPlaylist(name: String!): Playlist
}

type Playlist {
  id: ID!
  name: String!
}

Directives

Directives allow adding additional functionality to the tests by adding @directiveName after the query. In the example below, @last() is a directive to allow the removePlaylist() mutation to run last. Queries support multiple directives.

type Mutation {
  """
  Examples:
  removePlaylist(id: "aihSOj7Qs0yzd6Kfc4x7Bg") @last()
  """
  removePlaylist(id: ID!): Playlist
}

Response Time SLA Directive

Add an @sla directive to your query to tell it how long it should take to run.

type Query {
  """
  Examples:
  playlist(id: "aihSOj7Qs0yzd6Kfc4x7Bg") @sla(maxResponseTime: "600ms")
  """
  playlist(id: ID!): Playlist
}

If the tests take longer than the max response type the test runner will fail the test and output a message about the response time that was exceeded.

  playlist(id: "aihSOj7Qs0yzd6Kfc4x7Bg")       805ms

  SLA response time 600ms exceeded

The SLA maxResponseTime must be a string and uses the ms library to parse the string with units into milliseconds.

"600ms", "2s", "1.2s"

Waits

The @wait directive causes the tester to sleep for the amount of time before running the test

type Query {
  """
  Examples:
  playlist(id: "aihSOj7Qs0yzd6Kfc4x7Bg") @wait(waitTime: "1s")
  """
  playlist(id: ID!): Playlist
}

The wait waitTime must be a string and uses the ms library to parse the string with units into milliseconds.

"600ms", "2s", "1.2s"

Multiple Queries and Aliasing

Multiple queries can be defined for a single API.

   If multiple queries are used each query MUST have a unique alias.  Aliases are OPTIONAL if the API has a single query.
type Mutation {
  """
  Examples:
  summerPlaylist: createPlaylist(name: "Summer Mix")
  fallPlaylist: createPlaylist(name: "Fall Mix")
  """
  createPlaylist(name: String!): Playlist
}

Data Passing Between Tests

The response from one query or mutation can be passed to the arguments of another query or mutation. This is especially helpful when something is being created with a server-generated id and you want to be able to run a query for an item with that id. In the example below the createPlaylist mutation returns a Playlist fallPlaylist with an id. We can pass the id to the playlist query using a handlebars-like syntax.

The response data is stored in a variable using either the query name or the alias if one was used.

type Mutation {
  """
  Examples:
  fallPlaylist: createPlaylist(name: "Fall Mix")
  """
  createPlaylist(name: String!): Playlist
}

// fallPlaylist = Playlist (From the above Mutation)

type Query {
  """
  Examples:
  playlist(id: "{{fallPlaylist.id}}")
  """
  playlist(id: ID!): Playlist
}

Dependency Ordering

When passing data between tests the tests become dependent on each other and the tests that are being referenced by other tests often must be run first. For example, to run the playlist(id: "{{fallPlaylist.id}}") query which references the fallPlaylist: createPlaylist(name: "Fall Mix") the createPlaylist must be run before playlist. Rather than having to worry about what order the tests should be run the tool will do a dependency analysis to order them.

type Mutation {
  """
  Examples:
  fallPlaylist: createPlaylist(name: "Fall Mix")
  """
  createPlaylist(name: String!): Playlist
}

type Query {
  """
  Examples:
  playlist(id: "{{fallPlaylist.id}}")
  """
  playlist(id: ID!): Playlist
}

Define cleanup tests that run last

When tests contain mutations that remove or archive data it's often necessary to do these steps at the end so any other tests that depend on that data existing or being active are not broken. The @last() directive can be added to a query to ensure all the tests marked with the directive are run after all the other tests.

type Mutation {
  """
  Examples:
  removePlaylist(id: "{{fallPlaylist.id}}") @last()
  """
  removePlaylist(id: ID!): Playlist
}

Array Assertions

The tester attempts to increase test coverage by retrieving every field available to query. However, when the fields contain arrays which return empty the coverage has a hole in due to the missing data. You can use the @ensureMinimum directive to cause the tester to assert that arrays you name have at least N items.

type Query {
  """
  Examples:
  searchPlaylists(term: "Mix") @ensureMinimum(nItems: 2, inArrays:["searchPlaylists", "searchPlaylists.tracks"])
  """
  searchPlaylists(term: String!): [Playlist!]!
}

type Playlist {
  id: ID!
  name: String!
  tracks: [Track!]!
}

type Track {
  id: ID!
  title: String!
  artist: String!
  album: String!
}

Opt-Out of Certain Queries

When annotating, if you add +NOFOLLOW in examples will prevent this query from running.

type Query {
  """
  Examples:
  +NOFOLLOW
  ignoredQuery(name: "Ignore me")
  """
  ignoredQuery(name: String): String
}

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Project Link: https://github.com/chad-superhifi/graphql-service-tester.git