Skip to content

Commit

Permalink
fix(Discriminators): copying base InputTypes to base resolver `ArgT…
Browse files Browse the repository at this point in the history
…ypes`

* test(discriminator): add more test cases for `mergeCustomizationOptions()`

* fix: fix copying base `InputTypes` to copying base resolver `ArgTypes`

Refactor: rename fn `setBaseInputTypesOnChildInputTypes` to `copyResolverArgTypes`

* test(discriminator): add more test cases for customization Options
  • Loading branch information
mernxl authored and nodkz committed Jul 18, 2018
1 parent 8fb2928 commit 88451fd
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 25 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/composeWithMongooseDiscriminators-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,21 @@ describe('composeWithMongooseDiscriminators ->', () => {
expect(itc.isRequired('name')).toBe(true);
expect(itc.isRequired('friends')).toBe(true);
});

it('should be passed down record opts to resolvers', () => {
const typeComposer = composeWithMongooseDiscriminators(CharacterModel, {
resolvers: {
createOne: {
record: {
removeFields: ['friends'],
requiredFields: ['name'],
},
},
},
});
const createOneRecordArgTC = typeComposer.getResolver('createOne').getArgTC('record');
expect(createOneRecordArgTC.isRequired('name')).toBe(true);
expect(createOneRecordArgTC.hasField('friends')).toBe(false);
});
});
});
34 changes: 33 additions & 1 deletion src/discriminators/__tests__/DiscriminatorTypeComposer-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* @flow */

import { schemaComposer, graphql, TypeComposer, InterfaceTypeComposer } from 'graphql-compose';
import {
SchemaComposer,
schemaComposer,
graphql,
TypeComposer,
InterfaceTypeComposer,
} from 'graphql-compose';
import { getCharacterModels } from '../__mocks__/characterModels';
import { MovieModel } from '../__mocks__/movieModel';
import { composeWithMongoose } from '../../composeWithMongoose';
Expand Down Expand Up @@ -498,5 +504,31 @@ describe('DiscriminatorTypeComposer', () => {

expect(tc.getFieldNames()).not.toEqual(expect.arrayContaining(['dob', 'starShips']));
});

it('should pass down baseTypeComposerResolverOptions', () => {
const personTC = composeWithMongooseDiscriminators(CharacterModel, {
schemaComposer: new SchemaComposer(),
resolvers: {
createOne: {
record: {
removeFields: ['friends'],
requiredFields: ['name'],
},
},
},
}).discriminator(PersonModel, {
resolvers: {
createOne: {
record: {
requiredFields: ['dob'],
},
},
},
});
const createOneRecordArgTC = personTC.getResolver('createOne').getArgTC('record');
expect(createOneRecordArgTC.isRequired('name')).toBe(true);
expect(createOneRecordArgTC.isRequired('dob')).toBe(true);
expect(createOneRecordArgTC.hasField('friends')).toBe(false);
});
});
});
37 changes: 37 additions & 0 deletions src/discriminators/__tests__/prepareChildResolvers-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* @flow */

import { schemaComposer } from 'graphql-compose';
import { getCharacterModels } from '../__mocks__/characterModels';
import { composeWithMongooseDiscriminators } from '../../composeWithMongooseDiscriminators';

const { CharacterModel, PersonModel } = getCharacterModels('type');

describe('prepareChildResolvers()', () => {
describe('copyResolverArgTypes()', () => {
afterAll(() => {
schemaComposer.clear();
});
// Note childResolver Arg fields are copied from baseResolver
const baseDTC = composeWithMongooseDiscriminators(CharacterModel, {
resolvers: {
createOne: {
requiredFields: ['kind'],
},
},
});
const PersonTC = baseDTC.discriminator(PersonModel);

it('should copy base common ResolverArgTypes to child', () => {
expect(
baseDTC
.getResolver('createOne')
.getArgTC('record')
.getFieldType('kind')
).toEqual(
PersonTC.getResolver('createOne')
.getArgTC('record')
.getFieldType('kind')
);
});
});
});
42 changes: 22 additions & 20 deletions src/discriminators/prepareChildResolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,32 @@ function hideDKey<TContext>(
}
}

