-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Root Level Limit: different take with onValidate
- Loading branch information
Showing
4 changed files
with
296 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,47 @@ | ||
import { createGraphQLError } from '@graphql-tools/utils'; | ||
import { createGraphQLError, getRootTypes } from '@graphql-tools/utils'; | ||
import { Plugin } from '@envelop/core'; | ||
import type { ValidationRule } from 'graphql/validation/ValidationContext'; | ||
import { isObjectType } from 'graphql'; | ||
|
||
interface GraphQLParams { | ||
operationName?: string; | ||
query?: string; | ||
export interface RootLevelQueryLimitOptions { | ||
maxRootLevelFields: number; | ||
} | ||
|
||
export function rootLevelQueryLimit({ maxRootLevelFields }: { maxRootLevelFields: number }) { | ||
return { | ||
onParams({ params }: unknown) { | ||
const { query, operationName } = params as GraphQLParams; | ||
|
||
if (operationName?.includes('IntrospectionQuery')) return true; | ||
|
||
const newQuery = formatQuery(query || ''); | ||
const linesArray = newQuery.split('\n'); | ||
|
||
let countLeadingSpacesTwo = 0; | ||
|
||
for (const line of linesArray) { | ||
const leadingSpaces = line?.match(/^\s*/)?.[0]?.length || 0; | ||
|
||
if (leadingSpaces === 4 && line[leadingSpaces] !== ')') { | ||
countLeadingSpacesTwo++; | ||
|
||
if (countLeadingSpacesTwo > maxRootLevelFields * 2) { | ||
export function createRootLevelQueryLimitRule(opts: RootLevelQueryLimitOptions): ValidationRule { | ||
const { maxRootLevelFields } = opts; | ||
|
||
return function rootLevelQueryLimitRule (context) { | ||
const rootTypes = getRootTypes(context.getSchema()); | ||
let rootFieldCount = 0; | ||
return { | ||
Field() { | ||
const parentType = context.getParentType(); | ||
if (isObjectType(parentType) && rootTypes.has(parentType)) { | ||
rootFieldCount++; | ||
if (rootFieldCount > maxRootLevelFields) { | ||
throw createGraphQLError('Query is too complex.', { | ||
extensions: { | ||
http: { | ||
spec: false, | ||
status: 400, | ||
headers: { | ||
Allow: 'POST', | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
}, | ||
}; | ||
}; | ||
} | ||
|
||
function formatQuery(queryString: string) { | ||
queryString = queryString.replace(/^\s+/gm, ''); | ||
|
||
let indentLevel = 0; | ||
let formattedString = ''; | ||
|
||
for (let i = 0; i < queryString.length; i++) { | ||
const char = queryString[i]; | ||
|
||
if (char === '{' || char === '(') { | ||
formattedString += char; | ||
indentLevel++; | ||
// formattedString += ' '.repeat(indentLevel * 4); | ||
} else if (char === '}' || char === ')') { | ||
indentLevel--; | ||
|
||
if (formattedString[formattedString.length - 1] !== '\n') | ||
formattedString = formattedString.trim().replace(/\n$/, ''); | ||
|
||
if (char === ')') formattedString += char; | ||
} | ||
|
||
if (char === '}') formattedString += '\n' + ' '.repeat(indentLevel * 4) + char; | ||
} else if (char === '\n') { | ||
if (queryString[i + 1] !== '\n' && queryString[i + 1] !== undefined) { | ||
formattedString += char + ' '.repeat(indentLevel * 4); | ||
} | ||
} else { | ||
formattedString += char; | ||
export function rootLevelQueryLimit(opts: RootLevelQueryLimitOptions): Plugin { | ||
const rootLevelQueryLimitRule = createRootLevelQueryLimitRule(opts); | ||
return { | ||
onValidate({ addValidationRule }) { | ||
addValidationRule( | ||
rootLevelQueryLimitRule | ||
) | ||
} | ||
} | ||
|
||
return formattedString; | ||
} |
Oops, something went wrong.