types.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { GraphQLRequestContext } from '@apollo/server';
  2. import { ComplexityEstimator } from 'graphql-query-complexity';
  3. /**
  4. * @description
  5. * Options that can be passed to the `.init()` static method of the HardenPlugin.
  6. *
  7. * @docsCategory core plugins/HardenPlugin
  8. */
  9. export interface HardenPluginOptions {
  10. /**
  11. * @description
  12. * Defines the maximum permitted complexity score of a query. The complexity score is based
  13. * on the number of fields being selected as well as other factors like whether there are nested
  14. * lists.
  15. *
  16. * A query which exceeds the maximum score will result in an error.
  17. *
  18. * @default 1000
  19. */
  20. maxQueryComplexity?: number;
  21. /**
  22. * @description
  23. * An array of custom estimator functions for calculating the complexity of a query. By default,
  24. * the plugin will use the {@link defaultVendureComplexityEstimator} which is specifically
  25. * tuned to accurately estimate Vendure queries.
  26. */
  27. queryComplexityEstimators?: ComplexityEstimator[];
  28. /**
  29. * @description
  30. * When set to `true`, the complexity score of each query will be logged at the Verbose
  31. * log level, and a breakdown of the calculation for each field will be logged at the Debug level.
  32. *
  33. * This is very useful for tuning your complexity scores.
  34. *
  35. * @default false
  36. */
  37. logComplexityScore?: boolean;
  38. /**
  39. * @description
  40. * This object allows you to tune the complexity weight of specific fields. For example,
  41. * if you have a custom `stockLocations` field defined on the `ProductVariant` type, and
  42. * you know that it is a particularly expensive operation to execute, you can increase
  43. * its complexity like this:
  44. *
  45. * @example
  46. * ```ts
  47. * HardenPlugin.init({
  48. * maxQueryComplexity: 650,
  49. * customComplexityFactors: {
  50. * 'ProductVariant.stockLocations': 10
  51. * }
  52. * }),
  53. * ```
  54. */
  55. customComplexityFactors?: {
  56. [path: string]: number;
  57. };
  58. /**
  59. * @description
  60. * Graphql-js will make suggestions about the names of fields if an invalid field name is provided.
  61. * This would allow an attacker to find out the available fields by brute force even if introspection
  62. * is disabled.
  63. *
  64. * Setting this option to `true` will prevent these suggestion error messages from being returned,
  65. * instead replacing the message with a generic "Invalid request" message.
  66. *
  67. * @default true
  68. */
  69. hideFieldSuggestions?: boolean;
  70. /**
  71. * @description
  72. * When set to `'prod'`, the plugin will disable dev-mode features of the GraphQL APIs:
  73. *
  74. * - introspection
  75. * - GraphQL playground
  76. *
  77. * @default 'prod'
  78. */
  79. apiMode?: 'dev' | 'prod';
  80. /**
  81. * @description
  82. * Allows you to skip the complexity check for certain requests.
  83. *
  84. * @example
  85. * ```ts
  86. * HardenPlugin.init({
  87. * skip: (context) => context.request.http.headers['x-storefront-ssr-auth'] === 'some-secret-token'
  88. * }),
  89. * ```
  90. */
  91. skip?: (context: GraphQLRequestContext<any>) => Promise<boolean> | boolean;
  92. }