Skip to content

Commit

Permalink
Merge pull request #2 from lpessoa/upgrade/apollo-v4
Browse files Browse the repository at this point in the history
chore: fixing pipes and code-first spec tests and some code cleanup
  • Loading branch information
luas10c committed Feb 2, 2023
2 parents c49e4b2 + b46d9b4 commit 250639a
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 1,285 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ test-schema.graphql

# dist
packages/**/dist
**/*.tsbuildinfo
**/*.tsbuildinfo

.yarn/
49 changes: 25 additions & 24 deletions packages/apollo/lib/drivers/apollo-base.driver.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
//import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { AbstractGraphQLDriver } from '@nestjs/graphql';

/* import {
ApolloError,
ApolloServerBase,
ApolloServerPluginLandingPageDisabled,
ApolloServerPluginLandingPageGraphQLPlayground,
AuthenticationError,
ForbiddenError,
PluginDefinition,
UserInputError,
} from 'apollo-server-core'; */

import { loadPackage } from '@nestjs/common/utils/load-package.util';

import { GraphQLError, GraphQLFormattedError } from 'graphql';
import { GraphQLError, GraphQLFormattedError, Kind } from 'graphql';
import * as omit from 'lodash.omit';
import { ApolloDriverConfig } from '../interfaces';
import { createAsyncIterator } from '../utils/async-iterator.util';

import { ApolloServerErrorCode } from '@apollo/server/errors';
import { ApolloServer, type BaseContext } from '@apollo/server';
import {
ApolloServerErrorCode,
unwrapResolverError,
} from '@apollo/server/errors';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
import { expressMiddleware } from '@apollo/server/express4';
Expand All @@ -32,6 +23,13 @@ import {
fastifyApolloHandler,
fastifyApolloDrainPlugin,
} from '@as-integrations/fastify';
import { HttpStatus } from '@nestjs/common';

const apolloPredefinedExceptions: Partial<Record<HttpStatus, string>> = {
[HttpStatus.BAD_REQUEST]: ApolloServerErrorCode.BAD_USER_INPUT,
[HttpStatus.UNAUTHORIZED]: 'UNAUTHENTICATED',
[HttpStatus.FORBIDDEN]: 'FORBIDDEN',
};

export abstract class ApolloBaseDriver<
T extends Record<string, any> = ApolloDriverConfig,
Expand Down Expand Up @@ -171,11 +169,11 @@ export abstract class ApolloBaseDriver<
const app = httpAdapter.getInstance();

const { path, typeDefs, resolvers, schema } = options;

const server = new ApolloServer<BaseContext>({
typeDefs,
resolvers,
schema,
...options,
plugins: [fastifyApolloDrainPlugin(app)],
});

Expand Down Expand Up @@ -214,25 +212,28 @@ export abstract class ApolloBaseDriver<
const exceptionRef = originalError?.extensions?.exception;
const isHttpException =
exceptionRef?.response?.statusCode && exceptionRef?.status;

if (!isHttpException) {
return originalError as GraphQLFormattedError;
}
let error: ApolloServerErrorCode;
let error: GraphQLError;

const httpStatus = exceptionRef?.status;
console.log('httpStatus', httpStatus);
/* if (httpStatus in apolloPredefinedExceptions) {
error = new apolloPredefinedExceptions[httpStatus](
exceptionRef?.message,
);
if (httpStatus in apolloPredefinedExceptions) {
error = new GraphQLError(exceptionRef?.message, {
extensions: {
code: apolloPredefinedExceptions[httpStatus],
},
});
} else {
error = new ApolloError(exceptionRef.message, httpStatus?.toString());
error = new GraphQLError(exceptionRef.message, httpStatus?.toString());
}

error.stack = exceptionRef?.stacktrace;
//TODO: we need to verify if previous behavior is to be kept
// if so we must open a PR on Apollo to include response inside the raised exception
//https://github.com/apollographql/apollo-server/blob/e6d0d6d9cbd78d4914adf2abb04d84710991849a/packages/server/src/errorNormalize.ts#L58
error.extensions['response'] = exceptionRef?.response;
return error; */
return error;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import {
GqlOptionsFactory,
SubscriptionConfig,
} from '@nestjs/graphql';
/* import {
ApolloServerPluginLandingPageGraphQLPlaygroundOptions,
Config,
GraphQLExecutor,
} from 'apollo-server-core'; */
import { GraphQLSchema } from 'graphql';
import { CorsOptions } from 'cors';

Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/tests/code-first-federation/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
import { ApolloDriverConfig } from '../../lib';
import { ApolloFederationDriver } from '../../lib/drivers';
import { PostModule } from './post/post.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
GraphQLInt,
} from 'graphql';
import responseCachePlugin from 'apollo-server-plugin-response-cache';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloFederationDriverConfig } from '../../lib';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
import { ApolloDriverConfig } from '../../../lib';
import { ApolloFederationDriver } from '../../../lib/drivers';
import { PostsModule } from './posts/posts.module';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
import { ApolloDriverConfig } from '../../../lib';
import { ApolloFederationDriver } from '../../../lib/drivers';
import { UsersModule } from './users/users.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { IRecipeResolver } from '../code-first-federation/recipe/irecipe.resolve
import { UserResolver } from '../code-first-federation/user/user.resolver';
import { printedSchemaSnapshot } from '../utils/printed-schema-with-cache-control.snapshot';
import { INestApplication } from '@nestjs/common';
import { ApolloServerBase } from 'apollo-server-core';
import { ApolloServer } from '@apollo/server';
import { ApolloFederationDriver } from '../../lib';
import { gql } from 'graphql-tag';
import { CachingApplicationModule } from '../code-first-federation/caching.module';
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo/tests/e2e/code-first-federation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ApolloServer } from '@apollo/server';
import { INestApplication } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { Test } from '@nestjs/testing';
import { ApolloServerBase } from 'apollo-server-core';
import { gql } from 'graphql-tag';
import { ApolloFederationDriver } from '../../lib';
import { ApplicationModule } from '../code-first-federation/app.module';

