createApp is no longer async, linting

This commit is contained in:
Jackson Harper 2022-02-15 14:30:26 -08:00
parent fb224d60c4
commit 7e35281bd1
4 changed files with 117 additions and 1 deletions

View file

@ -20,7 +20,7 @@ import { makeExecutableSchema } from '@graphql-tools/schema'
import { applyMiddleware } from 'graphql-middleware'
import * as cookie from 'cookie'
import typeDefs from './schema'
// import { SanitizeDirective } from './directives'
import { sanitizeDirectiveTransformer } from './directives'
import { functionResolvers } from './resolvers/function_resolvers'
import ScalarResolvers from './scalars'
import * as Sentry from '@sentry/node'
@ -109,6 +109,7 @@ const contextFunc: ContextFunction<ExpressContext, ResolverContext> = async ({
export function makeApolloServer(app: Express): ApolloServer {
let schema = makeExecutableSchema({ typeDefs, resolvers })
schema = sanitizeDirectiveTransformer(schema)
const apollo = new ApolloServer({
schema: schema,
context: contextFunc,

View file

@ -23,3 +23,43 @@
// }
// }
// }
import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils'
import { GraphQLNonNull, GraphQLScalarType, GraphQLSchema } from 'graphql'
import { SanitizedString } from './scalars'
export const sanitizeDirectiveTransformer = (
schema: GraphQLSchema
): GraphQLSchema => {
return mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const deprecatedDirective = getDirective(
schema,
fieldConfig,
'sanitize'
)?.[0]
if (deprecatedDirective) {
// const { allowedTags, maxLength } = this.args
const allowedTags = undefined
const maxLength = undefined
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
)
} else {
throw new Error(`Not a scalar type: ${fieldConfig.type}`)
}
}
return fieldConfig
},
})
}

View file

@ -0,0 +1,69 @@
// import { createTestUser, deleteTestUser, getProfile, getUser } from '../db'
// import { graphqlRequest, request } from '../util'
// import { expect } from 'chai'
// import {
// LoginErrorCode,
// SignupErrorCode,
// UpdateUserErrorCode,
// UpdateUserProfileErrorCode,
// } from '../../src/generated/graphql'
// import { User } from '../../src/entity/user'
// import { hashPassword } from '../../src/utils/auth'
// import 'mocha'
// describe('Sanitize Directive', () => {
// const username = 'fake_user'
// const correctPassword = 'fakePassword'
// let authToken: string
// let user: User
// before(async () => {
// const hashedPassword = hashPassword(correctPassword)
// user = await createTestUser(username, '', hashedPassword)
// const res = await request
// .post('/local/debug/fake-user-login')
// .send({ fakeEmail: user.email })
// authToken = res.body.authToken
// })
// after(async () => {
// await deleteTestUser(username)
// })
// describe('Update user with a bio that is too long', () => {
// let bio = "".padStart(500, '*');
// let query: string
// beforeEach(() => {
// query = `
// mutation {
// updateUser(
// input: {
// name: "fakeUser"
// bio: "${bio}"
// }
// ) {
// ... on UpdateUserSuccess {
// user {
// id
// }
// }
// ... on UpdateUserError {
// errorCodes
// }
// }
// }
// `
// })
// it('responds with error code EMPTY_NAME', async () => {
// const response = await graphqlRequest(query, authToken)
// console.log('GOT RESPONSE', response)
// expect(response.body.data.updateUser.errorCodes).to.eql([
// UpdateUserErrorCode.EmptyName,
// ])
// })
// })
// })

View file

@ -1,10 +1,16 @@
import { createApp } from '../src/server'
import supertest from 'supertest'
import { v4 } from 'uuid'
import { corsConfig } from '../src/utils/corsConfig'
const { app, apollo } = createApp()
export const request = supertest(app)
before(async () => {
await apollo.start()
apollo.applyMiddleware({ app, path: '/api/graphql', cors: corsConfig })
})
export const graphqlRequest = (
query: string,
authToken?: string