Skip to content

Commit deb5de8

Browse files
committed
test: improve 271 test suite and add tsts for 253 issus – for proper testing aliases with projections & descriminators
1 parent ae34da9 commit deb5de8

File tree

2 files changed

+151
-13
lines changed

2 files changed

+151
-13
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { schemaComposer } from 'graphql-compose';
2+
import { composeWithMongooseDiscriminators } from '../../index';
3+
import { mongoose, Schema } from '../../__mocks__/mongooseCommon';
4+
import { testFieldConfig } from '../../utils/testHelpers';
5+
6+
const SubCarSchema = new Schema({ s: { type: Number, required: true, alias: 'speed' } });
7+
8+
const CarSchema = new Schema(
9+
{ s: { type: Number, required: true, alias: 'speed' }, aaa: SubCarSchema },
10+
{ discriminatorKey: 't' }
11+
);
12+
const Car = mongoose.model('Car', CarSchema);
13+
14+
const TimeMachineSchema = new Schema(
15+
{ f: { type: Number, required: true, alias: 'fluxCompensatorVersion' } },
16+
{ discriminatorKey: 't' }
17+
);
18+
const TimeMachine = Car.discriminator('TimeMachine', TimeMachineSchema);
19+
20+
const CarDTC = composeWithMongooseDiscriminators(Car);
21+
22+
schemaComposer.Query.addFields({
23+
allCars: CarDTC.getResolver('findMany'),
24+
timeMachines: CarDTC.discriminator(TimeMachine).getResolver('findMany'),
25+
});
26+
27+
// console.log(schemaComposer.toSDL({ omitDescriptions: true }));
28+
29+
beforeAll(async () => {
30+
await mongoose.createConnection();
31+
await TimeMachine.create({ speed: 300, fluxCompensatorVersion: 5 });
32+
});
33+
afterAll(() => mongoose.disconnect());
34+
35+
describe('issue #253 - Consider aliases from discriminators during preparation', () => {
36+
it('check data in db', async () => {
37+
const data = await Car.find({}).lean();
38+
expect(data).toEqual([{ __v: 0, _id: expect.anything(), f: 5, s: 300, t: 'TimeMachine' }]);
39+
});
40+
41+
it('check query', async () => {
42+
const res = await testFieldConfig({
43+
field: CarDTC.getResolver('findMany'),
44+
selection: `{
45+
__typename
46+
speed
47+
... on TimeMachine {
48+
fluxCompensatorVersion
49+
}
50+
}`,
51+
schemaComposer,
52+
});
53+
expect(res).toEqual([
54+
{
55+
__typename: 'TimeMachine',
56+
fluxCompensatorVersion: 5,
57+
speed: 300,
58+
},
59+
]);
60+
});
61+
});

src/__tests__/github_issues/271-test.ts

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { Document } from 'mongoose';
55
import { convertSchemaToGraphQL } from '../../../src/fieldsConverter';
66
const schemaComposer = new SchemaComposer<{ req: any }>();
77