describe('Code-first - Federation', () => {
let app: INestApplication;
let apolloClient: ApolloServerBase;
let apolloClient: ApolloServer;

beforeEach(async () => {
const module = await Test.createTestingModule({
Expand Down
21 changes: 15 additions & 6 deletions packages/apollo/tests/e2e/code-first.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { INestApplication } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { Test } from '@nestjs/testing';
import { ApolloServerBase } from 'apollo-server-core';
import { ApolloServer } from '@apollo/server';
import { gql } from 'graphql-tag';
import { ApolloDriver } from '../../lib';
import { ApplicationModule } from '../code-first/app.module';
import * as assert from 'assert';

describe('Code-first', () => {
let app: INestApplication;
let apolloClient: ApolloServerBase;
let apolloClient: ApolloServer;

beforeEach(async () => {
const module = await Test.createTestingModule({
Expand All @@ -33,7 +34,9 @@ describe('Code-first', () => {
}
`,
});
expect(response.data).toEqual({
assert(response.body.kind === 'single');
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data).toEqual({
categories: [
{
name: 'Category #1',
Expand All @@ -59,7 +62,9 @@ describe('Code-first', () => {
}
`,
});
expect(response.data).toEqual({
assert(response.body.kind === 'single');
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data).toEqual({
search: [
{
title: 'recipe',
Expand Down Expand Up @@ -88,7 +93,9 @@ describe('Code-first', () => {
}
`,
});
expect(response.data).toEqual({
assert(response.body.kind === 'single');
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data).toEqual({
recipes: [
{
id: '1',
Expand Down Expand Up @@ -133,7 +140,9 @@ describe('Code-first', () => {
}
`,
});
expect(response.data).toEqual({
assert(response.body.kind === 'single');
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data).toEqual({
recipes: [
{
id: '1',
Expand Down
13 changes: 3 additions & 10 deletions packages/apollo/tests/e2e/pipes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,10 @@ describe('GraphQL - Pipes', () => {
data: null,
errors: [
{
extensions: {
code: 'BAD_USER_INPUT',
response: {
error: 'Bad Request',
message: [
'description must be longer than or equal to 30 characters',
],
statusCode: 400,
},
},
message: 'Bad Request Exception',
locations: [{ line: 2, column: 3 }],
path: ['addRecipe'],
extensions: { code: 'INTERNAL_SERVER_ERROR' },
},
],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
import { join } from 'path';
import { ApolloDriverConfig } from '../../../lib';
import { ApolloFederationDriver } from '../../../lib/drivers';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
import { join } from 'path';
import { ApolloDriverConfig, ApolloDriverConfigFactory } from '../../../../lib';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
import { join } from 'path';
import { ApolloDriverConfig } from '../../../lib';
import { ApolloFederationDriver } from '../../../lib/drivers';
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/lib/exceptions/graphql-exception.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GraphQLError } from 'graphql';

import type { ExpcetionOptions } from '../interfaces/graphql-exception.interface';
import type { ExceptionOptions } from '../interfaces/graphql-exception.interface';

export class GraphQLException extends GraphQLError {
constructor(message: string, options: ExpcetionOptions) {
constructor(message: string, options: ExceptionOptions) {
super(message, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
GraphQLErrorExtensions,
} from 'graphql/error';

export interface ExpcetionOptions extends GraphQLErrorOptions {
export interface ExceptionOptions extends GraphQLErrorOptions {
extensions: GraphQLErrorExtensions & {
http: {
status: number;
Expand Down
Loading

0 comments on commit 250639a

Please sign in to comment.