Skip to content

Commit

Permalink
chore(): fix tests, tweaks, moving files around
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 19, 2021
1 parent 8415cdf commit 7bb8cfb
Show file tree
Hide file tree
Showing 79 changed files with 710 additions and 14,508 deletions.
13,323 changes: 0 additions & 13,323 deletions package-lock.json

This file was deleted.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
"publish:npm": "lerna publish --conventional-commits",
"prepublish:next": "npm run build",
"publish:next": "lerna publish --dist-tag next",
"test:e2e": "lerna run --parallel test:e2e",
"test:e2e:dev": "lerna run --parallel test:e2e:dev"
"test:e2e": "lerna run test:e2e",
"test:e2e:dev": "lerna run test:e2e:dev"
},
"resolutions": {
"graphql": "15.6.1"
},
"devDependencies": {
"@apollo/gateway": "0.42.3",
Expand Down Expand Up @@ -57,6 +60,7 @@
"lerna": "^4.0.0",
"lerna-changelog": "^2.2.0",
"lint-staged": "11.2.3",
"mercurius": "^8.9.1",
"prettier": "2.4.1",
"reflect-metadata": "0.1.13",
"release-it": "14.11.6",
Expand Down Expand Up @@ -91,5 +95,8 @@
"repository": {
"type": "git",
"url": "https://github.com/nestjs/graphql"
},
"dependencies": {
"@graphql-tools/schema": "7.1.5"
}
}
10 changes: 0 additions & 10 deletions packages/apollo/lib/drivers/apollo-base.driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { HttpStatus } from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { AbstractGraphQLDriver } from '@nestjs/graphql-experimental/drivers/abstract-graphql.driver';
import { normalizeRoutePath } from '@nestjs/graphql-experimental/utils';
import {
ApolloError,
ApolloServerBase,
Expand Down Expand Up @@ -163,15 +162,6 @@ export abstract class ApolloBaseDriver<
this._apolloServer = apolloServer;
}

private getNormalizedPath(apolloOptions: T): string {
const prefix = this.applicationConfig.getGlobalPrefix();
const useGlobalPrefix = prefix && apolloOptions.useGlobalPrefix;
const gqlOptionsPath = normalizeRoutePath(apolloOptions.path);
return useGlobalPrefix
? normalizeRoutePath(prefix) + gqlOptionsPath
: gqlOptionsPath;
}