8+
// mongoose.set('debug', true);
9+
810
const AuthorSchema = new mongoose.Schema(
911
{
1012
name: { type: String },
11-
age: { type: Number },
13+
ag: { type: Number, alias: 'age' },
1214
isAlive: { type: Boolean },
1315
},
1416
{ _id: false }
@@ -17,8 +19,8 @@ const AuthorSchema = new mongoose.Schema(
1719
const BookSchema = new mongoose.Schema({
1820
_id: { type: Number },
1921
title: { type: String, required: true },
20-
pageCount: { type: Number },
21-
author: { type: AuthorSchema },
22+
pc: { type: Number, alias: 'pageCount' },
23+
a: { type: AuthorSchema, alias: 'author' },
2224
});
2325

2426
interface IAuthor {
@@ -36,25 +38,45 @@ interface IBook extends Document {
3638

3739
const BookModel = mongoose.model<IBook>('Book', BookSchema);
3840

39-
convertSchemaToGraphQL(AuthorSchema, 'Author', schemaComposer);
41+
const AuthorTC = convertSchemaToGraphQL(AuthorSchema, 'Author', schemaComposer);
42+
AuthorTC.setField('isAbove100', {
43+
type: 'String',
44+
resolve: (s: IAuthor) => {
45+
return !s?.age ? 'unknown' : s.age > 100 ? 'yes' : 'no';
46+
},
47+
projection: { age: true },
48+
});
4049

4150
const BookTC = composeMongoose(BookModel, { schemaComposer });
51+
BookTC.setField('bookSize', {
52+
type: 'String',
53+
resolve: (s: IBook) => {
54+
return !s?.pageCount ? 'unknown' : s.pageCount > 500 ? 'big' : 'small';
55+
},
56+
projection: { pageCount: true },
57+
});
4258

59+
let lastExecutedProjection: Record<string, true>;
4360
schemaComposer.Query.addFields({
44-
bookById: BookTC.mongooseResolvers.findById(),
45-
bookFindOne: BookTC.mongooseResolvers.findOne(),
46-
booksMany: BookTC.mongooseResolvers.findMany(),
61+
booksMany: BookTC.mongooseResolvers.findMany().wrapResolve((next) => async (rp) => {
62+
const res = await next(rp);
63+
lastExecutedProjection = rp.query._fields;
64+
return res;
65+
}),
4766
});
4867

4968
const schema = schemaComposer.buildSchema();
5069

5170
beforeAll(async () => {
52-
mongoose.set('debug', true);
5371
await BookModel.base.createConnection();
5472
await BookModel.create({
5573
_id: 1,
5674
title: 'Atlas Shrugged',
57-
author: { age: new Date().getFullYear() - 1905, isAlive: false, name: 'Ayn Rand' },
75+
author: {
76+
age: 115,
77+
isAlive: false,
78+
name: 'Ayn Rand',
79+
},
5880
pageCount: 1168,
5981
});
6082
});
@@ -63,7 +85,28 @@ afterAll(() => {
6385
BookModel.base.disconnect();
6486
});
6587

66-
describe('nested projection - issue #271', () => {
88+
describe('nested projections with aliases - issue #271', () => {
89+
it('check mongoose itself', async () => {
90+
const book = (
91+
await BookModel.find({}).select({
92+
bookSize: true,
93+
'a.isAbove100': true,
94+
'a.ag': true,
95+
pc: true,
96+
})
97+
)[0];
98+
expect(book?.pageCount).toEqual(1168);
99+
expect(book?.author?.age).toEqual(115);
100+
expect(book?.toObject({ virtuals: true })).toEqual({
101+
_id: 1,
102+
a: { ag: 115, age: 115, id: null },
103+
author: { ag: 115, age: 115, id: null },
104+
id: '1',
105+
pageCount: 1168,
106+
pc: 1168,
107+
});
108+
});
109+
67110
it('Happy Path', async () => {
68111
const result = await graphql.graphql({
69112
schema,
@@ -76,7 +119,12 @@ describe('nested projection - issue #271', () => {
76119
}`,
77120
contextValue: {},
78121
});
79-
console.log(JSON.stringify(result));
122+
expect(lastExecutedProjection).toEqual({ 'a.name': true, pc: true, title: true });
123+
expect(result).toEqual({
124+
data: {
125+
booksMany: [{ author: { name: 'Ayn Rand' }, pageCount: 1168, title: 'Atlas Shrugged' }],
126+
},
127+
});
80128
});
81129

82130
it('Handles a Fragment', async () => {
@@ -90,12 +138,41 @@ describe('nested projection - issue #271', () => {
90138
query {
91139
booksMany {
92140
title
93-
pageCount
94141
author { ...Auth }
95142
}
96143
}`,
97144
contextValue: {},
98145
});
99-
console.log(JSON.stringify(result));
146+
expect(lastExecutedProjection).toEqual({ 'a.name': true, title: true });
147+
expect(result).toEqual({
148+
data: {
149+
booksMany: [{ author: { name: 'Ayn Rand' }, title: 'Atlas Shrugged' }],
150+
},
151+
});
152+
});
153+
154+
it('Handles computable fields which uses `projection` property', async () => {
155+
const result = await graphql.graphql({
156+
schema,
157+
source: `
158+
query {
159+
booksMany {
160+
bookSize
161+
author {
162+
isAbove100
163+
}
164+
}
165+
}`,
166+
contextValue: {},
167+
});
168+
expect(lastExecutedProjection).toEqual({
169+
bookSize: true,
170+
'a.isAbove100': true,
171+
'a.ag': true,
172+
pc: true,
173+
});
174+
expect(result).toEqual({
175+
data: { booksMany: [{ author: { isAbove100: 'yes' }, bookSize: 'big' }] },
176+
});
100177
});
101178
});

0 commit comments

Comments
 (0)