EasyGraphQL / easygraphql-tester

Test GraphQL queries, mutations and schemas on an easy way! 🚀
https://easygraphql.com/docs/easygraphql-tester/overview
MIT License
313 stars 34 forks source link

How can I test my middlewares? #129

Open ngshiheng opened 4 years ago

ngshiheng commented 4 years ago

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',
         );