private wrapFormatErrorFn(options: T) {
if (options.autoTransformHttpErrors === false) {
return;
Expand Down
55 changes: 16 additions & 39 deletions packages/apollo/lib/drivers/apollo-federation.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,41 @@ import { SchemaDirectiveVisitor } from '@graphql-tools/utils';
import { Injectable } from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { ModulesContainer } from '@nestjs/core';
import { GraphQLSchemaBuilder } from '@nestjs/graphql-experimental/graphql-schema.builder';
import { GraphQLSchemaHost } from '@nestjs/graphql-experimental/graphql-schema.host';
import {
ResolversExplorerService,
ScalarsExplorerService,
} from '@nestjs/graphql-experimental/services';
import { GraphQLFederationFactory } from '@nestjs/graphql-experimental/federation/graphql-federation.factory';
import { extend } from '@nestjs/graphql-experimental/utils/extend.util';
import { ApolloDriverConfig } from '..';
import { GraphQLFederationFactory } from '../factories/graphql-federation.factory';
import { PluginsExplorerService } from '../services/plugins-explorer.service';
import { ApolloBaseDriver } from './apollo-base.driver';

@Injectable()
export class ApolloFederationDriver extends ApolloBaseDriver {
private readonly graphqlFederationFactory: GraphQLFederationFactory;
private readonly pluginsExplorerService: PluginsExplorerService;

constructor(
resolversExplorerService: ResolversExplorerService,
scalarsExplorerService: ScalarsExplorerService,
gqlSchemaBuilder: GraphQLSchemaBuilder,
gqlSchemaHost: GraphQLSchemaHost,
private readonly graphqlFederationFactory: GraphQLFederationFactory,
modulesContainer: ModulesContainer,
) {
super();
const pluginsExplorerService = new PluginsExplorerService(modulesContainer);
this.graphqlFederationFactory = new GraphQLFederationFactory(
resolversExplorerService,
scalarsExplorerService,
pluginsExplorerService,
gqlSchemaBuilder,
gqlSchemaHost,
);
this.pluginsExplorerService = new PluginsExplorerService(modulesContainer);
}

public async start(options: ApolloDriverConfig): Promise<void> {
const { printSubgraphSchema } = loadPackage(
'@apollo/subgraph',
'ApolloFederation',
() => require('@apollo/subgraph'),
options.plugins = extend(
options.plugins || [],
this.pluginsExplorerService.explore(options),
);
options = await this.mergeDefaultOptions(options);

const { typePaths } = options;
const typeDefs =
(await this.graphQlTypesLoader.mergeTypesByPaths(typePaths)) || [];

const mergedTypeDefs = extend(typeDefs, options.typeDefs);
const adapterOptions = await this.graphqlFederationFactory.mergeOptions({
...options,
typeDefs: mergedTypeDefs,
});
await this.runPreOptionsHooks(adapterOptions);
const adapterOptions = await this.graphqlFederationFactory.mergeWithSchema(
options,
);
await this.runExecutorFactoryIfPresent(adapterOptions);

if (options.definitions && options.definitions.path) {
const { printSubgraphSchema } = loadPackage(
'@apollo/subgraph',
'ApolloFederation',
() => require('@apollo/subgraph'),
);
await this.graphQlFactory.generateDefinitions(
printSubgraphSchema(adapterOptions.schema),
options,
Expand All @@ -72,10 +53,6 @@ export class ApolloFederationDriver extends ApolloBaseDriver {
}
}

public async runPreOptionsHooks(apolloOptions: ApolloDriverConfig) {
await this.runExecutorFactoryIfPresent(apolloOptions);
}

protected async registerExpress(apolloOptions: ApolloDriverConfig) {
return super.registerExpress(apolloOptions, {
preStartHook: () => {
Expand Down
29 changes: 10 additions & 19 deletions packages/apollo/lib/drivers/apollo.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,31 @@ export class ApolloDriver extends ApolloBaseDriver {
}

public async start(apolloOptions: ApolloDriverConfig) {
const options = await this.mergeDefaultOptions(apolloOptions);
const typeDefs =
(await this.graphQlTypesLoader.mergeTypesByPaths(options.typePaths)) ||
[];

const mergedTypeDefs = extend(typeDefs, options.typeDefs);

options.plugins = extend(
options.plugins || [],
this.pluginsExplorerService.explore(options),
apolloOptions.plugins = extend(
apolloOptions.plugins || [],
this.pluginsExplorerService.explore(apolloOptions),
);

const adapterOptions =
await this.graphQlFactory.mergeOptions<ApolloDriverConfig>({
...options,
typeDefs: mergedTypeDefs,
});
await this.runPreOptionsHooks(adapterOptions);
const options =
await this.graphQlFactory.mergeWithSchema<ApolloDriverConfig>(
apolloOptions,
);

if (options.definitions && options.definitions.path) {
await this.graphQlFactory.generateDefinitions(
printSchema(adapterOptions.schema),
printSchema(options.schema),
options,
);
}

await this.registerServer(adapterOptions);
await this.registerServer(options);

if (options.installSubscriptionHandlers || options.subscriptions) {
const subscriptionsOptions: SubscriptionConfig =
options.subscriptions || { 'subscriptions-transport-ws': {} };
this._subscriptionService = new GqlSubscriptionService(
{
schema: adapterOptions.schema,
schema: options.schema,
path: options.path,
context: options.context,
...subscriptionsOptions,
Expand Down
1 change: 0 additions & 1 deletion packages/apollo/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './decorators';
export * from './drivers';
export * from './factories';
export * from './interfaces';
export * from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ export interface ApolloDriverConfig
extends Omit<Config, 'typeDefs'>,
ServerRegistration,
Omit<GqlModuleOptions, 'context'> {
/**
* Paths to files that contain GraphQL definitions
*/
typePaths?: string[];

/**
* Executor factory function
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"url": "https://github.com/nestjs/graphql/issues"
},
"dependencies": {
"@graphql-tools/utils": "7"
"@graphql-tools/utils": "7.10.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ArgumentsHost, Catch, UnauthorizedException } from '@nestjs/common';
import { GqlExceptionFilter } from '@nestjs/graphql-experimental';

@Catch(UnauthorizedException)
export class UnauthorizedFilter implements GqlExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql-experimental';

@Injectable()
export class AuthGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Scalar } from '@nestjs/graphql-experimental';
import { Kind, ValueNode } from 'graphql';

@Scalar('Date', (type) => Date)
export class DateScalar {
description = 'Date custom scalar type';
Expand Down
6 changes: 4 additions & 2 deletions packages/apollo/tests/e2e/generated-definitions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { INestApplication } from '@nestjs/common';
import { GraphQLFactory } from '@nestjs/graphql-experimental';
import {
GraphQLFactory,
GraphQLFederationDefinitionsFactory,
} from '@nestjs/graphql-experimental';
import { Test } from '@nestjs/testing';
import * as fs from 'fs';
import * as path from 'path';
import * as util from 'util';
import { GraphQLFederationDefinitionsFactory } from '../../lib';
import { ApplicationModule } from '../code-first/app.module';

const readFile = util.promisify(fs.readFile);
Expand Down
10 changes: 5 additions & 5 deletions packages/apollo/tests/subscriptions/graphql-ws.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ describe('graphql-ws protocol', () => {

app = module.createNestApplication();
await app.init();
await app.listen(3000);
await app.listen(3007);
});

it('should receive an error if missing token', (done) => {
wsClient = createClient({
url: 'ws://localhost:3000/graphql',
url: 'ws://localhost:3007/graphql',
webSocketImpl: ws,
connectionParams: {},
retryAttempts: 0,
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('graphql-ws protocol', () => {

it('should receive an error if token is malformed', (done) => {
wsClient = createClient({
url: 'ws://localhost:3000/graphql',
url: 'ws://localhost:3007/graphql',
webSocketImpl: ws,
connectionParams: {
authorization: 'wrong token',
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('graphql-ws protocol', () => {

it('should receive error on subscription if guard fails', (done) => {
wsClient = createClient({
url: 'ws://localhost:3000/graphql',
url: 'ws://localhost:3007/graphql',
webSocketImpl: ws,
connectionParams: {
authorization: 'Bearer notest',
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('graphql-ws protocol', () => {

it('should connect to subscriptions', (done) => {
wsClient = createClient({
url: 'ws://localhost:3000/graphql',
url: 'ws://localhost:3007/graphql',
webSocketImpl: ws,
connectionParams: {
authorization: 'Bearer test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ describe('subscriptions-transport-ws protocol', () => {

app = module.createNestApplication();
await app.init();
await app.listen(3001);
await app.listen(3006);
});

it('should receive an error if missing token', (done) => {
wsClient = new SubscriptionClient(
'ws://localhost:3001/graphql',
'ws://localhost:3006/graphql',
{
connectionCallback: (errors) => {
const error = errors as unknown as Error;
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('subscriptions-transport-ws protocol', () => {

it('should receive an error if token is malformed', (done) => {
wsClient = new SubscriptionClient(
'ws://localhost:3001/graphql',
'ws://localhost:3006/graphql',
{
connectionCallback: (errors) => {
const error = errors as unknown as Error;
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('subscriptions-transport-ws protocol', () => {

it('should fail to connect if no authorization is provided', (done) => {
wsClient = new SubscriptionClient(
'ws://localhost:3001/graphql',
'ws://localhost:3006/graphql',
{
connectionParams: {
authorization: 'Bearer notest',
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('subscriptions-transport-ws protocol', () => {

it('should receive subscriptions', (done) => {
wsClient = new SubscriptionClient(
'ws://localhost:3001/graphql',
'ws://localhost:3006/graphql',
{
connectionParams: {
authorization: 'Bearer test',
Expand Down
21 changes: 13 additions & 8 deletions packages/graphql/lib/drivers/abstract-graphql.driver.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Inject } from '@nestjs/common';
import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
import { GraphQLTypesLoader } from '../graphql-types.loader';
import { GraphQLFactory } from '../graphql.factory';
import { GqlModuleOptions } from '../interfaces';
import { wrapContextResolver } from '../utils';
import { GqlModuleOptions, GraphQLDriver } from '../interfaces';
import { normalizeRoutePath, wrapContextResolver } from '../utils';

export abstract class AbstractGraphQLDriver<
TDriver = unknown,
TOptions extends Record<string, any> = GqlModuleOptions,
> {
> implements GraphQLDriver<TOptions>
{
@Inject()
protected readonly httpAdapterHost: HttpAdapterHost;

Expand All @@ -18,15 +18,11 @@ export abstract class AbstractGraphQLDriver<
@Inject()
protected readonly graphQlFactory: GraphQLFactory;

@Inject()
protected readonly graphQlTypesLoader: GraphQLTypesLoader;

abstract get instance(): TDriver;

public abstract start(options: TOptions): Promise<unknown>;
public abstract stop(): Promise<void>;

public async runPreOptionsHooks(options: TOptions): Promise<void> {}
public async mergeDefaultOptions(
options: TOptions,
defaults: Record<string, any> = {
Expand All @@ -41,4 +37,13 @@ export abstract class AbstractGraphQLDriver<
wrapContextResolver(clonedOptions, options);
return clonedOptions;
}

protected getNormalizedPath(options: TOptions): string {
const prefix = this.applicationConfig.getGlobalPrefix();
const useGlobalPrefix = prefix && options.useGlobalPrefix;
const gqlOptionsPath = normalizeRoutePath(options.path);
return useGlobalPrefix
? normalizeRoutePath(prefix) + gqlOptionsPath
: gqlOptionsPath;
}
}
Loading

0 comments on commit 7bb8cfb

Please sign in to comment.