Skip to content

Commit

Permalink
refactor(utils): consolidate more utils.
Browse files Browse the repository at this point in the history
Consolidate forEachField functions.
Move forEachField and forEachDefaultValue to utils folder.
Change forEachField, forEachDefaultValue and mergeDeep to no longer use default exports to fit style of other utils files.

TODO: streamline use/nonuse of default exports within entire library.
  • Loading branch information
yaacovCR committed Oct 25, 2019
1 parent fcf6752 commit 80918f1
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 49 deletions.
11 changes: 7 additions & 4 deletions src/generate/addResolveFunctionsToSchema.ts
Expand Up @@ -18,10 +18,13 @@ import {
import SchemaError from './SchemaError';
import checkForResolveTypeResolver from './checkForResolveTypeResolver';
import extendResolversFromInterfaces from './extendResolversFromInterfaces';
import forEachField from './forEachField';
import forEachDefaultValue from './forEachDefaultValue';
import { parseInputValue, serializeInputValue } from '../utils/transformInputValue';
import { healSchema } from '../utils/heal';
import {
parseInputValue,
serializeInputValue,
healSchema,
forEachField,
forEachDefaultValue,
} from '../utils';

function addResolveFunctionsToSchema(
options: IAddResolveFunctionsToSchemaOptions | GraphQLSchema,
Expand Down
2 changes: 1 addition & 1 deletion src/generate/assertResolveFunctionsPresent.ts
Expand Up @@ -5,7 +5,7 @@ import {
GraphQLScalarType,
} from 'graphql';
import { IResolverValidationOptions } from '../Interfaces';
import forEachField from './forEachField';
import { forEachField } from '../utils';
import SchemaError from './SchemaError';

function assertResolveFunctionsPresent(
Expand Down
1 change: 0 additions & 1 deletion src/generate/index.ts
Expand Up @@ -10,5 +10,4 @@ export { default as concatenateTypeDefs } from './concatenateTypeDefs';
export { default as decorateWithLogger } from './decorateWithLogger';
export { default as extendResolversFromInterfaces } from './extendResolversFromInterfaces';
export { default as extractExtensionDefinitions } from './extractExtensionDefinitions';
export { default as forEachField } from './forEachField';
export { default as SchemaError } from './SchemaError';
8 changes: 5 additions & 3 deletions src/makeExecutableSchema.ts
Expand Up @@ -2,8 +2,11 @@ import { defaultFieldResolver, GraphQLSchema, GraphQLFieldResolver } from 'graph

import { IExecutableSchemaDefinition, ILogger } from './Interfaces';

import { SchemaDirectiveVisitor } from './utils/SchemaDirectiveVisitor';
import mergeDeep from './utils/mergeDeep';
import {
SchemaDirectiveVisitor,
forEachField,
mergeDeep
} from './utils';

import {
attachDirectiveResolvers,
Expand All @@ -13,7 +16,6 @@ import {
addSchemaLevelResolveFunction,
buildSchemaFromTypeDefinitions,
decorateWithLogger,
forEachField,
SchemaError
} from './generate';

Expand Down
6 changes: 2 additions & 4 deletions src/mock.ts
Expand Up @@ -16,10 +16,8 @@ import {
GraphQLNonNull,
} from 'graphql';
import * as uuid from 'uuid';
import {
forEachField,
buildSchemaFromTypeDefinitions,
} from './makeExecutableSchema';
import { buildSchemaFromTypeDefinitions } from './makeExecutableSchema';
import { forEachField } from './utils';

import {
IMocks,
Expand Down
38 changes: 9 additions & 29 deletions src/stitching/mergeSchemas.ts
@@ -1,6 +1,5 @@
import {
DocumentNode,
GraphQLField,
GraphQLNamedType,
GraphQLObjectType,
GraphQLResolveInfo,
Expand Down Expand Up @@ -36,10 +35,15 @@ import {
ExpandAbstractTypes,
ReplaceFieldWithFragment,
} from '../transforms';
import mergeDeep from '../utils/mergeDeep';
import { SchemaDirectiveVisitor } from '../utils/SchemaDirectiveVisitor';
import { cloneDirective, cloneType } from '../utils/clone';
import { healSchema, healTypes } from '../utils/heal';
import {
SchemaDirectiveVisitor,
cloneDirective,
cloneType,
healSchema,
healTypes,
forEachField,
mergeDeep,
} from '../utils';
import { makeMergedType } from './makeMergedType';

type MergeTypeCandidate = {
Expand Down Expand Up @@ -356,30 +360,6 @@ function createDelegatingResolver({
};
}

type FieldIteratorFn = (
fieldDef: GraphQLField<any, any>,
typeName: string,
fieldName: string,
) => void;

function forEachField(schema: GraphQLSchema, fn: FieldIteratorFn): void {
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(typeName => {
const type = typeMap[typeName];

if (
!getNamedType(type).name.startsWith('__') &&
type instanceof GraphQLObjectType
) {
const fields = type.getFields();
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];
fn(field, typeName, fieldName);
});
}
});
}

function addTypeCandidate(
typeCandidates: { [name: string]: Array<MergeTypeCandidate> },
name: string,
Expand Down
@@ -1,7 +1,7 @@
import { getNamedType, GraphQLInputObjectType, GraphQLSchema, GraphQLObjectType } from 'graphql';
import { IDefaultValueIteratorFn } from '../Interfaces';

function forEachDefaultValue(schema: GraphQLSchema, fn: IDefaultValueIteratorFn): void {
export function forEachDefaultValue(schema: GraphQLSchema, fn: IDefaultValueIteratorFn): void {

const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(typeName => {
Expand All @@ -27,5 +27,3 @@ function forEachDefaultValue(schema: GraphQLSchema, fn: IDefaultValueIteratorFn)
}
});
}

export default forEachDefaultValue;
4 changes: 1 addition & 3 deletions src/generate/forEachField.ts → src/utils/forEachField.ts
@@ -1,7 +1,7 @@
import { getNamedType, GraphQLObjectType, GraphQLSchema } from 'graphql';
import { IFieldIteratorFn } from '../Interfaces';

function forEachField(schema: GraphQLSchema, fn: IFieldIteratorFn): void {
export function forEachField(schema: GraphQLSchema, fn: IFieldIteratorFn): void {
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(typeName => {
const type = typeMap[typeName];
Expand All @@ -19,5 +19,3 @@ function forEachField(schema: GraphQLSchema, fn: IFieldIteratorFn): void {
}
});
}

export default forEachField;
9 changes: 9 additions & 0 deletions src/utils/index.ts
Expand Up @@ -4,3 +4,12 @@ export { SchemaVisitor } from './SchemaVisitor';
export { SchemaDirectiveVisitor } from './SchemaDirectiveVisitor';
export { visitSchema } from './visitSchema';
export { getResolversFromSchema } from './getResolversFromSchema';
export { forEachField } from './forEachField';
export { forEachDefaultValue } from './forEachDefaultValue';
export {
transformInputValue,
parseInputValue,
parseInputValueLiteral,
serializeInputValue,
} from './transformInputValue';
export { mergeDeep } from './mergeDeep';
2 changes: 1 addition & 1 deletion src/utils/mergeDeep.ts
@@ -1,4 +1,4 @@
export default function mergeDeep(target: any, source: any): any {
export function mergeDeep(target: any, source: any): any {
let output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
Expand Down

0 comments on commit 80918f1

Please sign in to comment.