Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Test for Issue 376 to Produce Error #403

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
150 changes: 93 additions & 57 deletions src/__tests__/github_issues/376-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,115 +7,151 @@ 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<ProfileDocType>(
{
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<any>('User', UserSchema);
const UserTC = composeMongoose(UserModel, { schemaComposer });
const ProfileModel = mongoose.model<ProfileDocType>('Profile', ProfileSchema);
composeMongoose<ProfileDocType>(ProfileModel, { schemaComposer });

const PublicProfileTC = composeMongoose<ProfileDocType>(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<OrgDocType>(
{
_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<any>('Org', OrgSchema);
const OrgTC = composeMongoose(OrgModel, { schemaComposer });
const OrgModel = mongoose.model<OrgDocType>('Org', OrgSchema);
const OrgTC = composeMongoose<OrgDocType>(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
}
}
}`,
});
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' } },
],
},
});
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down