Skip to content

Commit

Permalink
feat(core): allow schema merging and remove unused injector context d…
Browse files Browse the repository at this point in the history
…ependency
  • Loading branch information
marcus-sa committed Jan 24, 2024
1 parent 29a6483 commit 6004f0e
Show file tree
Hide file tree
Showing 8 changed files with 3,131 additions and 8,909 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@deepkit-modules/nx-webpack-plugin": "^0.2.12",
"@deepkit/postgres": "1.0.1-alpha.113",
"@deepkit/type-compiler": "1.0.1-alpha.112",
"@graphql-tools/schema": "^10.0.2",
"@nx/devkit": "^16.8.0",
"@nx/esbuild": "16.7.4",
"@nx/eslint-plugin": "16.7.4",
Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"graphql-scalars": "1.22.2",
"@graphql-tools/schema": "10.0.2",
"tslib": "2.6.2"
},
"peerDependencies": {
Expand All @@ -19,8 +20,8 @@
"@deepkit/http": "^1.0.1-alpha.113",
"@deepkit/type": "^1.0.1-alpha.113",
"@deepkit/broker": "^1.0.1-alpha.113",
"rxjs": "^7.8.1",
"@deepkit/injector": "^1.0.1-alpha.113"
"@deepkit/injector": "^1.0.1-alpha.113",
"rxjs": "^7.8.1"
},
"publishConfig": {
"access": "public"
Expand Down
34 changes: 17 additions & 17 deletions packages/core/src/lib/decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { graphql } from './decorators';
import { buildSchema } from './schema-builder';
import { Resolvers } from './resolvers';
import { Context, GraphQLMiddleware, Parent } from './types';
import { Context, GraphQLContext, GraphQLMiddleware, Parent } from './types';

/*test('invalid return type for mutation', () => {
expect(() => {
Expand Down Expand Up @@ -75,11 +75,11 @@ test('middleware errors', async () => {
{ controller: TestResolver, module: injectorContext.rootModule },
]);

const schema = buildSchema(resolvers, injectorContext);
const schema = buildSchema(resolvers);

await executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `{ get }`,
});

Expand Down Expand Up @@ -120,11 +120,11 @@ test('resolver middleware is invoked', async () => {
{ controller: TestResolver, module: injectorContext.rootModule },
]);

const schema = buildSchema(resolvers, injectorContext);
const schema = buildSchema(resolvers);

await executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `{ get }`,
});

Expand Down Expand Up @@ -165,11 +165,11 @@ test('query middleware is invoked', async () => {
{ controller: TestResolver, module: injectorContext.rootModule },
]);

const schema = buildSchema(resolvers, injectorContext);
const schema = buildSchema(resolvers);

await executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `{ get }`,
});

Expand Down Expand Up @@ -210,11 +210,11 @@ test('mutation middleware is invoked', async () => {
{ controller: TestResolver, module: injectorContext.rootModule },
]);

const schema = buildSchema(resolvers, injectorContext);
const schema = buildSchema(resolvers);

await executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `mutation { create }`,
});

Expand Down Expand Up @@ -255,11 +255,11 @@ test('subscription middleware is invoked', async () => {
{ controller: TestResolver, module: injectorContext.rootModule },
]);

const schema = buildSchema(resolvers, injectorContext);
const schema = buildSchema(resolvers);

await executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `subscription { create }`,
});

Expand Down Expand Up @@ -298,7 +298,7 @@ test('mutation', async () => {
await expect(
executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `mutation { createUser(data: { username: "Test" }) { id } }`,
}),
).resolves.toMatchSnapshot();
Expand Down Expand Up @@ -329,7 +329,7 @@ test('query', async () => {
await expect(
executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `{ getUser(id: 1) { id } }`,
rootValue: {},
}),
Expand Down Expand Up @@ -364,7 +364,7 @@ test('mutation args validation', async () => {

await expect(
executeGraphQL({
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `mutation { createUser(data: { username: "Test" }) { username } }`,
schema,
}),
Expand Down Expand Up @@ -403,7 +403,7 @@ test.skip('Context', async () => {

await expect(
executeGraphQL({
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `
{
info {
Expand Down Expand Up @@ -468,7 +468,7 @@ describe('resolveField', () => {
await expect(
executeGraphQL({
schema,
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `
{
getUser(id: "9f617521-b9c2-4ab9-a339-3c551c799027") {
Expand Down Expand Up @@ -529,7 +529,7 @@ describe('resolveField', () => {

await expect(
executeGraphQL({
contextValue: { injectorContext },
contextValue: <GraphQLContext>{ injectorContext },
source: `
{
getUser(id: "398cdf36-e3ac-475c-90aa-c70f99add874") {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/lib/graphql-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class GraphQLServer {
async onServerMainBootstrapDone(): Promise<void> {
const schemaBuilder = new SchemaBuilder(
this.resolvers,
this.injectorContext,
);
const schema = schemaBuilder.build();
await this.driver.start(schema);
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/lib/schema-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReceiveType, resolveReceiveType, Type } from '@deepkit/type';
import { ClassType } from '@deepkit/core';
import { InjectorContext, InjectorModule } from '@deepkit/injector';
import { mergeSchemas } from '@graphql-tools/schema';
import {
GraphQLFieldConfigMap,
GraphQLNamedType,
Expand All @@ -17,29 +17,25 @@ import { GraphQLContext } from './types';
export interface SchemaBuilderOptions {
readonly inputTypes?: readonly Type[];
readonly outputTypes?: readonly Type[];
readonly schemas?: readonly GraphQLSchema[];
}

export function buildSchema(
resolvers: Resolvers,
injectorContext: InjectorContext = new InjectorContext(new InjectorModule()),
options?: SchemaBuilderOptions,
): GraphQLSchema {
return new SchemaBuilder(resolvers, injectorContext, options).build();
return new SchemaBuilder(resolvers, options).build();
}

export class SchemaBuilder {
private readonly inputTypes = new Set<Type>(this.options?.inputTypes);

private readonly outputTypes = new Set<Type>(this.options?.outputTypes);

private readonly typesBuilder = new TypesBuilder(
this.resolvers,
this.injectorContext,
);
private readonly typesBuilder = new TypesBuilder(this.resolvers,);

constructor(
private readonly resolvers: Resolvers,
private readonly injectorContext: InjectorContext,
private readonly options?: SchemaBuilderOptions,
) {}

Expand Down Expand Up @@ -194,11 +190,15 @@ export class SchemaBuilder {
const subscription = this.buildRootSubscriptionType();
const types = [...this.buildInputTypes(), ...this.buildOutputTypes()];

return new GraphQLSchema({
const schema = new GraphQLSchema({
query,
mutation,
subscription,
types,
});

if (!this.options?.schemas?.length) return schema;

return mergeSchemas({ schemas: [schema, ...this.options.schemas] });
}
}
5 changes: 2 additions & 3 deletions packages/core/src/lib/types-builder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLEnumType, GraphQLID, GraphQLUnionType } from 'graphql';
import { InjectorContext, InjectorModule } from '@deepkit/injector';
import { InjectorContext } from '@deepkit/injector';
import {
BrokerBus,
BrokerBusChannel,
Expand Down Expand Up @@ -49,7 +49,7 @@ import {

import { TypesBuilder } from './types-builder';
import { GraphQLContext, ID } from './types';
import { Resolver, Resolvers } from './resolvers';
import { Resolvers } from './resolvers';
import { isAsyncIterable } from './utils';
import { InvalidSubscriptionTypeError } from './errors';

Expand All @@ -59,7 +59,6 @@ describe('TypesBuilder', () => {
beforeEach(() => {
builder = new TypesBuilder(
new Resolvers([]),
new InjectorContext(new InjectorModule()),
);
});

Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/lib/types-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ export class TypesBuilder {

private readonly inputObjectTypes = new Map<string, GraphQLInputObjectType>();

constructor(
private readonly resolvers: Resolvers,
private readonly injectorContext: InjectorContext,
) {}
constructor(private readonly resolvers: Resolvers) {}

getScalarType(type: Type): GraphQLScalarType {
if (type.typeName === 'ID') return GraphQLID;
Expand Down
Loading

0 comments on commit 6004f0e

Please sign in to comment.