117 lines
3.3 KiB
TypeScript
117 lines
3.3 KiB
TypeScript
import { DateTimeResolver, JSONObjectResolver } from 'graphql-scalars';
|
|
import PrismaPlugin, {
|
|
PothosPrismaDatamodel,
|
|
PrismaClient,
|
|
} from '@pothos/plugin-prisma';
|
|
import { Request, Response } from 'express';
|
|
import SmartSubscriptionPlugin, {
|
|
subscribeOptionsFromIterator,
|
|
} from '@pothos/plugin-smart-subscriptions';
|
|
|
|
import AuthzPlugin from '@pothos/plugin-authz';
|
|
import ErrorsPlugin from '@pothos/plugin-errors';
|
|
import type { FileUpload } from 'graphql-upload/processRequest.js';
|
|
import GraphQLUpload from 'graphql-upload/GraphQLUpload.js';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { PrismaCrudGenerator } from './graphql.generator';
|
|
import type PrismaTypes from '../types/pothos.generated';
|
|
import PrismaUtils from '@pothos/plugin-prisma-utils';
|
|
import { PubSub } from 'graphql-subscriptions';
|
|
import RelayPlugin from '@pothos/plugin-relay';
|
|
import SchemaBuilder from '@pothos/core';
|
|
import SimpleObjectPlugin from '@pothos/plugin-simple-objects';
|
|
import { User } from '@prisma/client';
|
|
import { getDatamodel } from '../types/pothos.generated';
|
|
|
|
// import { rules } from '../common/graphql/common.graphql.auth-rule';
|
|
|
|
export interface SchemaContext {
|
|
req: Request;
|
|
res: Response;
|
|
me: User;
|
|
pubSub: PubSub;
|
|
generator: PrismaCrudGenerator<BuilderTypes>;
|
|
}
|
|
|
|
export interface SchemaBuilderOption {
|
|
Context: SchemaContext;
|
|
PrismaTypes: PrismaTypes;
|
|
DataModel: PothosPrismaDatamodel;
|
|
Connection: {
|
|
totalCount: number | (() => number | Promise<number>);
|
|
};
|
|
// AuthZRule: keyof typeof rules;
|
|
Scalars: {
|
|
DateTime: {
|
|
Input: Date;
|
|
Output: Date;
|
|
};
|
|
Json: {
|
|
Input: JSON;
|
|
Output: JSON;
|
|
};
|
|
Upload: {
|
|
Input: FileUpload;
|
|
Output: FileUpload;
|
|
};
|
|
};
|
|
}
|
|
|
|
@Injectable()
|
|
export class Builder extends SchemaBuilder<SchemaBuilderOption> {
|
|
public generator: PrismaCrudGenerator<BuilderTypes>;
|
|
|
|
constructor(private readonly prisma: PrismaClient) {
|
|
super({
|
|
plugins: [
|
|
PrismaPlugin,
|
|
PrismaUtils,
|
|
SimpleObjectPlugin,
|
|
SmartSubscriptionPlugin,
|
|
RelayPlugin,
|
|
ErrorsPlugin,
|
|
AuthzPlugin,
|
|
],
|
|
smartSubscriptions: {
|
|
debounceDelay: 1000,
|
|
...subscribeOptionsFromIterator((name, { pubSub }) => {
|
|
return pubSub.asyncIterator(name);
|
|
}),
|
|
},
|
|
relay: {},
|
|
prisma: {
|
|
client: prisma,
|
|
exposeDescriptions: true,
|
|
filterConnectionTotalCount: true,
|
|
onUnusedQuery: process.env.NODE_ENV === 'production' ? null : 'warn',
|
|
dmmf: getDatamodel(),
|
|
},
|
|
errors: {
|
|
defaultTypes: [],
|
|
},
|
|
});
|
|
this.generator = new PrismaCrudGenerator<BuilderTypes>(this);
|
|
this.addScalarType('DateTime', DateTimeResolver);
|
|
this.addScalarType('Json', JSONObjectResolver);
|
|
this.addScalarType('Upload', GraphQLUpload);
|
|
|
|
this.queryType({});
|
|
this.mutationType({});
|
|
this.subscriptionType({});
|
|
|
|
this.globalConnectionField('totalCount', (t) =>
|
|
t.int({
|
|
nullable: true,
|
|
resolve: (parent) =>
|
|
typeof parent.totalCount === 'function'
|
|
? parent.totalCount()
|
|
: parent.totalCount,
|
|
}),
|
|
);
|
|
|
|
// test print ManagedServiceWhereUniqueInput
|
|
}
|
|
}
|
|
export type BuilderTypes =
|
|
PothosSchemaTypes.ExtendDefaultTypes<SchemaBuilderOption>;
|