Skip to content

How can I test my middlewares? #129

Open

Description

@ngshiheng

Can you show me an example of how can i test my middlewares?
I want to test that my queries can only be accessed if the correct jwt token is passed through the request Authorization header. I'm using graphql-yoga middleware. Here's an example of my graphql server:

const server = new GraphQLServer({
    typeDefs: './src/schemas/schema.graphql',
    resolvers,
    middlewares: [permissions],   // <-- here's where I pass in my middleware
    context: request => {
        return {
            ...request,
            prisma,
        };
    },
});

Here's what I intend to do, e.g.:

test('only authorized user can see this', async () => {
        const query = `
             query user($id: ID!){
                user(id: $id) {
                    id
                    email
                    name
                }
            }
        `;
        const args = {
            id: 'foo-bar-id',
        };
        const result = await tester.graphql(
            query,
            {},
            { prisma },
            args,
        );
         expect(result.errors[0].message).to.be.eq(
             'Error!! Unauthorized request',
         );

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions