-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use it with Apollo Server? #7
Comments
It's definitely possible. I just briefly looked into the apollo-server-express code. You can create the validation rule similar to the example give in the README for the use with express-graphql. (Not tested): import { graphqlExpress } from 'apollo-server-express';
const myGraphQLSchema = // ... define or import your schema here!
const PORT = 3000;
const app = express();
// bodyParser is needed just for POST.
app.use('/graphql', bodyParser.json(), graphqlExpress((req, res) => {
// Extract variables from req object here to pass to validation rule
return {
schema: myGraphQLSchema,
validationRules: [
// Add queryComplexity configuration here
]
}
})); If you have a full working version, a PR to the README would be appreciated. |
@ivome Can we somehow use |
@19majkel94 Unfortunately this does not work as far as I can tell. This only returns the definitions of the variable nodes, not the actual values. The problem is that the variable values are not part of the ValidationContext in graphql-js. I can only think of two options:
I haven't looked at the source code of ApolloServer 2 in detail, but nr 2 is probably the best option as this also opens up the possibility to write other dynamic validation rules (restrict parts of the schema for certain IP blocks / user agents etc.) Or any other ideas? |
I've looked at it and they just pass the array of validators to I will open an issue on apollo-server repo for that 😉 |
I just published version v0.3.0 of this library which adds a helper function https://github.com/slicknode/graphql-query-complexity#calculate-query-complexity |
How to use validation for graphql-upload multipart request?
|
@ivome |
For anyone who comes across this, looking to define cost complexity using the Unfortunately my example relies what I believe is unsupported/undocumented behaviour in |
I'm going to close this here as this can be solved outside of this library like mentioned above: https://github.com/MichalLytek/type-graphql/blob/4501867fffe3e6f5b3e71af0b71651efcd48d9c3/examples/query-complexity/index.ts#L16-L64 If someone wants to create an npm package with an apollo server plugin, I'm happy to accept a PR with a link in the README. |
Small refactor of @MichalLytek's file that made it into a plugin https://gist.github.com/reconbot/37ccd8b8fcdfb78ff6a16a7055edf103 |
And since you asked https://www.npmjs.com/package/graphql-query-complexity-apollo-plugin |
First of all, I'm sorry to continue a closed question here But I'm really in pain I looked up how graphql-query-complexity is used // Nest
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
cors: corsOptions,
path: gqlPath,
playground: !isPrd,
plugins: [{
// How to get a schema => https://github.com/MichalLytek/type-graphql/blob/4501867fffe3e6f5b3e71af0b71651efcd48d9c3/examples/query-complexity/index.ts#L30
}],
cache: 'bounded', // 解决生产报错 Persisted queries are enabled and are using an unbounded cache. Your server is vulnerable to denial of service attacks via memory exhaustion. Set `cache: "bounded"` or `persistedQueries: false` in your ApolloServer constructor, or see https://go.apollo.dev/s/cache-backends for other alternatives.
autoSchemaFile: path.resolve(__dirname, 'schema/index.gql'), // true 在内存中
context(ctx) {
return ctx;
},
}), |
After much effort I finally found a working example:nestjs/graphql#373 |
Is it possible to use it with Apollo Server? More precisely with apollo-server-express?
If yes, could you please add the sample code to
README.md
?The text was updated successfully, but these errors were encountered: