Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/apollo/tests/code-first-extensions/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '../../lib';
import { UserModule } from './user/user.module';
import { Module } from '@nestjs/common';

/**
* Main application module for the code-first extensions example.
*/
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: true,
playground: false,
}),
UserModule,
],
})
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Extensions, Field, InputType } from '@nestjs/graphql';

@InputType()
@Extensions({ exampleExtension: 'exampleValue' })
export class CreateUserInput {
@Extensions({ fieldLevelExtension: 123 })
@Field()
name!: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ID, Field, ObjectType, Extensions } from '@nestjs/graphql';

@ObjectType()
export class Status {
@Field(() => ID)
id!: string;

@Field()
@Extensions({ isPublic: true })
code!: string;
}
15 changes: 15 additions & 0 deletions packages/apollo/tests/code-first-extensions/user/user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Extensions, Field, ID, ObjectType } from '@nestjs/graphql';
import { Status } from './user-status.dto';

@ObjectType()
export class User {
@Field(() => ID)
id!: string;

@Field()
name!: string;

@Extensions({ isPublic: true })
@Field(() => Status, { nullable: true, description: 'DTO Description' })
status?: Status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { UserResolver } from './user.resolver';
import { UserService } from './user.service';

@Module({
providers: [UserResolver, UserService],
})
export class UserModule {}
38 changes: 38 additions & 0 deletions packages/apollo/tests/code-first-extensions/user/user.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Args,
Mutation,
Parent,
Query,
ResolveField,
Resolver,
} from '@nestjs/graphql';
import { User } from './user.dto';
import { UserService } from './user.service';
import { CreateUserInput } from './create-user.input';
import { Status } from './user-status.dto';

@Resolver(() => User)
export class UserResolver {
constructor(private readonly userService: UserService) {}

@Query(() => [User], { name: 'users' })
findAll(): User[] {
return this.userService.findAll();
}

@Mutation(() => User)
createUser(@Args('createUserInput') createUserInput: CreateUserInput): User {
return this.userService.create(createUserInput);
}

@ResolveField('status', undefined, {
nullable: true,
description: 'Resolve Field Description',
})
getStatus(@Parent() user: User): Status {
return {
id: 'status-id',
code: 'ACTIVE',
};
}
}
26 changes: 26 additions & 0 deletions packages/apollo/tests/code-first-extensions/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { User } from './user.dto';
import { CreateUserInput } from './create-user.input';

@Injectable()
export class UserService {
private users: User[] = [];
private idCounter = 1;

findAll(): User[] {
return this.users;
}

findOne(id: string): User | undefined {
return this.users.find((user) => user.id === id);
}

create(createUserInput: CreateUserInput): User {
const user: User = {
id: String(this.idCounter++),
name: createUserInput.name,
};
this.users.push(user);
return user;
}
}
35 changes: 35 additions & 0 deletions packages/apollo/tests/e2e/code-first-extensions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { INestApplication } from '@nestjs/common';
import { GraphQLSchemaHost } from '@nestjs/graphql';
import { Test } from '@nestjs/testing';
import * as assert from 'assert';
import { GraphQLObjectType } from 'graphql';
import { AppModule } from '../code-first-extensions/app.module';

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

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleRef.createNestApplication();
await app.init();
});

afterAll(async () => {
await app.close();
});

it('Adds extensions to resolved field', async () => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I've asserted that this test fails without the changed I made to the type-metadata.storage

const { schema } = app.get(GraphQLSchemaHost);
const userObject = schema.getType('User') as GraphQLObjectType;
assert(userObject, 'User type not found in schema');
const statusField = userObject.getFields().status;
assert(statusField, 'status field not found in User type');

const extensions = statusField.extensions;

expect(extensions.isPublic).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export class TypeMetadataStorageHost {
}
let objectOrInterfaceTypeField =
objectOrInterfaceTypeMetadata.properties.find(
(fieldDef) => fieldDef.name === item.methodName,
(fieldDef) => fieldDef.schemaName === item.schemaName,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this didn't compare schemaName to find existing fields' metadata. When comparing to methodName it wrongly assumes the methodName will always reflect the schemaName, which is not the case.

);
for (
let _objectTypeRef = objectTypeRef;
Expand All @@ -430,7 +430,7 @@ export class TypeMetadataStorageHost {
) {
const possibleTypeMetadata = getTypeMetadata(_objectTypeRef);
objectOrInterfaceTypeField = possibleTypeMetadata?.properties.find(
(fieldDef) => fieldDef.name === item.methodName,
(fieldDef) => fieldDef.schemaName === item.schemaName,
);
if (objectOrInterfaceTypeField) {
objectTypeRef = _objectTypeRef;
Expand Down
1 change: 0 additions & 1 deletion packages/graphql/tests/graphql.factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { GraphQLAstExplorer, GraphQLFactory } from '../lib';
import { GraphQLSchemaBuilder } from '../lib/graphql-schema.builder';
import { ResolversExplorerService } from '../lib/services/resolvers-explorer.service';
import { ScalarsExplorerService } from '../lib/services/scalars-explorer.service';
import gql from 'graphql-tag';
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unused import

import { GraphQLSchema } from 'graphql';

describe('GraphQLFactory', () => {
Expand Down