-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typegraphql): update code to use typegraphql 2.0.0
BREAKING CHANGE: @tsed/typegraphql use new @tsed/apollo package and support Apollo v4
- Loading branch information
Showing
24 changed files
with
2,146 additions
and
807 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,61 @@ | ||
import {AlterApolloSettings, ApolloSettingsWithID} from "@tsed/apollo"; | ||
import {isClass, Type} from "@tsed/core"; | ||
import {Configuration, Inject, InjectorService, Module} from "@tsed/di"; | ||
import {TypeGraphQLSettings} from "./interfaces/interfaces.js"; | ||
import {TypeGraphQLService} from "./services/TypeGraphQLService.js"; | ||
import {buildSchema} from "type-graphql"; | ||
import {RESOLVERS_PROVIDERS} from "./constants/constants.js"; | ||
import "./interfaces/interfaces.js"; | ||
import {ContextMiddleware} from "./middlewares/ContextMiddleware.js"; | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
@Module() | ||
export class TypeGraphQLModule { | ||
@Inject() | ||
protected service: TypeGraphQLService; | ||
|
||
export class TypeGraphQLModule implements AlterApolloSettings { | ||
@Inject() | ||
protected injector: InjectorService; | ||
|
||
@Configuration() | ||
protected configuration: Configuration; | ||
|
||
get settings(): {[key: string]: TypeGraphQLSettings} | undefined { | ||
return this.configuration.get("graphql") || this.configuration.get("typegraphql"); | ||
} | ||
|
||
$onRoutesInit(): Promise<any> | void { | ||
const {settings} = this; | ||
if (settings) { | ||
const promises = Object.entries(settings).map(([key, options]) => { | ||
return this.service.createServer(key, options); | ||
}); | ||
|
||
return Promise.all(promises); | ||
} | ||
} | ||
|
||
$afterListen(): Promise<any> | void { | ||
const host = this.configuration.getBestHost(); | ||
async $alterApolloSettings(settings: ApolloSettingsWithID): Promise<ApolloSettingsWithID> { | ||
const {resolvers: initialResolvers = [], buildSchemaOptions = {}, ...serverOptions} = settings; | ||
|
||
const displayLog = (key: string, path: string) => { | ||
const url = typeof host.port === "number" ? `${host.protocol}://${host.address}:${host.port}` : ""; | ||
const resolvers: any = this.getResolvers(settings.id, [...(initialResolvers as any[]), ...(buildSchemaOptions.resolvers || [])]); | ||
|
||
this.injector.logger.info(`[${key}] GraphQL server is available on ${url}/${path.replace(/^\//, "")}`); | ||
}; | ||
serverOptions.schema = await buildSchema({ | ||
container: this.injector as never, | ||
...buildSchemaOptions, | ||
resolvers, | ||
globalMiddlewares: [ContextMiddleware, ...(buildSchemaOptions.globalMiddlewares || [])] | ||
}); | ||
|
||
const {settings} = this; | ||
return serverOptions; | ||
} | ||
|
||
if (settings) { | ||
Object.entries(settings).map(([key, options]) => { | ||
const {path} = options; | ||
protected getResolvers(id: string, resolvers: Type<any>[]): Type<any>[] { | ||
const globalResolvers = this.injector | ||
.getProviders(RESOLVERS_PROVIDERS) | ||
.filter((provider) => { | ||
const opts = provider.store.get("graphql"); | ||
|
||
displayLog(key, path); | ||
return !opts?.id || opts?.id === id; | ||
}) | ||
.map((provider) => { | ||
return provider.useClass; | ||
}); | ||
} | ||
|
||
return resolvers | ||
.map((resolver) => { | ||
if (!(this.injector.has(resolver) || !isClass(resolver))) { | ||
this.injector | ||
.addProvider(resolver, { | ||
useClass: resolver | ||
}) | ||
.invoke(resolver); | ||
} | ||
|
||
return resolver; | ||
}) | ||
.concat(globalResolvers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
packages/graphql/typegraphql/src/interfaces/TypeGraphQLSettings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1 @@ | ||
import {ApolloConfig, ApolloSettings} from "@tsed/apollo"; | ||
import {BuildSchemaOptions} from "type-graphql"; | ||
|
||
export interface TypeGraphQLSettings extends ApolloSettings { | ||
// TypeGraphQL options | ||
buildSchemaOptions?: Partial<BuildSchemaOptions>; | ||
// apollo-server-express options | ||
// See options descriptions on https://www.apollographql.com/docs/apollo-server/api/apollo-server.html | ||
serverConfig?: ApolloConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import {TypeGraphQLSettings} from "./TypeGraphQLSettings.js"; | ||
import {BuildSchemaOptions} from "type-graphql"; | ||
import type {ApolloSettings as AS} from "@tsed/apollo"; | ||
|
||
declare global { | ||
namespace TsED { | ||
interface ApolloSettings { | ||
buildSchemaOptions?: Partial<BuildSchemaOptions>; | ||
} | ||
|
||
interface Configuration { | ||
graphql: {[key: string]: TypeGraphQLSettings}; | ||
typegraphql: {[key: string]: TypeGraphQLSettings}; | ||
/** | ||
* @deprecated Use apollo instead | ||
*/ | ||
graphql: {[key: string]: AS}; | ||
/** | ||
* @deprecated Use apollo instead | ||
*/ | ||
typegraphql: {[key: string]: AS}; | ||
} | ||
} | ||
} | ||
|
||
export * from "./TypeGraphQLSettings.js"; |
77 changes: 0 additions & 77 deletions
77
packages/graphql/typegraphql/src/services/TypeGraphQLService.spec.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.