Skip to content

Commit

Permalink
feat: already generated TC may be obtained via schemaComposer.getTC(m…
Browse files Browse the repository at this point in the history
…ongooseSchema)

Removed old logic, where generated schemas stored in the mongoose object 
`model.schema._gqcTypeComposer`.
Related: 
#26 (comment)
  • Loading branch information
nodkz committed Jul 10, 2018
1 parent 86f05c9 commit 34e756d
Show file tree
Hide file tree
Showing 18 changed files with 25 additions and 35 deletions.
10 changes: 8 additions & 2 deletions src/__tests__/composeWithMongoose-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ afterAll(() => UserModel.base.disconnect());
describe('composeWithMongoose ->', () => {
beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
});

describe('MongooseModelToTypeComposer()', () => {
Expand All @@ -27,7 +26,7 @@ describe('composeWithMongoose ->', () => {
it('should set type name from model or opts.name', () => {
expect(composeWithMongoose(UserModel).getTypeName()).toBe(UserModel.modelName);

UserModel.schema._gqcTypeComposer = undefined;
schemaComposer.clear();
expect(composeWithMongoose(UserModel, { name: 'Ok' }).getTypeName()).toBe('Ok');
});

Expand All @@ -48,6 +47,13 @@ describe('composeWithMongoose ->', () => {
expect(tc.getFieldType('_id')).toBeInstanceOf(GraphQLNonNull);
expect(tc.getFieldType('_id').ofType).toBe(GraphQLMongoID);
});

it('composeWithMongoose should generate new TypeComposer (without cache)', () => {
const tc1: any = composeWithMongoose(UserModel);
schemaComposer.clear();
const tc2: any = composeWithMongoose(UserModel);
expect(tc1).not.toBe(tc2);
});
});

describe('filterFields()', () => {
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ afterAll(() => UserModel.base.disconnect());
describe('integration tests', () => {
beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
});

describe('check subdocuments', () => {
Expand Down
30 changes: 17 additions & 13 deletions src/fieldsConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type ComposeScalarType = string | GraphQLScalarType;
type ComposeOutputType = TypeComposer | ComposeScalarType | EnumTypeComposer | [ComposeOutputType];

export type MongoosePseudoModelT = {
_gqcTypeComposer?: TypeComposer,
schema: Schema<any>,
};

Expand Down Expand Up @@ -117,19 +116,21 @@ export function convertModelToGraphQL(
): TypeComposer {
const schemaComposer = sc || globalSchemaComposer;

// if model already has generated TypeComposer early, then return it
const modelSchema: any = model.schema;
if (modelSchema && modelSchema._gqcTypeComposer) {
return modelSchema._gqcTypeComposer;
}

if (!typeName) {
throw new Error('You provide empty name for type. `name` argument should be non-empty string.');
}

const typeComposer = schemaComposer.getOrCreateTC(typeName);
// if model already has generated TypeComposer early, then return it
// $FlowFixMe await landing [email protected] or above
if (schemaComposer.has(model.schema)) {
// $FlowFixMe await landing [email protected] or above
return schemaComposer.getTC(model.schema);
}

modelSchema._gqcTypeComposer = typeComposer; // eslint-disable-line no-param-reassign
const typeComposer = schemaComposer.getOrCreateTC(typeName);
// $FlowFixMe await landing [email protected] or above
schemaComposer.set(model.schema, typeComposer);
schemaComposer.set(typeName, typeComposer);

const mongooseFields = getFieldsFromModel(model);
const graphqlFields = {};
Expand Down Expand Up @@ -161,7 +162,7 @@ export function convertModelToGraphQL(
}

export function convertSchemaToGraphQL(
schema: Object, // MongooseModelSchemaT, TODO use Model from mongoose_v4.x.x definition when it will be public
schema: Schema<any>,
typeName: string,
sc?: SchemaComposer<any>
): TypeComposer {
Expand All @@ -171,15 +172,18 @@ export function convertSchemaToGraphQL(
throw new Error('You provide empty name for type. `name` argument should be non-empty string.');
}

if (schema._gqcTypeComposer) {
return schema._gqcTypeComposer;
// $FlowFixMe await landing [email protected] or above
if (schemaComposer.has(schema)) {
// $FlowFixMe await landing [email protected] or above
return schemaComposer.getTC(schema);
}

const tc = convertModelToGraphQL({ schema }, typeName, schemaComposer);
// also generate InputType
tc.getInputTypeComposer();

schema._gqcTypeComposer = tc; // eslint-disable-line
// $FlowFixMe await landing [email protected] or above
schemaComposer.set(schema, tc);
return tc;
}

Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/count-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('count() ->', () => {
let UserTC;

beforeEach(() => {
UserModel.schema._gqcTypeComposer = undefined;
schemaComposer.clear();
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/createOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('createOne() ->', () => {
let UserTC;

beforeEach(() => {
UserModel.schema._gqcTypeComposer = undefined;
schemaComposer.clear();
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
UserTC.setRecordIdFn(source => (source ? `${source._id}` : ''));
Expand Down
3 changes: 0 additions & 3 deletions src/resolvers/__tests__/findById-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ describe('findById() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);

PostModel.schema._gqcTypeComposer = undefined;
PostTypeComposer = convertModelToGraphQL(PostModel, 'Post', schemaComposer);
});

Expand Down
3 changes: 0 additions & 3 deletions src/resolvers/__tests__/findByIds-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ describe('findByIds() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);

PostModel.schema._gqcTypeComposer = undefined;
PostTypeComposer = convertModelToGraphQL(PostModel, 'Post', schemaComposer);
});

Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/findMany-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('findMany() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});

Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/findOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ let UserTC;

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});

Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/removeById-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('removeById() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
UserTC.setRecordIdFn(source => (source ? `${source._id}` : ''));
});
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/removeMany-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('removeMany() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});

Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/removeOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('removeOne() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
UserTC.setRecordIdFn(source => (source ? `${source._id}` : ''));
});
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/updateById-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('updateById() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
UserTC.setRecordIdFn(source => (source ? `${source._id}` : ''));
});
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/updateMany-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('updateMany() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});

Expand Down
1 change: 0 additions & 1 deletion src/resolvers/__tests__/updateOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('updateOne() ->', () => {

beforeEach(() => {
schemaComposer.clear();
UserModel.schema._gqcTypeComposer = undefined;
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
UserTC.setRecordIdFn(source => (source ? `${source._id}` : ''));
});
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/helpers/__tests__/filter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('Resolver helper `filter` ->', () => {
let UserTC: TypeComposer;

beforeEach(() => {
UserModel.schema._gqcTypeComposer = undefined;
schemaComposer.clear();
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/helpers/__tests__/record-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe('Resolver helper `record` ->', () => {
let UserTC: TypeComposer;

beforeEach(() => {
UserModel.schema._gqcTypeComposer = undefined;
schemaComposer.clear();
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});
Expand Down
1 change: 0 additions & 1 deletion src/resolvers/helpers/__tests__/sort-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe('Resolver helper `sort` ->', () => {
let UserTC: TypeComposer;

beforeEach(() => {
UserModel.schema._gqcTypeComposer = undefined;
schemaComposer.clear();
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
});
Expand Down

0 comments on commit 34e756d

Please sign in to comment.