Skip to content

Commit

Permalink
test: check projection with addRelation method
Browse files Browse the repository at this point in the history
closes #370
  • Loading branch information
nodkz committed Aug 25, 2021
1 parent 89c5328 commit c2449a9
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/__tests__/github_issues/370-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { SchemaComposer, graphql } from 'graphql-compose';
import { composeMongoose } from '../../index';
import { mongoose } from '../../__mocks__/mongooseCommon';
import { Schema } from 'mongoose';

const schemaComposer = new SchemaComposer<{ req: any }>();

// mongoose.set('debug', true);

const UserSchema = new mongoose.Schema({
firstName: {
type: String,
required: true,
},
_organizationIds: [
{
type: Schema.Types.ObjectId,
ref: 'Organization',
index: true,
},
],
});
const UserModel = mongoose.model('User', UserSchema);
const UserTC = composeMongoose(UserModel, { schemaComposer });

const OrganizationSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
});
const OrganizationModel = mongoose.model('Organization', OrganizationSchema);
const OrganizationTC = composeMongoose(OrganizationModel, { schemaComposer });

UserTC.addRelation('organizations', {
resolver: () => OrganizationTC.mongooseResolvers.findByIds(),
prepareArgs: {
_ids: (source: any) => source._organizationIds,
skip: null,
sort: null,
},
projection: { _organizationIds: 1 },
});

schemaComposer.Query.addFields({
users: UserTC.mongooseResolvers.findMany(),
});

const schema = schemaComposer.buildSchema();

beforeAll(async () => {
await OrganizationModel.base.createConnection();
const orgs = await OrganizationModel.create([
{ title: 'Org1' },
{ title: 'Org2' },
{ title: 'Org3' },
]);
await UserModel.create([
{ firstName: 'User1', _organizationIds: [orgs[0]._id, orgs[1]._id] },
{ firstName: 'User2', _organizationIds: [orgs[2]._id] },
]);
});
afterAll(() => {
OrganizationModel.base.disconnect();
});

describe('issue #370 - addRelation: projection not working as expected ', () => {
it('check', async () => {
const result = await graphql.graphql({
schema,
source: `query {
users {
firstName
organizations {
title
}
}
}`,
});
expect(result).toEqual({
data: {
users: [
{ firstName: 'User1', organizations: [{ title: 'Org1' }, { title: 'Org2' }] },
{ firstName: 'User2', organizations: [{ title: 'Org3' }] },
],
},
});
});
});

0 comments on commit c2449a9

Please sign in to comment.