Skip to content

Commit

Permalink
feat(apollo,yoga,core): use graphql child injector context as graphql…
Browse files Browse the repository at this point in the history
… context
  • Loading branch information
marcus-sa committed Jan 23, 2024
1 parent b572b73 commit 518f17f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/apollo/src/lib/apollo-graphql-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InjectorContext } from '@deepkit/injector';
import { GraphQLSchema } from 'graphql';
import { WebWorkerFactory, ApplicationServer } from '@deepkit/framework';
import { HttpBadRequestError, httpWorkflow } from '@deepkit/http';
import { Driver } from '@deepkit-graphql/core';
import { Driver, GraphQLContext } from '@deepkit-graphql/core';

import { ApolloGraphQLConfig } from './apollo-graphql-config';
import { ApolloServerPlugins } from './plugins';
Expand Down Expand Up @@ -53,7 +53,7 @@ export class ApolloDriver extends Driver {

const response = await this.server!.executeHTTPGraphQLRequest({
httpGraphQLRequest,
context: async () => injectorContext,
context: async (): Promise<GraphQLContext> => ({ injectorContext }),
});
if (!response) {
throw new HttpBadRequestError(JSON.stringify(httpGraphQLRequest));
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from './lib/decorators';
export * from './lib/types-builder';
export * from './lib/schema-builder';
export * from './lib/graphql.module';
export * from './lib/graphql-http-context';
export * from './lib/driver';
export * from './lib/types';
export * from './lib/resolvers';
22 changes: 11 additions & 11 deletions packages/core/src/lib/decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test('middleware errors', async () => {

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

Expand Down Expand Up @@ -124,7 +124,7 @@ test('resolver middleware is invoked', async () => {

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

Expand Down Expand Up @@ -169,7 +169,7 @@ test('query middleware is invoked', async () => {

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

Expand Down Expand Up @@ -214,7 +214,7 @@ test('mutation middleware is invoked', async () => {

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

Expand Down Expand Up @@ -259,7 +259,7 @@ test('subscription middleware is invoked', async () => {

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

Expand Down Expand Up @@ -298,7 +298,7 @@ test('mutation', async () => {
await expect(
executeGraphQL({
schema,
contextValue: injectorContext,
contextValue: { 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: { 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: { 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: { injectorContext },
source: `
{
info {
Expand Down Expand Up @@ -468,7 +468,7 @@ describe('resolveField', () => {
await expect(
executeGraphQL({
schema,
contextValue: injectorContext,
contextValue: { 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: { injectorContext },
source: `
{
getUser(id: "398cdf36-e3ac-475c-90aa-c70f99add874") {
Expand Down
8 changes: 0 additions & 8 deletions packages/core/src/lib/graphql-http-context.ts

This file was deleted.

13 changes: 7 additions & 6 deletions packages/core/src/lib/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { Resolvers } from './resolvers';
import { TypesBuilder } from './types-builder';
import { gqlClassDecorator } from './decorators';
import { GraphQLContext } from './types';

export interface SchemaBuilderOptions {
readonly inputTypes?: readonly Type[];
Expand Down Expand Up @@ -56,10 +57,10 @@ export class SchemaBuilder {

generateMutationResolverFields(): GraphQLFieldConfigMap<
unknown,
InjectorContext
GraphQLContext
> {
return [...this.resolvers].reduce<
GraphQLFieldConfigMap<unknown, InjectorContext>
GraphQLFieldConfigMap<unknown, GraphQLContext>
>(
(fields, resolver) => ({
// TODO: validate that fields don't override each other
Expand All @@ -72,10 +73,10 @@ export class SchemaBuilder {

generateSubscriptionResolverFields(): GraphQLFieldConfigMap<
unknown,
InjectorContext
GraphQLContext
> {
return [...this.resolvers].reduce<
GraphQLFieldConfigMap<unknown, InjectorContext>
GraphQLFieldConfigMap<unknown, GraphQLContext>
>(
(fields, resolver) => ({
// TODO: validate that fields don't override each other
Expand All @@ -88,10 +89,10 @@ export class SchemaBuilder {

generateQueryResolverFields(): GraphQLFieldConfigMap<
unknown,
InjectorContext
GraphQLContext
> {
return [...this.resolvers].reduce<
GraphQLFieldConfigMap<unknown, InjectorContext>
GraphQLFieldConfigMap<unknown, GraphQLContext>
>(
(fields, resolver) => ({
// TODO: validate that fields don't override each other
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/lib/types-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
GraphQLUnionType,
} from 'graphql';

import { Context, GraphQLFields, Instance, InternalMiddleware } from './types';
import { Context, GraphQLContext, GraphQLFields, Instance, InternalMiddleware } from './types';
import { typeResolvers } from './decorators';
import { Resolver, Resolvers } from './resolvers';
import { InvalidSubscriptionTypeError, TypeNameRequiredError } from './errors';
Expand Down Expand Up @@ -612,7 +612,7 @@ export class TypesBuilder {
reflectionMethod: ReflectionMethod,
middlewares: readonly InternalMiddleware[],
type: 'query' | 'mutation' | 'subscription' | 'resolveField',
): GraphQLFieldResolver<unknown, InjectorContext, any> {
): GraphQLFieldResolver<unknown, GraphQLContext, any> {
const resolve = (injectorContext: InjectorContext) => {
const instance = injectorContext.get(
resolver.controller,
Expand Down Expand Up @@ -670,7 +670,7 @@ export class TypesBuilder {
returnType,
);

return async (parent, _args, injectorContext) => {
return async (parent, _args, { injectorContext }: GraphQLContext) => {
const args = deserializeArgs(_args, { loosely: false }) as Record<string, unknown>;
const argsValidationErrors = validateArgs(args);
if (argsValidationErrors.length) {
Expand Down Expand Up @@ -734,14 +734,14 @@ export class TypesBuilder {

generateSubscriptionResolverFields<T>(
resolver: Resolver<T>,
): GraphQLFieldConfigMap<unknown, InjectorContext> {
): GraphQLFieldConfigMap<unknown, GraphQLContext> {
const metadata = getClassDecoratorMetadata(resolver.controller);
const resolverType = reflect(resolver.controller);
const reflectionClass = ReflectionClass.from(resolverType);

const fields = new Map<
string,
GraphQLFieldConfig<unknown, InjectorContext>
GraphQLFieldConfig<unknown, GraphQLContext>
>();

// eslint-disable-next-line functional/no-loop-statement
Expand Down Expand Up @@ -775,14 +775,14 @@ export class TypesBuilder {

generateMutationResolverFields<T>(
resolver: Resolver<T>,
): GraphQLFieldConfigMap<unknown, InjectorContext> {
): GraphQLFieldConfigMap<unknown, GraphQLContext> {
const metadata = getClassDecoratorMetadata(resolver.controller);
const resolverType = reflect(resolver.controller);
const reflectionClass = ReflectionClass.from(resolverType);

const fields = new Map<
string,
GraphQLFieldConfig<unknown, InjectorContext>
GraphQLFieldConfig<unknown, GraphQLContext>
>();

// eslint-disable-next-line functional/no-loop-statement
Expand Down Expand Up @@ -817,7 +817,7 @@ export class TypesBuilder {
generateFieldResolver<T>(
resolver: Resolver<T>,
fieldName: string,
): Pick<GraphQLFieldConfig<unknown, InjectorContext>, 'args' | 'resolve'> {
): Pick<GraphQLFieldConfig<unknown, GraphQLContext>, 'args' | 'resolve'> {
const metadata = getClassDecoratorMetadata(resolver.controller);
const resolverType = reflect(resolver.controller);
const reflectionClass = ReflectionClass.from(resolverType);
Expand Down Expand Up @@ -852,14 +852,14 @@ export class TypesBuilder {

generateQueryResolverFields<T>(
resolver: Resolver<T>,
): GraphQLFieldConfigMap<unknown, InjectorContext> {
): GraphQLFieldConfigMap<unknown, GraphQLContext> {
const metadata = getClassDecoratorMetadata(resolver.controller);
const resolverType = reflect(resolver.controller);
const reflectionClass = ReflectionClass.from(resolverType);

const fields = new Map<
string,
GraphQLFieldConfig<unknown, InjectorContext>
GraphQLFieldConfig<unknown, GraphQLContext>
>();

// eslint-disable-next-line functional/no-loop-statement
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ClassType, AbstractClassType } from '@deepkit/core';
import { TypeAnnotation } from '@deepkit/type';
import { InjectorContext } from '@deepkit/injector';

export type GraphQLFields<T> = Record<string, { readonly type: T }>;

Expand Down Expand Up @@ -29,3 +30,7 @@ export type GraphQLMiddlewareFn = (
export type InternalMiddleware =
| ClassType<GraphQLMiddleware>
| GraphQLMiddlewareFn;

export interface GraphQLContext {
readonly injectorContext: InjectorContext;
}
4 changes: 2 additions & 2 deletions packages/yoga/src/lib/yoga-graphql-driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GraphQLSchema } from 'graphql';
import { WebWorkerFactory, ApplicationServer } from '@deepkit/framework';
import { httpWorkflow } from '@deepkit/http';
import { Driver } from '@deepkit-graphql/core';
import { Driver, GraphQLContext } from '@deepkit-graphql/core';
import { createYoga, YogaServerInstance } from 'graphql-yoga';
import { InjectorContext } from '@deepkit/injector';
import { Logger } from '@deepkit/logger';
Expand Down Expand Up @@ -29,7 +29,7 @@ export class YogaDriver extends Driver {
): Promise<void> {
if (!event.request.method || !this.server) return;

await this.server.handle(event.request, event.response, injectorContext);
await this.server.handle(event.request, event.response, <GraphQLContext>{ injectorContext });
}

async start(schema: GraphQLSchema): Promise<void> {
Expand Down

0 comments on commit 518f17f

Please sign in to comment.