Set search query max size to 255 char

This commit is contained in:
Hongbo Wu 2023-04-05 12:20:52 +08:00
parent e5ffd3f5bd
commit c596e88f46
3 changed files with 41 additions and 3 deletions

View file

@ -46,5 +46,43 @@ export const sanitizeDirectiveTransformer = (schema: GraphQLSchema) => {
}
return fieldConfig
},
[MapperKind.ARGUMENT]: (argConfig) => {
const sanitizeDirective = getDirective(schema, argConfig, 'sanitize')?.[0]
if (!sanitizeDirective) {
return argConfig
}
const maxLength = sanitizeDirective.maxLength as number | undefined
const minLength = sanitizeDirective.minLength as number | undefined
const allowedTags = sanitizeDirective.allowedTags as string[] | undefined
const pattern = sanitizeDirective.pattern as string | undefined
if (
argConfig.type instanceof GraphQLNonNull &&
argConfig.type.ofType instanceof GraphQLScalarType
) {
argConfig.type = new GraphQLNonNull(
new SanitizedString(
argConfig.type.ofType,
allowedTags,
maxLength,
minLength,
pattern
)
)
} else if (argConfig.type instanceof GraphQLScalarType) {
argConfig.type = new SanitizedString(
argConfig.type,
allowedTags,
maxLength,
minLength,
pattern
)
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Not a scalar type: ${argConfig.type}`)
}
return argConfig
},
})
}

View file

@ -1,4 +1,4 @@
directive @sanitize(allowedTags: [String], maxLength: Int, minLength: Int, pattern: String) on INPUT_FIELD_DEFINITION
directive @sanitize(allowedTags: [String], maxLength: Int, minLength: Int, pattern: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
type AddPopularReadError {
errorCodes: [AddPopularReadErrorCode!]!

View file

@ -10,7 +10,7 @@ const schema = gql`
maxLength: Int
minLength: Int
pattern: String
) on INPUT_FIELD_DEFINITION
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
enum SortOrder {
ASCENDING
@ -2542,7 +2542,7 @@ const schema = gql`
search(
after: String
first: Int
query: String
query: String @sanitize(maxLength: 255)
includeContent: Boolean
format: String
): SearchResult!