Skip to content

Commit

Permalink
refactor: rename generateResolver to mongooseResolvers
Browse files Browse the repository at this point in the history
If our app is using different data sources so visibility will be greatly improved for developers with `UserTC.mongooseResolvers.find()`, `UserTC.elasticResovers.find()`.

see #263 (comment)
  • Loading branch information
nodkz committed Sep 12, 2020
1 parent 1a54aaf commit a136386
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
56 changes: 28 additions & 28 deletions src/__tests__/github_issues/141-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const UserModel = mongoose.model<IUser>('User', UserSchema);
const UserTC = composeMongoose(UserModel, { schemaComposer });

schemaComposer.Query.addFields({
userById: UserTC.generateResolver.findById(),
userFindOne: UserTC.generateResolver.findOne(),
userById: UserTC.mongooseResolvers.findById(),
userFindOne: UserTC.mongooseResolvers.findOne(),
});
schemaComposer.Mutation.addFields({
userCreateOne: UserTC.generateResolver.createOne(),
userUpdateById: UserTC.generateResolver.updateById(),
userCreateOne: UserTC.mongooseResolvers.createOne(),
userUpdateById: UserTC.mongooseResolvers.updateById(),
});

// const schema = schemaComposer.buildSchema();
Expand All @@ -48,28 +48,28 @@ describe('issue #141 - createOne with custom id (not MongoId)', () => {
});

it('Resolvers *ById should have Int type for args._id', () => {
expect(UserTC.generateResolver.findById().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.generateResolver.findByIdLean().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.generateResolver.removeById().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.generateResolver.updateById().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.mongooseResolvers.findById().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.mongooseResolvers.findByIdLean().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.mongooseResolvers.removeById().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.mongooseResolvers.updateById().getArgTypeName('_id')).toBe('Int!');

expect(UserTC.generateResolver.findByIds().getArgTypeName('_ids')).toBe('[Int!]!');
expect(UserTC.generateResolver.findByIdsLean().getArgTypeName('_ids')).toBe('[Int!]!');
expect(UserTC.mongooseResolvers.findByIds().getArgTypeName('_ids')).toBe('[Int!]!');
expect(UserTC.mongooseResolvers.findByIdsLean().getArgTypeName('_ids')).toBe('[Int!]!');
});

it('Resolvers dataLoader* should have Int type for args._id', () => {
expect(UserTC.generateResolver.dataLoader().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.generateResolver.dataLoaderLean().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.generateResolver.dataLoaderMany().getArgTypeName('_ids')).toBe('[Int!]!');
expect(UserTC.generateResolver.dataLoaderManyLean().getArgTypeName('_ids')).toBe('[Int!]!');
expect(UserTC.mongooseResolvers.dataLoader().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.mongooseResolvers.dataLoaderLean().getArgTypeName('_id')).toBe('Int!');
expect(UserTC.mongooseResolvers.dataLoaderMany().getArgTypeName('_ids')).toBe('[Int!]!');
expect(UserTC.mongooseResolvers.dataLoaderManyLean().getArgTypeName('_ids')).toBe('[Int!]!');
});

