### Installing supertest-graphql with npm Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Command to install the supertest-graphql package using npm. This is the first step to use the library in your project. ```Shell npm install supertest-graphql ``` -------------------------------- ### Testing GraphQL Mutation with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Illustrates how to test a GraphQL mutation using the `.mutate()` method. Similar to queries, variables can be passed using `.variables()`. It shows an example of a `PetAnimal` mutation. ```TypeScript const { data } = await request(app) .mutate(gql` mutation PetAnimal($petId: ID!) { petAnimal(petId: $petId) { name petType } } `) .variables({petId: 'my-cat' }) ``` -------------------------------- ### Testing GraphQL Subscriptions with supertest-graphql WebSockets Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Demonstrates testing GraphQL subscriptions over WebSockets using `supertestWs`. It shows how to set up the server for WebSocket tests, subscribe to an event, wait for the next value, and assert the response. ```TypeScript import { supertestWs } from 'supertest-graphql' import gql from 'graphql-tag' // for websocket the server needs to be started and stopped manually beForeEach(() => server.listen(0, "localhost")) afterEach(() => server.close()) test('should get pets', async () => { const sub = await supertestWs(app) .subscribe(gql` subscription { newPetAdded { name petType } } `) // will wait or pop the next value const { data } = await sub.next().expectNoErrors() expect(data.newPetAdded.name).toEqual('Fifi') }) ``` -------------------------------- ### Using WebSocket Legacy Protocol with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Demonstrates how to explicitly set the WebSocket protocol to the legacy version using `.protocol()` and the `LEGACY_WEBSOCKET_PROTOCOL` constant. ```TypeScript import { supertestWs, LEGACY_WEBSOCKET_PROTOCOL } from 'supertest-graphql' const sub = await supertestWs(app) .protocol(LEGACY_WEBSOCKET_PROTOCOL) .subscribe(...) ``` -------------------------------- ### Adding Authentication to GraphQL Requests with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Shows how to add basic authentication credentials to a GraphQL request using the `.auth()` method provided by supertest. ```TypeScript const { data } = await request(app) .auth('username', 'password') .query(...) ``` -------------------------------- ### Testing GraphQL Query with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Demonstrates a basic test case using supertest-graphql to send a GraphQL query, assert no errors, and check the response data. It uses `request` to wrap the app and `gql` for parsing the query string. ```TypeScript import request from 'supertest-graphql' import gql from 'graphql-tag' test('should get pets', async () => { const { data } = await request(app) .query(gql` query { pets { name petType } } `) .expectNoErrors() expect(data.pets).toHaveLength(2) }) ``` -------------------------------- ### Adding Authentication to WebSocket Subscriptions with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Explains how to pass authentication parameters during the WebSocket connection handshake for subscriptions using the `.connectionParams()` method with `supertestWs`. ```TypeScript import { supertestWs } from 'supertest-graphql' const sub = await supertestWs(app) .connectionParams({ token: 'my token' }) .subscribe(...) ``` -------------------------------- ### Adding Authentication Headers to GraphQL Requests with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Demonstrates how to add custom headers, such as an Authorization token, to a GraphQL request using the standard supertest `.set()` method. ```TypeScript const { data } = await request(app) .set('authorization', 'my token') .query(...) ``` -------------------------------- ### Changing GraphQL Endpoint Path with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Shows how to specify a different endpoint path for GraphQL requests using the `.path()` method, overriding the default `/graphql`. ```TypeScript const { data } = await request(app) .path('/new-graphql') .query(...) ``` -------------------------------- ### Passing Variables to GraphQL Query with supertest-graphql Source: https://github.com/alexstrat/supertest-graphql/blob/master/README.md Shows how to pass variables to a parameterized GraphQL query using the `.variables()` method. The variables object keys should match the variable names defined in the query. ```TypeScript const { data } = await request(app) .query(gql` query GetPets($first: Int){ pets(first: $first) { name petType } } `) .variables({ first: 4 }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.