question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access to query variables in validationRules

See original GitHub issue

My 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:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

10reactions
MichalLytekcommented, Jul 10, 2019

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

1reaction
martijnwalravencommented, Jul 9, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Restrict data input by using validation rules - Microsoft Support
Validations rules help you check data as it is added to your Access desktop database which improves accuracy and consistency of data entry....
Read more >
Validation Rules - Hot Chocolate v10 - ChilliCream
In query validation rules you can access the query and the schema but the argument values and the variable values are not yet...
Read more >
validation rule based on a query | Access World Forums
My problem is - I wish to have some validation rules based on the min and max values of each parameters (so that...
Read more >
GraphQL validation using directives
For example, you can ensure that an argument is a particular scalar ... a @constraint directive to decorate an SDL with validation rules....
Read more >
Validation Rules for "Validation Rules" Settings
From the Select drop-down menu in the Validation Rules setting, select Accepted. The Accepted validation rule has no parameters.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found