it('Check createOne/findOne resolvers', async () => {
UserTC.generateResolver.createOne();
UserTC.mongooseResolvers.createOne();

expect(
await testFieldConfig({
field: UserTC.generateResolver.createOne({
field: UserTC.mongooseResolvers.createOne({
suffix: 'WithId',
record: {
removeFields: [], // <-- empty array allows to override removing _id arg
Expand All @@ -89,7 +89,7 @@ describe('issue #141 - createOne with custom id (not MongoId)', () => {

expect(
await testFieldConfig({
field: UserTC.generateResolver.findById(),
field: UserTC.mongooseResolvers.findById(),
args: {
_id: 15,
},
Expand Down Expand Up @@ -132,26 +132,26 @@ describe('issue #141 - createOne with custom id (not MongoId)', () => {
zone: String
}"
`);
expect(ComplexTC.generateResolver.findById().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.generateResolver.findByIdLean().getArgTypeName('_id')).toBe(
expect(ComplexTC.mongooseResolvers.findById().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.mongooseResolvers.findByIdLean().getArgTypeName('_id')).toBe(
'Complex_idInput!'
);
expect(ComplexTC.generateResolver.removeById().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.generateResolver.updateById().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.generateResolver.findByIds().getArgTypeName('_ids')).toBe(
expect(ComplexTC.mongooseResolvers.removeById().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.mongooseResolvers.updateById().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.mongooseResolvers.findByIds().getArgTypeName('_ids')).toBe(
'[Complex_idInput!]!'
);
expect(ComplexTC.generateResolver.findByIdsLean().getArgTypeName('_ids')).toBe(
expect(ComplexTC.mongooseResolvers.findByIdsLean().getArgTypeName('_ids')).toBe(
'[Complex_idInput!]!'
);
expect(ComplexTC.generateResolver.dataLoader().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.generateResolver.dataLoaderLean().getArgTypeName('_id')).toBe(
expect(ComplexTC.mongooseResolvers.dataLoader().getArgTypeName('_id')).toBe('Complex_idInput!');
expect(ComplexTC.mongooseResolvers.dataLoaderLean().getArgTypeName('_id')).toBe(
'Complex_idInput!'
);
expect(ComplexTC.generateResolver.dataLoaderMany().getArgTypeName('_ids')).toBe(
expect(ComplexTC.mongooseResolvers.dataLoaderMany().getArgTypeName('_ids')).toBe(
'[Complex_idInput!]!'
);
expect(ComplexTC.generateResolver.dataLoaderManyLean().getArgTypeName('_ids')).toBe(
expect(ComplexTC.mongooseResolvers.dataLoaderManyLean().getArgTypeName('_ids')).toBe(
'[Complex_idInput!]!'
);

Expand All @@ -161,7 +161,7 @@ describe('issue #141 - createOne with custom id (not MongoId)', () => {
});
expect(
await testFieldConfig({
field: ComplexTC.generateResolver.findById(),
field: ComplexTC.mongooseResolvers.findById(),
args: { _id: { region: 'us-west', zone: 'a' } },
selection: `{
_id {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/github_issues/263-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ const UserTC = composeMongoose(UserModel);
const PostTC = composeMongoose(PostModel);

PostTC.addRelation('author', {
resolver: UserTC.generateResolver.dataLoaderLean(),
resolver: UserTC.mongooseResolvers.dataLoaderLean(),
prepareArgs: {
_id: (s) => s.authorId,
},
projection: { authorId: true },
});

PostTC.addRelation('reviewers', {
resolver: UserTC.generateResolver.dataLoaderManyLean(),
resolver: UserTC.mongooseResolvers.dataLoaderManyLean(),
prepareArgs: {
_ids: (s) => s.reviewerIds,
},
projection: { reviewerIds: true },
});

schemaComposer.Query.addFields({
posts: PostTC.generateResolver.findMany(),
posts: PostTC.mongooseResolvers.findMany(),
});
const schema = schemaComposer.buildSchema();

Expand Down
8 changes: 4 additions & 4 deletions src/composeMongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function composeMongoose<TDoc extends Document, TContext = any>(
model: Model<TDoc>,
opts: ComposeMongooseOpts<TContext> = {}
): ObjectTypeComposer<TDoc, TContext> & {
generateResolver: GenerateResolverType<TDoc, TContext>;
mongooseResolvers: GenerateResolverType<TDoc, TContext>;
} {
const m: Model<any> = model;
const name: string = (opts && opts.name) || m.modelName;
Expand Down Expand Up @@ -82,11 +82,11 @@ export function composeMongoose<TDoc extends Document, TContext = any>(

tc.makeFieldNonNull('_id');

const generateResolver = {} as any;
const mongooseResolvers = {} as any;
Object.keys(allResolvers).forEach((name) => {
generateResolver[name] = (allResolvers as any)[name].bind(undefined, model, tc);
mongooseResolvers[name] = (allResolvers as any)[name].bind(undefined, model, tc);
});
(tc as any).generateResolver = generateResolver;
(tc as any).mongooseResolvers = mongooseResolvers;

return tc as any;
}

0 comments on commit a136386

Please sign in to comment.