forked from OS-Guild/graphql-to-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yoavkarako
authored
Oct 8, 2018
1 parent
86f0df7
commit b6e53af
Showing
14 changed files
with
432 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,12 @@ | ||
import { getGraphQLFilterType } from './src/graphQLFilterType'; | ||
import getMongoDbFilter from './src/mongoDbFilter'; | ||
import { getGraphQLUpdateType, getGraphQLInsertType } from './src/graphQLMutationType'; | ||
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 { getMongoDbQueryResolver, getGraphQLQueryArgs, QueryOptions } from './src/queryResolver'; | ||
import { getMongoDbUpdateResolver, getGraphQLUpdateArgs, UpdateOptions } from './src/updateResolver'; | ||
import { setLogger } from './src/logger'; | ||
|
||
export { | ||
getGraphQLFilterType, | ||
getMongoDbFilter, | ||
getGraphQLUpdateType, | ||
getGraphQLInsertType, | ||
getMongoDbUpdate, | ||
GraphQLPaginationType, | ||
getGraphQLSortType, | ||
getMongoDbSort, | ||
getMongoDbProjection, | ||
QueryOptions, | ||
getMongoDbQueryResolver, | ||
getGraphQLQueryArgs, | ||
UpdateOptions, | ||
getMongoDbUpdateResolver, | ||
getGraphQLUpdateArgs, | ||
setLogger | ||
}; | ||
export { getGraphQLFilterType } from './src/graphQLFilterType'; | ||
export { default as getMongoDbFilter } from './src/mongoDbFilter'; | ||
export { getGraphQLUpdateType, getGraphQLInsertType } from './src/graphQLMutationType'; | ||
export { default as getMongoDbUpdate } from './src/mongoDbUpdate'; | ||
export { validateUpdateArgs } from "./src/mongoDbUpdateValidation"; | ||
export { default as GraphQLPaginationType } from './src/graphQLPaginationType'; | ||
export { default as getGraphQLSortType } from './src/graphQLSortType'; | ||
export { default as getMongoDbSort } from './src/mongoDbSort'; | ||
export { getMongoDbProjection } from './src/mongoDbProjection'; | ||
export { getMongoDbQueryResolver, getGraphQLQueryArgs, QueryOptions } from './src/queryResolver'; | ||
export { getMongoDbUpdateResolver, getGraphQLUpdateArgs, UpdateOptions } from './src/updateResolver'; | ||
export { setLogger } from './src/logger'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { UpdateArgs } from "./mongoDbUpdate"; | ||
import { GraphQLObjectType, GraphQLType, GraphQLNonNull, GraphQLList, GraphQLFieldMap } from "graphql"; | ||
import { isNonNullType, getInnerType, flatten, isListType } from "./common"; | ||
|
||
export interface UpdateField { | ||
[key: string]: UpdateField | UpdateField[] | 1 | ||
} | ||
|
||
export function validateUpdateArgs(updateArgs: UpdateArgs, graphQLType: GraphQLObjectType): void { | ||
let errors: string[] = []; | ||
|
||
errors = [...errors, ...validateNonNullableFields(Object.values(updateArgs), graphQLType)]; | ||
|
||
if (errors.length > 0) { | ||
throw errors.join("\n"); | ||
} | ||
} | ||
|
||
export function validateNonNullableFields(objects: object[], graphQLType: GraphQLObjectType, path: string[] = []): string[] { | ||
const typeFields = graphQLType.getFields(); | ||
|
||
const errors = validateNonNullableFieldsAssert(objects, typeFields, path); | ||
|
||
return [...errors, ...validateNonNullableFieldsTraverse(objects, typeFields, path)]; | ||
} | ||
|
||
export function validateNonNullableFieldsAssert(objects: object[], typeFields: GraphQLFieldMap<any, any>, path: string[] = []): string[] { | ||
return Object | ||
.keys(typeFields) | ||
.map(key => ({ key, type: typeFields[key].type })) | ||
.filter(field => isNonNullType(field.type)) | ||
.reduce((agg, field) => { | ||
let fieldPath = [...path, field.key].join("."); | ||
const fieldValues = objects.map(_ => _[field.key]).filter(_ => _ !== undefined); | ||
if (field.type instanceof GraphQLNonNull) { | ||
if (fieldValues.some(_ => _ === null)) | ||
return [...agg, `Non-nullable field "${fieldPath}" is set to null`]; | ||
if (fieldValues.length === 0) | ||
return [...agg, `Missing non-nullable field "${fieldPath}"`]; | ||
} | ||
if (isListType(field.type) && !validateNonNullListField(fieldValues, field.type)) { | ||
return [...agg, `Non-nullable element of array "${fieldPath}" is set to null`]; | ||
} | ||
|
||
return agg; | ||
}, []); | ||
} | ||
|
||
export function validateNonNullListField(fieldValues: object[], type: GraphQLType): boolean { | ||
if (type instanceof GraphQLNonNull) { | ||
if (fieldValues.some(_ => _ === null)) { | ||
return false; | ||
} | ||
|
||
return validateNonNullListField(fieldValues, type.ofType); | ||
} | ||
|
||
if (type instanceof GraphQLList) { | ||
return validateNonNullListField(flatten(fieldValues.filter(_ => _) as object[][]), type.ofType); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export function validateNonNullableFieldsTraverse(objects: object[], typeFields: GraphQLFieldMap<any, any>, path: string[] = []): string[] { | ||
let keys: string[] = Array.from(new Set(flatten(objects.map(_ => Object.keys(_))))); | ||
|
||
return keys.reduce((agg, key) => { | ||
const field = typeFields[key]; | ||
const type = field.type; | ||
const innerType = getInnerType(type); | ||
|
||
if (!(innerType instanceof GraphQLObjectType) || field.resolve) { | ||
return agg; | ||
} | ||
|
||
const newPath = [...path, key]; | ||
const values = objects.map(_ => _[key]).filter(_ => _); | ||
|
||
if (isListType(type)) { | ||
return [...agg, ...flatten(flattenListField(values, type).map(_ => validateNonNullableFields([_], innerType, newPath)))]; | ||
} else { | ||
return [...agg, ...validateNonNullableFields(values, innerType, newPath)]; | ||
} | ||
}, []); | ||
} | ||
|
||
export function flattenListField(objects: object[], type: GraphQLType): object[] { | ||
if (type instanceof GraphQLNonNull) { | ||
return flattenListField(objects, type.ofType); | ||
} | ||
|
||
if (type instanceof GraphQLList) { | ||
return flattenListField(flatten(objects as object[][]).filter(_ => _), type.ofType); | ||
} | ||
|
||
return objects; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.