Skip to content

Instantly share code, notes, and snippets.

@AugustoCalaca
Forked from sibelius/getObjectId.tsx
Last active May 1, 2020 02:31
Show Gist options
  • Save AugustoCalaca/642a99a4d0eab15557009099074036bf to your computer and use it in GitHub Desktop.
Save AugustoCalaca/642a99a4d0eab15557009099074036bf to your computer and use it in GitHub Desktop.
getObjectId from globalId
import { toGlobalId } from 'graphql-relay';
import { Types } from 'mongoose';
import { getObjectId } from './getObjectId';
import {
clearDbAndRestartCounters,
connectMongoose,
createUser,
disconnectMongoose
} from '../../../test/helpers';
beforeAll(connectMongoose);
beforeEach(clearDbAndRestartCounters);
afterAll(disconnectMongoose);
describe('getObjectId', () => {
it('should return an ObjectId when target is ObjectId', async () => {
const user = await createUser();
const result = getObjectId(user._id);
expect(result instanceof Types.ObjectId).toBe(true);
expect(result.equals(user._id)).toBe(true);
});
it('should return an ObjectId when target is Document', async () => {
const user = await createUser();
const result = getObjectId(user);
expect(result instanceof Types.ObjectId).toBe(true);
expect(result.equals(user._id)).toBe(true);
});
it('should return an ObjectId when target is a GlobalId', async () => {
const user = await createUser();
const result = getObjectId(toGlobalId('User', user._id));
expect(result instanceof Types.ObjectId).toBe(true);
expect(result.equals(user._id)).toBe(true);
});
it('should return an ObjectId from a getObjectId getObjectId', async () => {
const user = await createUser();
const result = getObjectId(getObjectId(toGlobalId('User', user._id)));
expect(result instanceof Types.ObjectId).toBe(true);
expect(result.equals(user._id)).toBe(true);
});
it('should return null on invalid ObjectId', () => {
expect(getObjectId('invalid string')).toBe(null);
});
it('should return null on invalid Object', () => {
const invalidDoc = { name: 'invalid' };
expect(getObjectId(invalidDoc)).toBe(null);
});
it('should return null on invalid GlobalId', () => {
const result = getObjectId(toGlobalId('User', 'invalid id'));
expect(result).toBe(null);
});
});
import { fromGlobalId } from 'graphql-relay';
import { Model, Types } from 'mongoose';
// returns an ObjectId given an param of unknown type
export const getObjectId = (target: string | Model<any> | Types.ObjectId): Types.ObjectId | null => {
if (target instanceof Types.ObjectId) {
return new Types.ObjectId(target.toString());
}
if (typeof target === 'object') {
return target && target._id ? new Types.ObjectId(target._id) : null;
}
if (Types.ObjectId.isValid(target)) {
return new Types.ObjectId(target.toString());
}
if (typeof target === 'string') {
const result = fromGlobalId(target);
if (result.type && result.id && Types.ObjectId.isValid(result.id)) {
return new Types.ObjectId(result.id);
}
if (Types.ObjectId.isValid(target)) {
return new Types.ObjectId(target);
}
return null;
}
return null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment