Access to query variables in validationRules
See original GitHub issueMy feature request is partially related to #1627. Generally, I want to integrate with a graphql-query-complexity
library that creates validationRules
but it need access to the query variables values and they’re not present in ValidationContext
in graphql-js
. Here’s how it’s configured using plain express-graphql
middleware:
import queryComplexity from 'graphql-query-complexity';
import express from 'express';
import graphqlHTTP from 'express-graphql';
import schema from './schema';
const app = express();
app.use('/api', graphqlHTTP(async (request, response, {variables}) => ({
schema,
validationRules: [ queryComplexity({
maximumComplexity: 1000,
variables,
onComplete: (complexity: number) => {console.log('Query Complexity:', complexity);},
}) ]
})));
In graphql-yoga
there is a callback, like in apollo-server for context
:
https://github.com/prisma/graphql-yoga/blob/3362b13374190fd2390594495dc08064e5ee4815/src/types.ts#L64-L77
And I think that apollo-server
should support this kind of configuration too 😉 Changing ValidationContext
mechanism in graphql-js
is much more complicated and the only way that the author has figured out is by making validationRules
dynamic in Apollo Server:
https://github.com/slicknode/graphql-query-complexity/issues/7#issuecomment-427435851
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:14 (7 by maintainers)
Top GitHub Comments
Here is a working example of
graphql-query-complexity
integration with Apollo Server 💪 https://github.com/19majkel94/type-graphql/blob/4501867fffe3e6f5b3e71af0b71651efcd48d9c3/examples/query-complexity/index.ts#L16-L64@19majkel94 Complexity calculation is a fine solution, but using the
didResolveOperation
hook from the request pipeline plugin API would be a better place for this because it gets called for every request and has access both to the specific operation that gets executed and the query variables.