graphql-shield icon indicating copy to clipboard operation
graphql-shield copied to clipboard

Initializing shield adding significant overhead even with empty middleware

Open OdedNir opened this issue 11 months ago • 0 comments

Bug report

  • [x] I have checked other issues to make sure this is not a duplicate.

Describe the bug

Even when adding empty middleware to the applyMiddleware function from graphql-middleware, it adds significant overhead to each request.

The example below shows a case where a query returns a hard-coded large dataset to neutralize the impact of the shield middlewares.

To Reproduce

Steps to reproduce the behavior:

  1. Initializing the following shield middlewares:

    const A = shield({});
    const B = shield({});
    const C = shield({});
    const D = shield({});
    const E = shield({});
    const F = shield({});
    const G = shield({});
    
  2. Creating the following Apollo Server (even with a tiny schema):

    const createApolloServer = () => {
      let schema = makeExecutableSchema({
        typeDefs: gql`
          type Car {
            id: ID!
            name: String!
            model: String!
          }
    
          type Query {
            cars: [Car]!
          }
        `,
        resolvers: {
          Query: {
            cars: () => [
              // Returning mock data just for the sake of this example
              { id: 1, name: "1", model: "A" },
              { id: 2, name: "2", model: "B" },
              // ...
              { id: 800, name: "800", model: "A" },
            ],
          },
        },
      });
    
      schema = applyMiddleware(schema, A, B, C, D, E, F, G);
    
      const apolloServer = new ApolloServer({
        schema,
        dataSources,
        context,
        formatError,
      });
    
      return apolloServer;
    };
    
  3. Run a query:

    query Cars {
      cars {
        id
        name
        model
      }
    }
    

Expected behavior

Adding empty middleware should not significantly impact the request handling time.

Actual behavior

Adding empty middleware results in noticeable overhead in request processing time.

Additional context

This issue affects the performance of the graphql-shield package and could impact applications using it for permission handling.

OdedNir avatar Mar 20 '24 12:03 OdedNir