fix some bug and produce many problem to solve later
This commit is contained in:
@@ -22,6 +22,9 @@ import SchemaBuilder from '@pothos/core'
|
||||
import SimpleObjectPlugin from '@pothos/plugin-simple-objects'
|
||||
import { User } from '@prisma/client'
|
||||
import { getDatamodel } from '../types/pothos.generated'
|
||||
import { DateTime } from 'luxon'
|
||||
import { Kind } from 'graphql'
|
||||
import { DateTimeUtils } from '../common/utils/datetime.utils'
|
||||
|
||||
// import { rules } from '../common/graphql/common.graphql.auth-rule';
|
||||
|
||||
@@ -29,6 +32,7 @@ export type SchemaContext =
|
||||
| {
|
||||
isSubscription: true
|
||||
websocket: {
|
||||
req: Request
|
||||
pubSub: PubSub
|
||||
me: User
|
||||
generator: PrismaCrudGenerator<BuilderTypes>
|
||||
@@ -57,8 +61,8 @@ export interface SchemaBuilderOption {
|
||||
// AuthZRule: keyof typeof rules;
|
||||
Scalars: {
|
||||
DateTime: {
|
||||
Input: Date
|
||||
Output: Date
|
||||
Input: string | DateTime | Date
|
||||
Output: string | DateTime | Date
|
||||
}
|
||||
Json: {
|
||||
Input: JSON
|
||||
@@ -112,7 +116,36 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
|
||||
},
|
||||
})
|
||||
this.generator = new PrismaCrudGenerator<BuilderTypes>(this)
|
||||
this.addScalarType('DateTime', DateTimeResolver)
|
||||
this.scalarType('DateTime', {
|
||||
serialize: (value) => {
|
||||
// Serialize outgoing DateTime to ISO string
|
||||
if (typeof value === 'string') {
|
||||
return value
|
||||
}
|
||||
if (typeof value === 'object' && value !== null && 'toISO' in value) {
|
||||
return value
|
||||
}
|
||||
// if value = Date, convert to DateTime
|
||||
if (value instanceof Date) {
|
||||
return DateTimeUtils.toIsoString(DateTimeUtils.fromDate(value))
|
||||
}
|
||||
throw new Error('Invalid DateTime')
|
||||
},
|
||||
parseValue: (value) => {
|
||||
// Parse incoming ISO string to Luxon DateTime
|
||||
if (typeof value === 'string') {
|
||||
return DateTimeUtils.fromIsoString(value)
|
||||
}
|
||||
throw new Error('Invalid DateTime')
|
||||
},
|
||||
parseLiteral: (ast) => {
|
||||
// parse string to DateTime
|
||||
if (ast.kind === Kind.STRING) {
|
||||
return DateTimeUtils.fromIsoString(ast.value)
|
||||
}
|
||||
throw new Error('Invalid DateTime')
|
||||
},
|
||||
})
|
||||
this.addScalarType('Json', JSONObjectResolver)
|
||||
this.addScalarType('Upload', GraphQLUpload)
|
||||
|
||||
|
||||
@@ -39,6 +39,8 @@ import { WorkshopModule } from '../Workshop/workshop.module'
|
||||
import { WorkshopOrganizationModule } from '../WorkshopOrganization/workshoporganization.module'
|
||||
import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module'
|
||||
import { initContextCache } from '@pothos/core'
|
||||
import { PubSub } from 'graphql-subscriptions'
|
||||
import { isSubscription } from 'rxjs/internal/Subscription'
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
@@ -81,8 +83,8 @@ import { initContextCache } from '@pothos/core'
|
||||
}),
|
||||
GraphQLModule.forRootAsync<ApolloDriverConfig>({
|
||||
driver: PothosApolloDriver,
|
||||
inject: [GraphqlService],
|
||||
useFactory: async (graphqlService: GraphqlService) => ({
|
||||
inject: [GraphqlService, 'PUB_SUB'],
|
||||
useFactory: async (graphqlService: GraphqlService, pubsub: PubSub) => ({
|
||||
path: process.env.API_PATH + '/graphql',
|
||||
debug: process.env.NODE_ENV === 'development' || false,
|
||||
playground: process.env.NODE_ENV === 'development' || false,
|
||||
@@ -91,18 +93,36 @@ import { initContextCache } from '@pothos/core'
|
||||
subscriptions: {
|
||||
'graphql-ws': true,
|
||||
},
|
||||
context: async ({ req }: { req: Request }) => ({
|
||||
...initContextCache(),
|
||||
isSubscription: false,
|
||||
http: {
|
||||
req,
|
||||
me: await graphqlService.acquireContext(req),
|
||||
invalidateCache: () =>
|
||||
graphqlService.invalidateCache(
|
||||
req.headers['x-session-id'] as string,
|
||||
),
|
||||
},
|
||||
}),
|
||||
context: async ({
|
||||
req,
|
||||
subscriptions,
|
||||
extra,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
}: { req?: Request; subscriptions?: any; extra?: any }) => {
|
||||
if (subscriptions) {
|
||||
return {
|
||||
isSubscription: true,
|
||||
websocket: {
|
||||
req: extra.request,
|
||||
pubSub: pubsub,
|
||||
me: await graphqlService.acquireContext(
|
||||
extra.request.headers['x-session-id'],
|
||||
),
|
||||
generator: extra.schemaBuilder,
|
||||
},
|
||||
}
|
||||
}
|
||||
return {
|
||||
isSubscription: false,
|
||||
http: {
|
||||
req,
|
||||
me: req ? await graphqlService.acquireContext(req) : null,
|
||||
pubSub: pubsub,
|
||||
invalidateCache: () => Promise.resolve(),
|
||||
generator: extra.schemaBuilder,
|
||||
},
|
||||
}
|
||||
},
|
||||
}),
|
||||
}),
|
||||
],
|
||||
@@ -124,7 +144,17 @@ import { initContextCache } from '@pothos/core'
|
||||
useFactory: (builder: Builder) => new PrismaCrudGenerator(builder),
|
||||
inject: [Builder],
|
||||
},
|
||||
{
|
||||
provide: 'PUB_SUB',
|
||||
useFactory: () => new PubSub(),
|
||||
},
|
||||
],
|
||||
exports: [
|
||||
Builder,
|
||||
PrismaCrudGenerator,
|
||||
GraphqlService,
|
||||
RedisService,
|
||||
'PUB_SUB',
|
||||
],
|
||||
exports: [Builder, PrismaCrudGenerator, GraphqlService, RedisService],
|
||||
})
|
||||
export class GraphqlModule {}
|
||||
|
||||
@@ -24,12 +24,8 @@ export class GraphqlService {
|
||||
const disableAuth = process.env.DISABLE_AUTH === 'true'
|
||||
try {
|
||||
sessionId = req.headers['x-session-id'] as string
|
||||
} catch (error) {
|
||||
Logger.error('Error acquiring context', error)
|
||||
if (disableAuth) {
|
||||
return null
|
||||
}
|
||||
throw new UnauthorizedException('Must provide a session ID')
|
||||
} catch (_error) {
|
||||
return null
|
||||
}
|
||||
if (disableAuth) {
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user