// makes sure that all input fields are same as that on Interface,
// that is all should be same as base typeComposer types
// only changes for common properties, executed only once, on discriminator creation
function setBaseInputTypesOnChildInputTypes<TContext>(
// Set baseDTC resolver argTypes on childTC fields shared with DInterface
function copyResolverArgTypes<TContext>(
resolver: ResolverClass<any, TContext>,
baseDTC: DiscriminatorTypeComposer<TContext>,
fromField: string[] | string
fromArg: string[] | string
) {
// set sharedField types on input types
if (resolver && baseDTC.hasInputTypeComposer()) {
if (Array.isArray(fromField)) {
for (const field of fromField) {
setBaseInputTypesOnChildInputTypes(resolver, baseDTC, field);
if (Array.isArray(fromArg)) {
for (const field of fromArg) {
copyResolverArgTypes(resolver, baseDTC, field);
}
} else if (fromField && resolver.hasArg(fromField)) {
const argTc = resolver.getArgTC(fromField);

const baseITCFields = baseDTC.getInputTypeComposer().getFieldNames();

for (const baseField of baseITCFields) {
if (argTc.hasField(baseField) && baseField !== '_id') {
argTc.extendField(baseField, {
type: baseDTC.getInputTypeComposer().getFieldType(baseField),
});
} else if (fromArg && resolver.hasArg(fromArg)) {
if (
baseDTC.hasResolver(resolver.name) &&
baseDTC.getResolver(resolver.name).hasArg(fromArg)
) {
const childResolverArgTc = resolver.getArgTC(fromArg);
const baseResolverArgTC = baseDTC.getResolver(resolver.name).getArgTC(fromArg);
const baseResolverArgTCFields = baseResolverArgTC.getFieldNames();

for (const baseArgField of baseResolverArgTCFields) {
if (childResolverArgTc.hasField(baseArgField) && baseArgField !== '_id') {
childResolverArgTc.extendField(baseArgField, {
type: baseResolverArgTC.getFieldType(baseArgField),
});
}
}
}
}
Expand Down Expand Up @@ -167,7 +169,7 @@ export function prepareChildResolvers<TContext>(
default:
}

setBaseInputTypesOnChildInputTypes(resolver, baseDTC, ['filter', 'record']);
copyResolverArgTypes(resolver, baseDTC, ['filter', 'record']);
reorderFieldsRecordFilter(resolver, baseDTC, opts.reorderFields, ['filter', 'record']);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ describe('mergeCustomizationOptions()', () => {
removeFields: ['parent', 'child'],
},
},
updateById: {
record: {
removeFields: ['one', 'two'],
requiredFields: ['eight'],
},
},
findMany: {
limit: { defaultValue: 20 },
// sort: false,
Expand Down Expand Up @@ -191,9 +197,9 @@ describe('mergeCustomizationOptions()', () => {
},
},
updateById: {
input: {
removeFields: ['one', 'two', 'five'],
requiredFields: ['eight', 'two', 'five'],
record: {
removeFields: ['five'],
requiredFields: ['two', 'five'],
},
},
},
Expand Down Expand Up @@ -228,14 +234,24 @@ describe('mergeCustomizationOptions()', () => {
},
findById: false,
updateById: {
input: {
record: {
removeFields: ['one', 'two', 'five'],
requiredFields: ['eight', 'two', 'five'],
},
},
},
};

it('should return most base options if no child', () => {
expect((mergeCustomizationOptions(baseCustomOptions): any).resolvers).toEqual(
baseCustomOptions.resolvers
);
});

it('should return child if no base is found', () => {
expect(mergeCustomizationOptions({}, childCustomOptions)).toEqual(childCustomOptions);
});

it('should merge customisation Options', () => {
expect(mergeCustomizationOptions(baseCustomOptions, childCustomOptions)).toEqual(expected);
});
Expand Down

0 comments on commit 88451fd

Please sign in to comment.