Skip to content
This repository was archived by the owner on Apr 7, 2020. It is now read-only.

fix: set path to fieldName for resolver(#19) #20

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jest/common/parent.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Connection, Document, Model, Schema } from 'mongoose';

export function getParentModel(connection: Connection, child: Model<Document>) {
export function getParentModel(connection: Connection, child: Model<Document>, _name?: string) {

const name = 'ParentModel';
const name = _name || 'ParentModel';
if (connection.modelNames().includes(name)) return connection.model(name);

const ParentSchema = new Schema({
Expand All @@ -12,5 +12,5 @@ export function getParentModel(connection: Connection, child: Model<Document>) {
}
});

return connection.model('ParentModel', ParentSchema);
return connection.model(name, ParentSchema);
}
4 changes: 2 additions & 2 deletions jest/common/simple.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Connection, Schema } from 'mongoose';

export function getSimpleModel(connection: Connection) {
export function getSimpleModel(connection: Connection, _name?: string) {

const name = 'SimpleModel';
const name = _name || 'SimpleModel';
if (connection.modelNames().includes(name)) return connection.model(name);

const SimpleSchema = new Schema({
Expand Down
3 changes: 3 additions & 0 deletions jest/tc-complex-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export function complexRefCase(connection: Connection): ComplexRefTestCase {
}
});

const ClonedSchema = BazSchema.clone()
ComplexRefParentSchema.add({clones: [ClonedSchema]})

const simpleModel = getSimpleModel(connection);
simpleModel.discriminator(childModelName, ComplexRefChildSchema);

Expand Down
85 changes: 85 additions & 0 deletions jest/tc-multiple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { GraphQLObjectType, GraphQLSchema } from 'graphql';
import { Connection, Document, Model, models } from 'mongoose';
import { getParentModel } from './common/parent.model';
import { getSimpleModel } from './common/simple.model';
import { simpleResponse } from './common/simple.response';
import { SimpleType } from './common/simple.type';
import { TestCase } from './tc';

interface MultipleTestCase extends Omit<TestCase, 'model'> {
firstModel: Model<Document>
secondModel: Model<Document>
firstParentModelName:string
secondParentModelName:string
firstChildModelName:string
secondChildModelName:string
}

/**
* A Ref Test Case.
*
* @param connection Mongoose Connection
* @returns Test Case
*/
export function multipleCase(connection: Connection): MultipleTestCase {

const firstSimpleModel = getSimpleModel(connection, 'first');
const firstChildModelName = firstSimpleModel.modelName;
const firstModel = getParentModel(connection, firstSimpleModel, 'FirstParent');
const firstParentModelName = firstModel.modelName;

const secondSimpleModel = getSimpleModel(connection, 'second');
const secondChildModelName = secondSimpleModel.modelName;
const secondModel = getParentModel(connection, secondSimpleModel, 'SecondParent');
const secondParentModelName = secondModel.modelName;

const response = {
child: { ...simpleResponse }
};

const FirstType: GraphQLObjectType = new GraphQLObjectType({
name: 'FirstType',
fields: () => ({
child: {
type: SimpleType
}
})
});

const SecondType: GraphQLObjectType = new GraphQLObjectType({
name: 'SecondType',
fields: () => ({
child: {
type: SimpleType
}
})
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'MultipleQueries',
fields: () => ({
first: {
type: FirstType,
resolve: () => response
},
second: {
type: SecondType,
resolve: () => response
}
})
})
});


return {
firstModel,
secondModel,
response,
schema,
firstParentModelName,
secondParentModelName,
firstChildModelName,
secondChildModelName
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nicky-lenaers/mogr",
"version": "1.6.0",
"version": "1.6.1",
"description": "MoGr dynamically maps GraphQL AST's to Mongoose Query Projection and/or Population and provides GraphQL Cursor Pagination.",
"main": "lib/index.js",
"scripts": {
Expand Down
43 changes: 43 additions & 0 deletions src/populate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { refCase } from '../jest/tc-ref';
import { refSelected } from '../jest/tc-ref-selected';
import { inlineFragmentRefCase } from '../jest/ts-inline-fragment-ref';
import { Registry } from './registry';
import { multipleCase } from '../jest/tc-multiple';

describe('populate', () => {

Expand Down Expand Up @@ -75,6 +76,48 @@ describe('populate', () => {
expect(population).toEqual([{ path: 'child', populate: [], select: ['id', 'foo', 'bar'] }]);
});

it('should handle multiple GraphQL Fields', async () => {

const tc = multipleCase(connection);
let firstpopulation: ModelPopulateOptions[];
let secondpopulation: ModelPopulateOptions[];

const server = mockServer(tc.schema, {
FirstType: (...args) => {
const info = args[args.length - 1];
firstpopulation = registry.populate(info, tc.firstModel.modelName);
return tc.response;
},
SecondType: (...args) => {
const info = args[args.length - 1];
secondpopulation = registry.populate(info, tc.secondModel.modelName);
return tc.response;
}
});

await server.query(`
query multiple {
first {
child {
foo
}
}
second {
child {
bar
}
}
}
`);

expect(firstpopulation).toEqual([
{ path: 'child', populate: [], select: ['id', 'foo'] }
]);
expect(secondpopulation).toEqual([
{ path: 'child', populate: [], select: ['id', 'bar'] }
]);
});

it('should handle complex GraphQL Fields', async () => {

const tc = complexRefCase(connection);
Expand Down
13 changes: 10 additions & 3 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ export class Registry {
*/
private _getSelections(info: GraphQLResolveInfo, root: string) {

const operationSelection: SelectionSetNode =
(info.operation.selectionSet.selections[0] as FieldNode)
.selectionSet as SelectionSetNode;
const operationSelection: SelectionSetNode = root
? ((info.operation.selectionSet.selections[0] as FieldNode)
.selectionSet as SelectionSetNode)
: ((info.operation.selectionSet.selections.find(
selection => (selection as FieldNode).name.value === info.fieldName
) as FieldNode).selectionSet as SelectionSetNode);

return this._getSelectionsOffsetByPath(
operationSelection,
Expand Down Expand Up @@ -180,6 +183,10 @@ export class Registry {
fields: PopulatableField[] = []
): PopulatableField[] {

if (!type) {
return fields
}

if (type.ref && typeof type.ref === 'string') {

if (!fields.map(field => field.path).includes(path)) {
Expand Down