Cast the directive types

This commit is contained in:
Jackson Harper
2022-02-15 15:44:59 -08:00
parent 6d5554dd5f
commit c8abf87367
2 changed files with 16 additions and 10 deletions

View File

@ -110,7 +110,7 @@ const contextFunc: ContextFunction<ExpressContext, ResolverContext> = async ({
export function makeApolloServer(app: Express): ApolloServer {
let schema = makeExecutableSchema({
resolvers,
typeDefs
typeDefs,
})
schema = sanitizeDirectiveTransformer(schema)

View File

@ -5,25 +5,31 @@ import { SanitizedString } from './scalars'
export const sanitizeDirectiveTransformer = (schema: GraphQLSchema) => {
return mapSchema(schema, {
[MapperKind.FIELD]: (fieldConfig) => {
const sanitizeDirective = getDirective(
schema,
fieldConfig,
'sanitize'
)
const sanitizeDirective = getDirective(schema, fieldConfig, 'sanitize')
if (!sanitizeDirective || sanitizeDirective.length < 1) {
return fieldConfig
}
const maxLength = sanitizeDirective[0].maxLength
const allowedTags = sanitizeDirective[0].allowedTags
const maxLength = sanitizeDirective[0].maxLength as number | undefined
const allowedTags = sanitizeDirective[0].allowedTags as
| string[]
| undefined
if (fieldConfig.type instanceof GraphQLNonNull && fieldConfig.type.ofType instanceof GraphQLScalarType) {
if (
fieldConfig.type instanceof GraphQLNonNull &&
fieldConfig.type.ofType instanceof GraphQLScalarType
) {
fieldConfig.type = new GraphQLNonNull(
new SanitizedString(fieldConfig.type.ofType, allowedTags, maxLength)
)
} else if (fieldConfig.type instanceof GraphQLScalarType) {
fieldConfig.type = new SanitizedString(fieldConfig.type, allowedTags, maxLength)
fieldConfig.type = new SanitizedString(
fieldConfig.type,
allowedTags,
maxLength
)
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Not a scalar type: ${fieldConfig.type}`)
}
return fieldConfig