Skip to content

Commit

Permalink
init tests + projection tests (OS-Guild#29)
Browse files Browse the repository at this point in the history
* init tests + projection tests

* correct travis node version to supported functionality
  • Loading branch information
yoavkarako authored Sep 27, 2018
1 parent 3f85630 commit 86f0df7
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ index.ts
.gitignore
.travis.yml
CHANGELOG.md
tsconfig.json
tsconfig.json
tests/
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: node_js
node_js:
- 6
- 5
install:
- npm install
before_script:
- npm install -g typescript
script:
- npm run build
- npm run build
- npm run test
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getMongoDbUpdate from './src/mongoDbUpdate';
import GraphQLPaginationType from './src/graphQLPaginationType';
import getGraphQLSortType from './src/graphQLSortType';
import getMongoDbSort from './src/mongoDbSort';
import getMongoDbProjection from './src/mongoDbProjection';
import { getMongoDbProjection } from './src/mongoDbProjection';
import { getMongoDbQueryResolver, getGraphQLQueryArgs, QueryOptions } from './src/queryResolver';
import { getMongoDbUpdateResolver, getGraphQLUpdateArgs, UpdateOptions } from './src/updateResolver';
import { setLogger } from './src/logger';
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc -p .",
"publish-now": "npm run build && npm publish"
"publish-now": "npm run build && npm publish",
"test": "ts-mocha tests/**/*.spec.ts"
},
"repository": {
"type": "git",
Expand All @@ -19,7 +20,14 @@
},
"homepage": "https://github.com/Soluto/graphql-to-mongodb#readme",
"devDependencies": {
"@types/graphql": "^0.12.4"
"@types/chai": "^4.1.4",
"@types/graphql": "^0.12.4",
"@types/mocha": "^5.2.5",
"chai": "^4.1.2",
"graphql": "^0.13.2",
"mocha": "^5.2.0",
"ts-mocha": "^2.0.0",
"typescript": "^3.0.3"
},
"peerDependencies": {
"graphql": ">=0.10"
Expand Down
17 changes: 10 additions & 7 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ export function setLogger(loggerObject: Logger): void {
logger = loggerObject || {};
}

export function logOnError<T>(action: () => T): T {
try {
return action();
} catch (exception) {
error('graphql-to-mongodb internal exception:', exception);
throw exception;
}
export function logOnError<T>(func: T): T {
const wrappedFunction = (...args) => {
try {
return (func as any)(...args);
} catch (exception) {
error('graphql-to-mongodb internal exception:', exception);
throw exception;
}
};
return wrappedFunction as any;
}
8 changes: 1 addition & 7 deletions src/mongoDbFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ const operatorsMongoDbKeys = {
OPTIONS: '$options',
};

function getMongoDbFilterOuter(graphQLType: GraphQLObjectType, graphQLFilter: object = {}): object {
return logOnError(() => {
return getMongoDbFilter(graphQLType, graphQLFilter);
});
}

function getMongoDbFilter(graphQLType: GraphQLObjectType, graphQLFilter: object = {}): object {
if (!isType(graphQLType)) throw 'First arg of getMongoDbFilter must be the base graphqlType to be parsed'

Expand Down Expand Up @@ -120,4 +114,4 @@ function parseMongoDbScalarFilter(graphQLFilter: object): object {
return mongoDbScalarFilter;
}

export default getMongoDbFilterOuter;
export default logOnError(getMongoDbFilter);
41 changes: 21 additions & 20 deletions src/mongoDbProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface MongoDbProjection {
[key: string]: 1
};

interface Field {
export interface Field {
[key: string]: Field | 1
}

Expand All @@ -18,20 +18,23 @@ interface FragmentDictionary {
[key: string]: FieldGraph[]
}

function getMongoDbProjection(info: GraphQLResolveInfo, graphQLType: GraphQLObjectType, ...excludedFields: string[]): MongoDbProjection {
return logOnError(() => {
if (!Object.keys(info).includes('fieldNodes')) throw 'First argument of "getMongoDbProjection" must be a GraphQLResolveInfo';
if (!isType(graphQLType)) throw 'Second argument of "getMongoDbProjection" must be a GraphQLType';
export const getMongoDbProjection = logOnError((info: GraphQLResolveInfo, graphQLType: GraphQLObjectType, ...excludedFields: string[]): MongoDbProjection => {
if (!Object.keys(info).includes('fieldNodes')) throw 'First argument of "getMongoDbProjection" must be a GraphQLResolveInfo';
if (!isType(graphQLType)) throw 'Second argument of "getMongoDbProjection" must be a GraphQLType';

const selections = flatten(info.fieldNodes.map(_ => _.selectionSet.selections));
const simplifiedNodes = simplifyNodes({ selections: selections }, info);
const field = mergeNodes(simplifiedNodes);
const requestedFields = getRequestedFields(info);

const projection = getProjection(field, graphQLType, [], ...excludedFields);
const projection = getProjection(requestedFields, graphQLType, [], ...excludedFields);

const resolveFieldsDependencies = getResolveFieldsDependencies(field, graphQLType);
return mergeProjectionAndResolveDependencies(projection, resolveFieldsDependencies);
});
const resolveFieldsDependencies = getResolveFieldsDependencies(requestedFields, graphQLType);

return mergeProjectionAndResolveDependencies(projection, resolveFieldsDependencies);
});

export function getRequestedFields(info: GraphQLResolveInfo): Field {
const selections = flatten(info.fieldNodes.map(_ => _.selectionSet.selections));
const simplifiedNodes = simplifyNodes({ selections: selections }, info);
return mergeNodes(simplifiedNodes);
}

function simplifyNodes(selectionSetNode: { selections: SelectionNode[] }, info: GraphQLResolveInfo, dictionary: FragmentDictionary = {}): FieldGraph[] {
Expand Down Expand Up @@ -90,7 +93,7 @@ function mergeNodes(fieldGraphs: FieldGraph[]): Field {
}, {});
}

function getProjection(fieldNode: Field, graphQLType: GraphQLObjectType, path: string[] = [], ...excludedFields: string[]): MongoDbProjection {
export function getProjection(fieldNode: Field, graphQLType: GraphQLObjectType, path: string[] = [], ...excludedFields: string[]): MongoDbProjection {
const typeFields = getTypeFields(graphQLType)();

return Object.assign({}, ...Object.keys(fieldNode)
Expand All @@ -109,7 +112,7 @@ function getProjection(fieldNode: Field, graphQLType: GraphQLObjectType, path: s
}));
}

function getResolveFieldsDependencies(fieldNode: Field, graphQLType: GraphQLObjectType): string[] {
export function getResolveFieldsDependencies(fieldNode: Field, graphQLType: GraphQLObjectType): string[] {
const typeFields = getTypeFields(graphQLType)();

return Object.keys(fieldNode)
Expand All @@ -134,7 +137,7 @@ function getResolveFieldsDependencies(fieldNode: Field, graphQLType: GraphQLObje
}, []);
}

function mergeProjectionAndResolveDependencies(projection: MongoDbProjection, resolveDependencies: string[]): MongoDbProjection {
export function mergeProjectionAndResolveDependencies(projection: MongoDbProjection, resolveDependencies: string[]): MongoDbProjection {

const newProjection = { ...projection };

Expand All @@ -147,11 +150,11 @@ function mergeProjectionAndResolveDependencies(projection: MongoDbProjection, re
continue;
}

if (projectionKeys.some(key => new RegExp(`^${key}.`).test(dependency))) {
if (projectionKeys.some(key => new RegExp(`^${key}[.]`).test(dependency))) {
continue;
}

const regex = new RegExp(`^${dependency}.`)
const regex = new RegExp(`^${dependency}[.]`)

projectionKeys
.filter(key => regex.test(key))
Expand All @@ -161,6 +164,4 @@ function mergeProjectionAndResolveDependencies(projection: MongoDbProjection, re
}

return newProjection;
}

export default getMongoDbProjection
}
20 changes: 9 additions & 11 deletions src/mongoDbUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ export interface updateObj {
}

function getMongoDbUpdate(update: updateArg): updateObj {
return logOnError(() => {
return clear({
update: {
$setOnInsert: update.setOnInsert,
$set: update.set ? flattenMongoDbSet(update.set) : undefined,
$inc: update.inc
},
options: update.setOnInsert ? { upsert: true } : undefined
}, FICTIVE_INC);
});
return clear({
update: {
$setOnInsert: update.setOnInsert,
$set: update.set ? flattenMongoDbSet(update.set) : undefined,
$inc: update.inc
},
options: update.setOnInsert ? { upsert: true } : undefined
}, FICTIVE_INC);
}

function flattenMongoDbSet(set: object, path: string[] = []): object {
Expand All @@ -42,4 +40,4 @@ function flattenMongoDbSet(set: object, path: string[] = []): object {
}));
}

export default getMongoDbUpdate;
export default logOnError(getMongoDbUpdate);
2 changes: 1 addition & 1 deletion src/queryResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getMongoDbFilter from './mongoDbFilter';
import getMongoDbProjection, { MongoDbProjection } from './mongoDbProjection';
import { getMongoDbProjection, MongoDbProjection } from './mongoDbProjection';
import { getGraphQLFilterType } from './graphQLFilterType';
import getGraphQLSortType from './graphQLSortType';
import GraphQLPaginationType from './graphQLPaginationType';
Expand Down
2 changes: 1 addition & 1 deletion src/updateResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getGraphQLUpdateType } from './graphQLMutationType';
import getMongoDbFilter from './mongoDbFilter';
import getMongoDbUpdate from './mongoDbUpdate';
import { GraphQLNonNull, isType, GraphQLResolveInfo, GraphQLFieldResolver, GraphQLObjectType } from 'graphql';
import getMongoDbProjection, { MongoDbProjection } from './mongoDbProjection';
import { getMongoDbProjection, MongoDbProjection } from './mongoDbProjection';

export interface UpdateCallback<TSource, TContext> {
(
Expand Down
Loading

0 comments on commit 86f0df7

Please sign in to comment.