Skip to content

Commit

Permalink
enable strictNullChecks 16 wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jun 3, 2021
1 parent becd145 commit a1d6c62
Show file tree
Hide file tree
Showing 30 changed files with 269 additions and 140 deletions.
18 changes: 9 additions & 9 deletions packages/utils/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,21 @@ export type InputFieldFilter = (
) => boolean;

export type FieldFilter = (
typeName?: string,
fieldName?: string,
fieldConfig?: GraphQLFieldConfig<any, any> | GraphQLInputFieldConfig
typeName: string,
fieldName: string,
fieldConfig: GraphQLFieldConfig<any, any> | GraphQLInputFieldConfig
) => boolean;

export type ObjectFieldFilter = (
typeName?: string,
fieldName?: string,
fieldConfig?: GraphQLFieldConfig<any, any>
typeName: string,
fieldName: string,
fieldConfig: GraphQLFieldConfig<any, any>
) => boolean;

export type RootFieldFilter = (
operation?: 'Query' | 'Mutation' | 'Subscription',
rootFieldName?: string,
fieldConfig?: GraphQLFieldConfig<any, any>
operation: 'Query' | 'Mutation' | 'Subscription',
rootFieldName: string,
fieldConfig: GraphQLFieldConfig<any, any>
) => boolean;

export type TypeFilter = (typeName: string, type: GraphQLType) => boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/wrap/src/generateProxyingResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLFieldResolver, GraphQLObjectType, GraphQLResolveInfo, OperationTypeNode } from 'graphql';

