Cast the directive types
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user