diff --git a/package.json b/package.json index 1b4a3f9f..72ba3890 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "eslint-plugin-import": "2.25.3", "eslint-plugin-prettier": "4.0.0", "graphql": "16.1.0", - "graphql-compose": "9.0.5", + "graphql-compose": "9.0.8", "jest": "27.4.5", "mongodb-memory-server": "8.0.4", "mongoose": "6.1.2", diff --git a/src/__tests__/github_issues/376-test.ts b/src/__tests__/github_issues/376-test.ts index 8b05bc84..ac54506a 100644 --- a/src/__tests__/github_issues/376-test.ts +++ b/src/__tests__/github_issues/376-test.ts @@ -7,105 +7,140 @@ const schemaComposer = new SchemaComposer<{ req: any }>(); // mongoose.set('debug', true); -const UserSchema = new Schema( +export interface Profile { + _id: string; + emailAddress: string; + name: string; +} + +type ProfileDocType = Profile & mongoose.Document; +const ProfileSchema = new Schema( { - name: { + _id: { type: String, required: true, - trim: true, }, - orgId: { - type: mongoose.Schema.Types.ObjectId, - ref: 'Org', + emailAddress: { + type: String, required: true, - set: function (this: any, newOrgId: any) { - // temporarily store previous org so - // assignment to new org will work. - this._prevOrg = this.orgId; - return newOrgId; - }, + trim: true, }, - }, - { - collection: 'users', - timestamps: { - createdAt: 'created', - updatedAt: 'modified', + name: { + type: String, + required: true, + trim: true, }, } ); -const UserModel = mongoose.model('User', UserSchema); -const UserTC = composeMongoose(UserModel, { schemaComposer }); +const ProfileModel = mongoose.model('Profile', ProfileSchema); +composeMongoose(ProfileModel, { schemaComposer }); + +const PublicProfileTC = composeMongoose(ProfileModel, { + name: "PublicProfile", + onlyFields: ["_id", "name"], + schemaComposer +}); -const OrgSchema = new Schema( +interface Org { + _id: string; + name: string; + ownerId?: string; +} + +type OrgDocType = Org & mongoose.Document; +const OrgSchema = new Schema( { + _id: { + type: String, + required: true, + }, name: { type: String, required: true, trim: true, unique: true, }, - Users: [ - { - type: mongoose.Schema.Types.ObjectId, - ref: 'User', - }, - ], + ownerId: String, }, - { - collection: 'orgs', - timestamps: { - createdAt: 'created', - updatedAt: 'modified', - }, - } ); -const OrgModel = mongoose.model('Org', OrgSchema); -const OrgTC = composeMongoose(OrgModel, { schemaComposer }); +const OrgModel = mongoose.model('Org', OrgSchema); +const OrgTC = composeMongoose(OrgModel, { schemaComposer }); -UserTC.addRelation('org', { - resolver: () => OrgTC.mongooseResolvers.findById(), +OrgTC.addRelation('owner', { + resolver: () => PublicProfileTC.mongooseResolvers.findById({ lean: true }), prepareArgs: { // Define the args passed to the resolver (eg what the _id value should be) // Source is the filter passed to the user query - _id: (source) => { - // console.log(source); - return source.orgId; + _id: (org) => { + // console.log(org); + return org.ownerId; }, }, - projection: { orgId: true }, // Additional fields from UserSchema we need to pass to the Org resolver + projection: { ownerId: 1 }, // Additional fields from UserSchema we need to pass to the Org resolver }); schemaComposer.Query.addFields({ - users: UserTC.mongooseResolvers.findMany(), + orgFindOne: OrgTC.mongooseResolvers.findOne({ + lean: true, + filter: { + onlyIndexed: true, + }, + }), + orgs: OrgTC.mongooseResolvers.findMany(), }); const schema = schemaComposer.buildSchema(); beforeAll(async () => { await OrgModel.base.createConnection(); - const orgs = await OrgModel.create([ - { name: 'Organization1' }, - { name: 'Organization2' }, - { name: 'Organization3' }, - ]); - await UserModel.create([ - { name: 'User1', orgId: orgs[1]._id }, - { name: 'User2', orgId: orgs[2]._id }, + const profiles = [ + { _id: "1", emailAddress: "user1@example.com", name: 'User1' }, + { _id: "2", emailAddress: "user2@example.com", name: 'User2' }, + ]; + await ProfileModel.create(profiles); + await OrgModel.create([ + { _id: "1", name: 'Organization1' }, + { _id: "2", name: 'Organization2', ownerId: profiles[0]._id }, + { _id: "3", name: 'Organization3', ownerId: profiles[1]._id }, ]); }); afterAll(() => { OrgModel.base.disconnect(); }); -describe('issue #376 - Projection not being added to query in relation', () => { +describe('issue #376 - Projection not being added to query in relation with findOne', () => { + it('check', async () => { + const result = await graphql.graphql({ + schema, + source: `query($filter: FilterFindOneOrgInput) { + orgFindOne(filter: $filter) { + name + owner { + name + } + } + }`, + variableValues: { + _id: "2", + }, + }); + expect(result).toEqual({ + data: { + orgFindOne: + { name: 'Organization2', owner: { name: 'User1' } }, + }, + }); + }); +}); + +describe('issue #376 - Projection IS added to query in relation with findMany', () => { it('check', async () => { const result = await graphql.graphql({ schema, source: `query { - users(sort: _ID_ASC) { + orgs { name - org { + owner { name } } @@ -113,9 +148,10 @@ describe('issue #376 - Projection not being added to query in relation', () => { }); expect(result).toEqual({ data: { - users: [ - { name: 'User1', org: { name: 'Organization2' } }, - { name: 'User2', org: { name: 'Organization3' } }, + orgs: [ + { name: 'Organization1', owner: null }, + { name: 'Organization2', owner: { name: 'User1' } }, + { name: 'Organization3', owner: { name: 'User2' } }, ], }, }); diff --git a/yarn.lock b/yarn.lock index dc319e8f..daebe0c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2960,10 +2960,10 @@ graphql-compose-pagination@8.3.0: resolved "https://registry.yarnpkg.com/graphql-compose-pagination/-/graphql-compose-pagination-8.3.0.tgz#a58c786256b4dba1843d495ae81cb0fc67ac1369" integrity sha512-zZoBhlWD9KnAJ9AUOeFNeoE4Q/zMxZaxg0tR+HFMVNFnw7ENUnIChMWnD2uvmvik+k8Yv304hMF4n81okGVsJw== -graphql-compose@9.0.5: - version "9.0.5" - resolved "https://registry.yarnpkg.com/graphql-compose/-/graphql-compose-9.0.5.tgz#673f5ea0bc2f75b797095d93778cc0ce2adefd26" - integrity sha512-u4QujGRgk2u1Y+LmKxVDJ7AQB7gj2RBI3GPPUFJRXOZOc5DDUUVugSaTEtjo8otGnYtrNbmqjB2xzYMUkQ2/ZQ== +graphql-compose@9.0.8: + version "9.0.8" + resolved "https://registry.yarnpkg.com/graphql-compose/-/graphql-compose-9.0.8.tgz#920b6b0584f5e3784a532138974063d3807f7455" + integrity sha512-I3zvygpVz5hOWk2cYL6yhbgfKbNWbiZFNXlWkv/55U+lX6Y3tL+SyY3zunw7QWrN/qtwG2DqZb13SHTv2MgdEQ== dependencies: graphql-type-json "0.3.2"