Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tatejones committed Nov 3, 2021
1 parent b231b0c commit d2c8fd3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
48 changes: 43 additions & 5 deletions src/__tests__/integration-discriminators-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ afterAll(() => mongoose.disconnect());
describe('#78 Mongoose and Discriminators', () => {
const options = { discriminatorKey: 'kind' };

const eventSchema = new mongoose.Schema({ refId: String }, options);
const eventSchema = new mongoose.Schema(
{ refId: String, name: { type: String, index: true } },
options
);
const Event = mongoose.model('Event', eventSchema);

const clickedLinkSchema = new mongoose.Schema({ url: String });
const clickedLinkSchema = new mongoose.Schema({ url: { type: String, index: true } }, options);
const ClickedLinkEvent = Event.discriminator('ClickedLinkEvent', clickedLinkSchema);

const EventTC = composeWithMongooseDiscriminators(Event);
Expand All @@ -23,13 +26,48 @@ describe('#78 Mongoose and Discriminators', () => {
afterAll(() => Event.deleteMany({}));

it('creating Types from models', () => {
expect(EventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId']);
expect(ClickedLinkEventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId', 'url']);
expect(EventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId', 'name']);
expect(ClickedLinkEventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId', 'name', 'url']);
});

it('manually override resolver output type for findMany', async () => {
it('perform filter operation on a child model', async () => {
// let's check graphql response
await Event.deleteMany({});
await Event.create({ refId: 'aaa', name: 'aName' });
await Event.create({ refId: 'bbb', name: 'bName' });
await ClickedLinkEvent.create({ refId: 'ccc', name: 'cName', url: 'url1' });
await ClickedLinkEvent.create({ refId: 'ddd', name: 'dName', url: 'url2' });

schemaComposer.Query.addFields({
clickedLinkEventFindMany: ClickedLinkEventTC.getResolver('findMany'),
});

const schema = schemaComposer.buildSchema();

const res = await graphql.graphql(
schema,
`{
clickedLinkEventFindMany( filter: { AND: [ { _operators: { url: { in: [ "url1", "url2" ] } } }, { name: "dName" } ] }) {
__typename
refId
name
url
}
}`
);

expect(res).toEqual({
data: {
clickedLinkEventFindMany: [
{ __typename: 'ClickedLinkEvent', refId: 'ddd', name: 'dName', url: 'url2' },
],
},
});
});

it('manually override resolver output type for findMany', async () => {
// let's check graphql response
await Event.deleteMany({});
await Event.create({ refId: 'aaa' });
await Event.create({ refId: 'bbb' });
await ClickedLinkEvent.create({ refId: 'ccc', url: 'url1' });
Expand Down
18 changes: 18 additions & 0 deletions src/discriminators/__tests__/prepareChildResolvers-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { schemaComposer, ObjectTypeComposer } from 'graphql-compose';
import { composeWithMongooseDiscriminators } from '../../composeWithMongooseDiscriminators';
import { getCharacterModels } from '../__mocks__/characterModels';
import { OPERATORS_FIELDNAME } from '../../resolvers/helpers/filterOperators';

const DKeyFieldName = 'type';
const { CharacterModel, PersonModel } = getCharacterModels(DKeyFieldName);
Expand Down Expand Up @@ -116,5 +117,22 @@ describe('prepareChildResolvers()', () => {
PersonTC.getResolver('createOne').getArgITC('record').getFieldType('kind')
);
});

it('should not copy base _operations to child', () => {
expect(
baseDTC
.getResolver('findMany')
.getArgITC('filter')
.getField(OPERATORS_FIELDNAME)
.type.getTypeName()
).toEqual('FilterFindManyCharacterOperatorsInput');

expect(
PersonTC.getResolver('findMany')
.getArgITC('filter')
.getField(OPERATORS_FIELDNAME)
.type.getTypeName()
).toEqual('FilterFindManyPersonOperatorsInput');
});
});
});
6 changes: 5 additions & 1 deletion src/discriminators/prepareChildResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DiscriminatorTypeComposer,
} from './DiscriminatorTypeComposer';
import { resolverFactory } from '../resolvers';
import { OPERATORS_FIELDNAME } from '../resolvers/helpers/filterOperators';

// set the DKey as a query on filter, also project it
// Also look at it like setting for filters, makes sure to limit
Expand Down Expand Up @@ -85,7 +86,10 @@ function copyResolverArgTypes<TSource, TContext>(
const baseResolverArgTCFields = baseResolverArgTC.getFieldNames();

for (const baseArgField of baseResolverArgTCFields) {
if (childResolverArgTC.hasField(baseArgField) && baseArgField !== '_id') {
if (
childResolverArgTC.hasField(baseArgField) &&
['_id', OPERATORS_FIELDNAME, 'OR', 'AND'].indexOf(baseArgField) === -1
) {
childResolverArgTC.extendField(baseArgField, {
type: baseResolverArgTC.getField(baseArgField).type,
});
Expand Down

0 comments on commit d2c8fd3

Please sign in to comment.