Skip to content

Commit

Permalink
convertModelToGraphQL method now return TypeComposer
Browse files Browse the repository at this point in the history
other small fixes
  • Loading branch information
nodkz committed Jul 18, 2016
1 parent 89a9fd7 commit bf0a460
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
14 changes: 10 additions & 4 deletions src/composeWithMongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export function composeWithMongoose(
): TypeComposer {
const name: string = (opts && opts.name) || model.modelName;

const type = convertModelToGraphQL(model, name);
const typeComposer = new TypeComposer(type);
const typeComposer = convertModelToGraphQL(model, name);

if (opts.description) {
typeComposer.setDescription(opts.description);
Expand All @@ -33,7 +32,8 @@ export function composeWithMongoose(
prepareFields(typeComposer, opts.fields);
}

typeComposer.setRecordIdFn((source) => `${source._id}`);
// $FlowFixMe
typeComposer.setRecordIdFn(source => (source ? `${source._id}` : ''));

createInputType(typeComposer, opts.inputType);

Expand Down Expand Up @@ -125,7 +125,7 @@ export function createResolvers(
}
});

if (!opts.hasOwnProperty('connection') || opts.connection !== false) {
if ((!opts.hasOwnProperty('connection') || opts.connection !== false) && opts.connection) {
prepareConnectionResolver(typeComposer, opts.connection);
}
}
Expand All @@ -142,27 +142,33 @@ export function prepareConnectionResolver(
uniqueFields: ['_id'],
sortValue: { _id: -1 },
directionFilter: (filter, cursorData, isBefore) => {
// $FlowFixMe
filter[OPERATORS_FIELDNAME] = filter[OPERATORS_FIELDNAME] || {};
// $FlowFixMe
filter[OPERATORS_FIELDNAME]._id = filter[OPERATORS_FIELDNAME]._id || {};
if (isBefore) {
filter[OPERATORS_FIELDNAME]._id.gt = cursorData._id;
} else {
filter[OPERATORS_FIELDNAME]._id.lt = cursorData._id;
}
// $FlowFixMe
return filter;
},
},
_ID_ASC: {
uniqueFields: ['_id'],
sortValue: { _id: 1 },
directionFilter: (filter, cursorData, isBefore) => {
// $FlowFixMe
filter[OPERATORS_FIELDNAME] = filter[OPERATORS_FIELDNAME] || {};
// $FlowFixMe
filter[OPERATORS_FIELDNAME]._id = filter[OPERATORS_FIELDNAME]._id || {};
if (isBefore) {
filter[OPERATORS_FIELDNAME]._id.lt = cursorData._id;
} else {
filter[OPERATORS_FIELDNAME]._id.gt = cursorData._id;
}
// $FlowFixMe
return filter;
},
},
Expand Down
33 changes: 13 additions & 20 deletions src/fieldsConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ export function getFieldsFromModel(model: MongooseModelT): MongooseFieldMapT {
export function convertModelToGraphQL(
model: MongooseModelT,
typeName: string
): GraphQLOutputType {
): TypeComposer {
if (!typeName) {
throw new Error('You provide empty name for type. '
+ '`typeName` argument should be non-empty string.');
+ '`name` argument should be non-empty string.');
}

const typeComposer = new TypeComposer(
Expand All @@ -147,7 +147,7 @@ export function convertModelToGraphQL(
});

typeComposer.addFields(graphqlFields);
return typeComposer.getType();
return typeComposer;
}

export function convertFieldToGraphQL(
Expand Down Expand Up @@ -195,17 +195,14 @@ export function deriveComplexType(field: MongooseFieldT): ComplexTypesT {
return ComplexTypes.SCALAR;
}

function removePseudoIdField(gqType: GraphQLObjectType): GraphQLObjectType {
function removePseudoIdField(typeComposer: TypeComposer): void {
// remove pseudo object id mongoose field
const composer = new TypeComposer(gqType);
const gqFields = composer.getFields();
const gqFields = typeComposer.getFields();
Object.keys(gqFields).forEach(name => {
if (gqFields[name].type === GraphQLMongoID) {
composer.removeField(name);
typeComposer.removeField(name);
}
});

return composer.getType();
}

export function scalarToGraphQL(field: MongooseFieldT): GraphQLOutputType {
Expand Down Expand Up @@ -254,13 +251,10 @@ export function embeddedToGraphQL(
const typeName = `${prefix}${capitalize(fieldName)}`;
// $FlowFixMe
const fieldAsModel: MongooseModelT = field;
let gqType = convertModelToGraphQL(fieldAsModel, typeName);

if (gqType instanceof GraphQLObjectType) {
gqType = removePseudoIdField(gqType);
}
const typeComposer = convertModelToGraphQL(fieldAsModel, typeName);
removePseudoIdField(typeComposer);

return gqType;
return typeComposer.getType();
}


Expand Down Expand Up @@ -298,11 +292,10 @@ export function documentArrayToGraphQL(

const typeName = `${prefix}${capitalize(_getFieldName(field))}`;

let outputType = convertModelToGraphQL(field, typeName);
if (outputType instanceof GraphQLObjectType) {
outputType = removePseudoIdField(outputType);
}
return new GraphQLList(outputType);
const typeComposer = convertModelToGraphQL(field, typeName);
removePseudoIdField(typeComposer);

return new GraphQLList(typeComposer.getType());
}


Expand Down
18 changes: 10 additions & 8 deletions src/resolvers/helpers/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,16 @@ export function addFieldsWithOperator(
operators = availableOperators;
}
operators.forEach(operatorName => {
if (operatorName.slice(-2) === '[]') {
fields[operatorName.slice(0, -2)] = {
...existedFields[fieldName],
// $FlowFixMe
type: new GraphQLList(getNamedType(existedFields[fieldName].type)),
};
} else {
fields[operatorName] = getNamedType(existedFields[fieldName]);
const namedType = getNamedType(existedFields[fieldName].type);
if (namedType) {
if (operatorName.slice(-2) === '[]') {
fields[operatorName.slice(0, -2)] = {
...existedFields[fieldName],
type: new GraphQLList(namedType),
};
} else {
fields[operatorName] = namedType;
}
}
});
if (Object.keys(fields).length > 0) {
Expand Down

0 comments on commit bf0a460

Please sign in to comment.