{"id":13516781,"url":"https://github.com/4Catalyzer/graphql-validation-complexity","last_synced_at":"2025-03-31T07:30:29.966Z","repository":{"id":22029363,"uuid":"94937931","full_name":"4Catalyzer/graphql-validation-complexity","owner":"4Catalyzer","description":"Query complexity validation for GraphQL.js","archived":false,"fork":false,"pushed_at":"2025-03-25T15:57:19.000Z","size":1114,"stargazers_count":343,"open_issues_count":19,"forks_count":24,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-26T18:03:23.803Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/4Catalyzer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-20T21:49:19.000Z","updated_at":"2025-01-14T14:35:45.000Z","dependencies_parsed_at":"2024-01-17T06:26:04.207Z","dependency_job_id":"2101a3cf-7a01-4d6b-810e-7464e4482bf7","html_url":"https://github.com/4Catalyzer/graphql-validation-complexity","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fgraphql-validation-complexity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fgraphql-validation-complexity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fgraphql-validation-complexity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fgraphql-validation-complexity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4Catalyzer","download_url":"https://codeload.github.com/4Catalyzer/graphql-validation-complexity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246372527,"owners_count":20766633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T05:01:25.792Z","updated_at":"2025-03-31T07:30:29.929Z","avatar_url":"https://github.com/4Catalyzer.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# graphql-validation-complexity [![Travis][build-badge]][build] [![npm][npm-badge]][npm]\n\nQuery complexity validation for GraphQL.js.\n\n[![Codecov][codecov-badge]][codecov]\n\n## Usage\n\n```js\nimport { createComplexityLimitRule } from 'graphql-validation-complexity';\n\nconst ComplexityLimitRule = createComplexityLimitRule(1000);\n// Then use this rule with validate() or other validation APIs.\n```\n\nFor example, with `express-graphql` or Apollo Server, pass the complexity limit rule to `validationRules`.\n\n```js\nconst graphqlMiddleware = graphqlHTTP({\n  schema,\n  validationRules: [createComplexityLimitRule(1000)],\n});\n\nconst apolloServer = new ApolloServer({\n  schema,\n  validationRules: [createComplexityLimitRule(1000)],\n});\n```\n\nYou can provide a configuration object with custom global costs for scalars and objects as `scalarCost` and `objectCost` respectively, and a custom cost factor for lists as `listFactor`.\n\n```js\nconst ComplexityLimitRule = createComplexityLimitRule(1000, {\n  scalarCost: 1,\n  objectCost: 10, // Default is 0.\n  listFactor: 20, // Default is 10.\n});\n```\n\nYou can also set custom costs and cost factors as field definition extensions with the `getCost` and `getCostFactor` callbacks.\n\n```js\nconst expensiveField = {\n  type: ExpensiveItem,\n  extensions: {\n    getCost: () =\u003e 50,\n  },\n};\n\nconst expensiveList = {\n  type: new GraphQLList(MyItem),\n  extensions: {\n    getCostFactor: () =\u003e 100,\n  },\n};\n```\n\nYou can also define these via field directives in the SDL.\n\n```graphql\ndirective @cost(value: Int) on FIELD_DEFINITION\ndirective @costFactor(value: Int) on FIELD_DEFINITION\n\ntype CustomCostItem {\n  expensiveField: ExpensiveItem @cost(value: 50)\n  expensiveList: [MyItem] @costFactor(value: 100)\n}\n```\n\nThe configuration object also supports an `onCost` callback for logging query costs and a `formatErrorMessage` callback for customizing error messages. `onCost` will be called for every query with its cost. `formatErrorMessage` will be called with the cost whenever a query exceeds the complexity limit, and should return a string containing the error message.\n\n```js\nconst ComplexityLimitRule = createComplexityLimitRule(1000, {\n  onCost: (cost) =\u003e {\n    console.log('query cost:', cost);\n  },\n  formatErrorMessage: (cost) =\u003e\n    `query with cost ${cost} exceeds complexity limit`,\n});\n```\n\nThe configuration object also supports a `createError` callback for creating a custom `GraphQLError`. `createError` will be called with the cost and the document node whenever an error occurs. `formatErrorMessage` will be ignored when `createError` is specified.\n\n```js\nconst ComplexityLimitRule = createComplexityLimitRule(1000, {\n  createError(cost, documentNode) {\n    const error = new GraphQLError('custom error', [documentNode]);\n    error.meta = { cost };\n    return error;\n  },\n});\n```\n\nBy default, the validation rule applies a custom, lower cost factor for lists of introspection types, to prevent introspection queries from having unreasonably high costs. You can adjust this by setting `introspectionListFactor` on the configuration object.\n\n```js\nconst ComplexityLimitRule = createComplexityLimitRule(1000, {\n  introspectionListFactor: 10, // Default is 2.\n});\n```\n\n[build-badge]: https://img.shields.io/travis/4Catalyzer/graphql-validation-complexity/master.svg\n[build]: https://travis-ci.org/4Catalyzer/graphql-validation-complexity\n[npm-badge]: https://img.shields.io/npm/v/graphql-validation-complexity.svg\n[npm]: https://www.npmjs.org/package/graphql-validation-complexity\n[codecov-badge]: https://img.shields.io/codecov/c/github/4Catalyzer/graphql-validation-complexity/master.svg\n[codecov]: https://codecov.io/gh/4Catalyzer/graphql-validation-complexity\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4Catalyzer%2Fgraphql-validation-complexity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4Catalyzer%2Fgraphql-validation-complexity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4Catalyzer%2Fgraphql-validation-complexity/lists"}