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

Commit

Permalink
feat(filterSchema): provide type argument to type filter
Browse files Browse the repository at this point in the history
also removes unnecessary checks considering defaults provides

lint: default values obviate checks
  • Loading branch information
yaacovCR committed Sep 12, 2019
1 parent 4695216 commit 68d4c1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/test/testAlternateMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
extractFields,
} from '../stitching';
import { SchemaExecutionConfig } from '../Interfaces';
import isSpecifiedScalarType from '../utils/isSpecifiedScalarType';

function renameFieldNode(fieldNode: FieldNode, name: string): FieldNode {
return {
Expand Down Expand Up @@ -428,8 +429,8 @@ describe('filter and rename object fields', () => {
'Query.propertyById' === `${operation}.${fieldName}`,
fieldFilter: (typeName: string, fieldName: string) =>
(typeName === 'New_Property' || fieldName === 'name'),
typeFilter: (typeName: string) =>
(typeName === 'New_Property' || typeName === 'New_Location')
typeFilter: (typeName: string, type) =>
(typeName === 'New_Property' || typeName === 'New_Location' || isSpecifiedScalarType(type))
});
});

Expand Down
25 changes: 12 additions & 13 deletions src/transforms/filterSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GraphQLObjectType,
GraphQLScalarType,
GraphQLUnionType,
GraphQLType,
} from 'graphql';
import { GraphQLSchemaWithTransforms } from '../Interfaces';
import { visitSchema, VisitSchemaKind } from './visitSchema';
Expand All @@ -29,40 +30,38 @@ export default function filterSchema({
}: {
schema: GraphQLSchemaWithTransforms;
rootFieldFilter?: RootFieldFilter;
typeFilter?: (typeName: string) => boolean;
typeFilter?: (typeName: string, type: GraphQLType) => boolean;
fieldFilter?: (typeName: string, fieldName: string) => boolean;
}): GraphQLSchemaWithTransforms {
const filteredSchema: GraphQLSchemaWithTransforms = visitSchema(schema, {
[VisitSchemaKind.QUERY]: (type: GraphQLObjectType) => {
return rootFieldFilter ? filterRootFields(type, 'Query', rootFieldFilter) : undefined;
return filterRootFields(type, 'Query', rootFieldFilter);
},
[VisitSchemaKind.MUTATION]: (type: GraphQLObjectType) => {
return rootFieldFilter ? filterRootFields(type, 'Mutation', rootFieldFilter) : undefined;
return filterRootFields(type, 'Mutation', rootFieldFilter);
},
[VisitSchemaKind.SUBSCRIPTION]: (type: GraphQLObjectType) => {
return rootFieldFilter ? filterRootFields(type, 'Subscription', rootFieldFilter) : undefined;
return filterRootFields(type, 'Subscription', rootFieldFilter);
},
[VisitSchemaKind.OBJECT_TYPE]: (type: GraphQLObjectType) => {
return (!typeFilter || typeFilter(type.name)) ?
(filterObjectFields ?
filterObjectFields(type, fieldFilter) :
undefined) :
return (typeFilter(type.name, type)) ?
filterObjectFields(type, fieldFilter) :
null;
},
[VisitSchemaKind.INTERFACE_TYPE]: (type: GraphQLInterfaceType) => {
return (!typeFilter || typeFilter(type.name)) ? undefined : null;
return typeFilter(type.name, type) ? undefined : null;
},
[VisitSchemaKind.UNION_TYPE]: (type: GraphQLUnionType) => {
return (!typeFilter || typeFilter(type.name)) ? undefined : null;
return typeFilter(type.name, type) ? undefined : null;
},
[VisitSchemaKind.INPUT_OBJECT_TYPE]: (type: GraphQLInputObjectType) => {
return (!typeFilter || typeFilter(type.name)) ? undefined : null;
return typeFilter(type.name, type) ? undefined : null;
},
[VisitSchemaKind.ENUM_TYPE]: (type: GraphQLEnumType) => {
return (!typeFilter || typeFilter(type.name)) ? undefined : null;
return typeFilter(type.name, type) ? undefined : null;
},
[VisitSchemaKind.SCALAR_TYPE]: (type: GraphQLScalarType) => {
return (!typeFilter || typeFilter(type.name)) ? undefined : null;
return typeFilter(type.name, type) ? undefined : null;
},
});

Expand Down

0 comments on commit 68d4c1d

Please sign in to comment.