Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add FilterInputObjectFields, RenameInputObjectFields
closes #1058
- Loading branch information
Showing
5 changed files
with
168 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { GraphQLSchema, GraphQLInputFieldConfig } from 'graphql'; | ||
|
||
import { Transform, Request, InputFieldFilter } from '@graphql-tools/utils'; | ||
|
||
import TransformInputFields from './TransformInputFields'; | ||
import { DelegationContext } from 'packages/delegate/src'; | ||
import { InputObjectNodeTransformer } from '../types'; | ||
|
||
export default class FilterInputObjectFields implements Transform { | ||
private readonly transformer: TransformInputFields; | ||
|
||
constructor(filter: InputFieldFilter, inputObjectNodeTransformer: InputObjectNodeTransformer) { | ||
this.transformer = new TransformInputFields( | ||
(typeName: string, fieldName: string, inputFieldConfig: GraphQLInputFieldConfig) => | ||
filter(typeName, fieldName, inputFieldConfig) ? undefined : null, | ||
undefined, | ||
inputObjectNodeTransformer | ||
); | ||
} | ||
|
||
public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema { | ||
return this.transformer.transformSchema(originalSchema); | ||
} | ||
|
||
public transformRequest(originalRequest: Request, delegationContext: DelegationContext): Request { | ||
return this.transformer.transformRequest(originalRequest, delegationContext); | ||
} | ||
} |
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,72 @@ | ||
import { GraphQLSchema, GraphQLInputFieldConfig, ObjectFieldNode } from 'graphql'; | ||
|
||
import { Transform, Request, mapSchema, MapperKind } from '@graphql-tools/utils'; | ||
|
||
import TransformInputFields from './TransformInputFields'; | ||
import { DelegationContext } from 'packages/delegate/src'; | ||
|
||
export default class RenameInputObjectFields implements Transform { | ||
private readonly renamer: (typeName: string, fieldName: string, inputFieldConfig: GraphQLInputFieldConfig) => string; | ||
private readonly transformer: TransformInputFields; | ||
private reverseMap: Record<string, Record<string, string>>; | ||
|
||
constructor(renamer: (typeName: string, fieldName: string, inputFieldConfig: GraphQLInputFieldConfig) => string) { | ||
this.renamer = renamer; | ||
this.transformer = new TransformInputFields( | ||
(typeName: string, inputFieldName: string, inputFieldConfig: GraphQLInputFieldConfig) => { | ||
const newName = renamer(typeName, inputFieldName, inputFieldConfig); | ||
if (newName !== undefined && newName !== inputFieldName) { | ||
return [renamer(typeName, inputFieldName, inputFieldConfig), inputFieldConfig]; | ||
} | ||
}, | ||
(typeName: string, inputFieldName: string, inputFieldNode: ObjectFieldNode) => { | ||
if (!(typeName in this.reverseMap)) { | ||
return inputFieldNode; | ||
} | ||
|
||
const inputFieldNameMap = this.reverseMap[typeName]; | ||
if (!(inputFieldName in inputFieldNameMap)) { | ||
return inputFieldNode; | ||
} | ||
|
||
return { | ||
...inputFieldNode, | ||
name: { | ||
...inputFieldNode.name, | ||
value: inputFieldNameMap[inputFieldName], | ||
}, | ||
}; | ||
} | ||
); | ||
this.reverseMap = Object.create(null); | ||
} | ||
|
||
public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema { | ||
mapSchema(originalSchema, { | ||
[MapperKind.INPUT_OBJECT_FIELD]: ( | ||
inputFieldConfig: GraphQLInputFieldConfig, | ||
fieldName: string, | ||
typeName | ||
): undefined => { | ||
const newName = this.renamer(typeName, fieldName, inputFieldConfig); | ||
if (newName !== undefined && newName !== fieldName) { | ||
if (this.reverseMap[typeName] == null) { | ||
this.reverseMap[typeName] = Object.create(null); | ||
} | ||
this.reverseMap[typeName][newName] = fieldName; | ||
} | ||
return undefined; | ||
}, | ||
|
||
[MapperKind.ROOT_OBJECT]() { | ||
return undefined; | ||
}, | ||
}); | ||
|
||
return this.transformer.transformSchema(originalSchema); | ||
} | ||
|
||
public transformRequest(originalRequest: Request, delegationContext: DelegationContext): Request { | ||
return this.transformer.transformRequest(originalRequest, delegationContext); | ||
} | ||
} |
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