import { getResponseKeyFromInfo } from '@graphql-tools/utils';
import { Maybe, getResponseKeyFromInfo } from '@graphql-tools/utils';
import {
delegateToSchema,
getSubschema,
Expand All @@ -20,7 +20,7 @@ export function generateProxyingResolvers<TContext>(

const transformedSchema = applySchemaTransforms(targetSchema, subschemaConfig);

const operationTypes: Record<OperationTypeNode, GraphQLObjectType> = {
const operationTypes: Record<OperationTypeNode, Maybe<GraphQLObjectType>> = {
query: targetSchema.getQueryType(),
mutation: targetSchema.getMutationType(),
subscription: targetSchema.getSubscriptionType(),
Expand Down
2 changes: 1 addition & 1 deletion packages/wrap/src/makeRemoteExecutableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function makeRemoteExecutableSchema({

export function defaultCreateRemoteResolver(
executor: Executor,
subscriber: Subscriber
subscriber?: Subscriber | undefined
): GraphQLFieldResolver<any, any> {
return (_parent, _args, context, info) =>
delegateToSchema({
Expand Down
18 changes: 11 additions & 7 deletions packages/wrap/src/transforms/FilterObjectFieldDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ export default class FilterObjectFieldDirectives implements Transform {
): GraphQLSchema {
const transformer = new TransformObjectFields(
(_typeName: string, _fieldName: string, fieldConfig: GraphQLFieldConfig<any, any>) => {
const keepDirectives = fieldConfig.astNode.directives.filter(dir => {
const directiveDef = originalWrappingSchema.getDirective(dir.name.value);
const directiveValue = directiveDef ? getArgumentValues(directiveDef, dir) : undefined;
return this.filter(dir.name.value, directiveValue);
});

if (keepDirectives.length !== fieldConfig.astNode.directives.length) {
const keepDirectives =
fieldConfig.astNode?.directives?.filter(dir => {
const directiveDef = originalWrappingSchema.getDirective(dir.name.value);
const directiveValue = directiveDef ? getArgumentValues(directiveDef, dir) : undefined;
return this.filter(dir.name.value, directiveValue);
}) ?? [];

if (
fieldConfig.astNode?.directives != null &&
keepDirectives.length !== fieldConfig.astNode.directives.length
) {
fieldConfig = {
...fieldConfig,
astNode: {
Expand Down
64 changes: 42 additions & 22 deletions packages/wrap/src/transforms/HoistField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import {
GraphQLFieldResolver,
} from 'graphql';

import { appendObjectFields, removeObjectFields, Request, ExecutionResult, relocatedError } from '@graphql-tools/utils';
import {
appendObjectFields,
removeObjectFields,
Request,
ExecutionResult,
relocatedError,
assertSome,
} from '@graphql-tools/utils';

import { Transform, defaultMergedResolver, DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';

Expand All @@ -24,7 +31,7 @@ export default class HoistField implements Transform {
private readonly oldFieldName: string;
private readonly argFilters: Array<(arg: GraphQLArgument) => boolean>;
private readonly argLevels: Record<string, number>;
private readonly transformer: Transform;
private readonly transformer: MapFields<any>;

constructor(
typeName: string,
Expand All @@ -45,6 +52,7 @@ export default class HoistField implements Transform {

const pathToField = path.slice();
const oldFieldName = pathToField.pop();
assertSome(oldFieldName);

this.oldFieldName = oldFieldName;
this.pathToField = pathToField;
Expand All @@ -59,7 +67,7 @@ export default class HoistField implements Transform {
{
[typeName]: value => unwrapValue(value, alias),
},
errors => unwrapErrors(errors, alias)
errors => (errors != null ? unwrapErrors(errors, alias) : undefined)
);
this.argLevels = argLevels;
}
Expand Down Expand Up @@ -97,7 +105,7 @@ export default class HoistField implements Transform {

if (hoistingToRootField) {
const targetSchema = subschemaConfig.schema;
const operation = this.typeName === targetSchema.getQueryType().name ? 'query' : 'mutation';
const operation = this.typeName === targetSchema.getQueryType()?.name ? 'query' : 'mutation';
const createProxyingResolver = subschemaConfig.createProxyingResolver ?? defaultCreateProxyingResolver;
resolve = createProxyingResolver({
subschemaConfig,
Expand All @@ -112,26 +120,32 @@ export default class HoistField implements Transform {

const newTargetField = {
...targetField,
resolve,
resolve: resolve!,
};

const level = this.pathToField.length;

Object.keys(targetField.args).forEach(argName => {
const argConfig = targetField.args[argName];
const arg = {
...argConfig,
name: argName,
description: argConfig.description,
defaultValue: argConfig.defaultValue,
extensions: argConfig.extensions,
astNode: argConfig.astNode,
} as GraphQLArgument;
if (this.argFilters[level](arg)) {
argsMap[argName] = arg;
this.argLevels[arg.name] = level;
const args = targetField.args;
if (args != null) {
for (const argName in args) {
const argConfig = args[argName];
if (argConfig == null) {
continue;
}
const arg = {
...argConfig,
name: argName,
description: argConfig.description,
defaultValue: argConfig.defaultValue,
extensions: argConfig.extensions,
astNode: argConfig.astNode,
} as GraphQLArgument;
if (this.argFilters[level](arg)) {
argsMap[argName] = arg;
this.argLevels[arg.name] = level;
}
}
});
}

newTargetField.args = argsMap;

Expand Down Expand Up @@ -180,11 +194,17 @@ export function wrapFieldNode(
kind: Kind.SELECTION_SET,
selections: [acc],
},
arguments: fieldNode.arguments.filter(arg => argLevels[arg.name.value] === index),
arguments:
fieldNode.arguments != null
? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === index)
: undefined,
}),
{
...fieldNode,
arguments: fieldNode.arguments.filter(arg => argLevels[arg.name.value] === path.length),
arguments:
fieldNode.arguments != null
? fieldNode.arguments.filter(arg => argLevels[arg.name.value] === path.length)
: undefined,
}
);
}
Expand Down Expand Up @@ -218,7 +238,7 @@ export function unwrapValue(originalValue: any, alias: string): any {
return originalValue;
}

function unwrapErrors(errors: ReadonlyArray<GraphQLError>, alias: string): Array<GraphQLError> {
function unwrapErrors(errors: ReadonlyArray<GraphQLError> | undefined, alias: string): Array<GraphQLError> | undefined {
if (errors === undefined) {
return undefined;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/wrap/src/transforms/MapFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class MapFields<TContext> implements Transform<any, TContext> {
transformedSchema?: GraphQLSchema
): GraphQLSchema {
const subscriptionTypeName = originalWrappingSchema.getSubscriptionType()?.name;
const objectValueTransformerMap = this.objectValueTransformerMap;
this.transformer = new TransformCompositeFields(
() => undefined,
(typeName, fieldName, fieldNode, fragments, transformationContext) => {
Expand All @@ -45,7 +46,7 @@ export default class MapFields<TContext> implements Transform<any, TContext> {

return fieldNodeTransformer(fieldNode, fragments, transformationContext);
},
this.objectValueTransformerMap != null
objectValueTransformerMap != null
? (data, transformationContext) => {
if (data == null) {
return data;
Expand All @@ -60,7 +61,7 @@ export default class MapFields<TContext> implements Transform<any, TContext> {
}
}

const transformer = this.objectValueTransformerMap[typeName];
const transformer = objectValueTransformerMap[typeName];
if (transformer == null) {
return data;
}
Expand Down
10 changes: 6 additions & 4 deletions packages/wrap/src/transforms/MapLeafValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class MapLeafValues implements Transform<MapLeafValuesTransformat
public transformRequest(
originalRequest: Request,
_delegationContext: DelegationContext,
transformationContext?: MapLeafValuesTransformationContext
transformationContext: MapLeafValuesTransformationContext
): Request {
const document = originalRequest.document;
const variableValues = originalRequest.variables;
Expand Down Expand Up @@ -97,7 +97,7 @@ export default class MapLeafValues implements Transform<MapLeafValuesTransformat
public transformResult(
originalResult: ExecutionResult,
_delegationContext: DelegationContext,
transformationContext?: MapLeafValuesTransformationContext
transformationContext: MapLeafValuesTransformationContext
): ExecutionResult {
return visitResult(
originalResult,
Expand All @@ -112,7 +112,9 @@ export default class MapLeafValues implements Transform<MapLeafValuesTransformat
variableValues: Record<string, any>
): Array<OperationDefinitionNode> {
return operations.map((operation: OperationDefinitionNode) => {
const variableDefinitionMap: Record<string, VariableDefinitionNode> = operation.variableDefinitions.reduce(
const variableDefinitionMap: Record<string, VariableDefinitionNode> = (
operation.variableDefinitions ?? []
).reduce(
(prev, def) => ({
...prev,
[def.variable.name.value]: def,
Expand All @@ -138,7 +140,7 @@ export default class MapLeafValues implements Transform<MapLeafValuesTransformat
field: FieldNode,
variableDefinitionMap: Record<string, VariableDefinitionNode>,
variableValues: Record<string, any>
): FieldNode {
): FieldNode | undefined {
const targetField = this.typeInfo.getFieldDef();

if (!targetField.name.startsWith('__')) {
Expand Down
17 changes: 13 additions & 4 deletions packages/wrap/src/transforms/RenameInputObjectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ import { Transform, DelegationContext, SubschemaConfig } from '@graphql-tools/de

import TransformInputObjectFields from './TransformInputObjectFields';

type RenamerFunction = (
typeName: string,
fieldName: string,
inputFieldConfig: GraphQLInputFieldConfig
) => string | undefined;

export default class RenameInputObjectFields implements Transform {
private readonly renamer: (typeName: string, fieldName: string, inputFieldConfig: GraphQLInputFieldConfig) => string;
private readonly renamer: RenamerFunction;
private readonly transformer: TransformInputObjectFields;
private reverseMap: Record<string, Record<string, string>>;

constructor(renamer: (typeName: string, fieldName: string, inputFieldConfig: GraphQLInputFieldConfig) => string) {
constructor(renamer: RenamerFunction) {
this.renamer = renamer;
this.transformer = new TransformInputObjectFields(
(typeName: string, inputFieldName: string, inputFieldConfig: GraphQLInputFieldConfig) => {
(typeName, inputFieldName, inputFieldConfig) => {
const newName = renamer(typeName, inputFieldName, inputFieldConfig);
if (newName !== undefined && newName !== inputFieldName) {
return [renamer(typeName, inputFieldName, inputFieldConfig), inputFieldConfig];
const value = renamer(typeName, inputFieldName, inputFieldConfig);
if (value != null) {
return [value, inputFieldConfig];
}
}
},
(typeName: string, inputFieldName: string, inputFieldNode: ObjectFieldNode) => {
Expand Down
23 changes: 12 additions & 11 deletions packages/wrap/src/transforms/TransformCompositeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import {
FragmentDefinitionNode,
} from 'graphql';

import { Request, MapperKind, mapSchema, visitData, ExecutionResult } from '@graphql-tools/utils';
import { Request, MapperKind, mapSchema, visitData, ExecutionResult, Maybe } from '@graphql-tools/utils';

import { Transform, DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';

import { FieldTransformer, FieldNodeTransformer, DataTransformer, ErrorsTransformer } from '../types';

export default class TransformCompositeFields<TContext = Record<string, any>> implements Transform<any, TContext> {
private readonly fieldTransformer: FieldTransformer;
private readonly fieldNodeTransformer: FieldNodeTransformer;
private readonly dataTransformer: DataTransformer;
private readonly errorsTransformer: ErrorsTransformer;
private readonly fieldNodeTransformer: FieldNodeTransformer | undefined;
private readonly dataTransformer: DataTransformer | undefined;
private readonly errorsTransformer: ErrorsTransformer | undefined;
private transformedSchema: GraphQLSchema;
private typeInfo: TypeInfo;
private mapping: Record<string, Record<string, string>>;
private subscriptionTypeName: string;
private subscriptionTypeName: string | undefined;

constructor(
fieldTransformer: FieldTransformer,
Expand Down Expand Up @@ -90,10 +90,11 @@ export default class TransformCompositeFields<TContext = Record<string, any>> im
_delegationContext: DelegationContext,
transformationContext: Record<string, any>
): ExecutionResult {
if (this.dataTransformer != null) {
result.data = visitData(result.data, value => this.dataTransformer(value, transformationContext));
const dataTransformer = this.dataTransformer;
if (dataTransformer != null) {
result.data = visitData(result.data, value => dataTransformer(value, transformationContext));
}
if (this.errorsTransformer != null) {
if (this.errorsTransformer != null && Array.isArray(result.errors)) {
result.errors = this.errorsTransformer(result.errors, transformationContext);
}
return result;
Expand All @@ -120,8 +121,8 @@ export default class TransformCompositeFields<TContext = Record<string, any>> im
typeInfo: TypeInfo,
fragments: Record<string, FragmentDefinitionNode>,
transformationContext: Record<string, any>
): SelectionSetNode {
const parentType: GraphQLType = typeInfo.getParentType();
): SelectionSetNode | undefined {
const parentType: Maybe<GraphQLType> = typeInfo.getParentType();
if (parentType == null) {
return undefined;
}
Expand Down Expand Up @@ -151,7 +152,7 @@ export default class TransformCompositeFields<TContext = Record<string, any>> im
});
}

let transformedSelection: SelectionNode | Array<SelectionNode>;
let transformedSelection: SelectionNode | Array<SelectionNode> | undefined;
if (this.fieldNodeTransformer == null) {
transformedSelection = selection;
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/wrap/src/transforms/TransformEnumValues.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLSchema, GraphQLEnumValueConfig, ExecutionResult } from 'graphql';

import { Request, MapperKind, mapSchema } from '@graphql-tools/utils';
import { Request, MapperKind, mapSchema, Maybe } from '@graphql-tools/utils';

import { Transform, DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';

Expand Down Expand Up @@ -62,7 +62,7 @@ export default class TransformEnumValues implements Transform<MapLeafValuesTrans
typeName: string,
externalValue: string,
enumValueConfig: GraphQLEnumValueConfig
): GraphQLEnumValueConfig | [string, GraphQLEnumValueConfig] {
): Maybe<GraphQLEnumValueConfig | [string, GraphQLEnumValueConfig]> {
const transformedEnumValue = this.enumValueTransformer(typeName, externalValue, enumValueConfig);
if (Array.isArray(transformedEnumValue)) {
const newExternalValue = transformedEnumValue[0];
Expand All @@ -86,7 +86,7 @@ function mapEnumValues(typeName: string, value: string, mapping: Record<string,
}

function generateValueTransformer(
valueTransformer: LeafValueTransformer,
valueTransformer: Maybe<LeafValueTransformer>,
mapping: Record<string, Record<string, string>>
): LeafValueTransformer {
if (valueTransformer == null) {
Expand Down
Loading

0 comments on commit a1d6c62

Please sign in to comment.