This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transforms): add HoistField transform
New HoistField transform allows moving a field on a remote type into a higher level object within the wrapping schema. The appendFields and filterFields methods from the WrapFIelds transform have been moved to the utils directory and exported; filterFields has been renamed to removeFields to better describe what it does.
- Loading branch information
Showing
6 changed files
with
182 additions
and
69 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,80 @@ | ||
/* tslint:disable:no-unused-expression */ | ||
|
||
import { | ||
GraphQLSchema, | ||
GraphQLObjectType, | ||
getNullableType, | ||
} from 'graphql'; | ||
import { Request } from '../Interfaces'; | ||
import { Transform } from './transforms'; | ||
import { healSchema, wrapFieldNode, renameFieldNode } from '../utils'; | ||
import { createMergedResolver } from '../stitching'; | ||
import { default as MapFields } from './MapFields'; | ||
import { appendFields, removeFields } from '../utils/fields'; | ||
|
||
export default class HoistField implements Transform { | ||
private typeName: string; | ||
private path: Array<string>; | ||
private newFieldName: string; | ||
private pathToField: Array<string>; | ||
private oldFieldName: string; | ||
private delimeter: string; | ||
private transformer: Transform; | ||
|
||
constructor( | ||
typeName: string, | ||
path: Array<string>, | ||
newFieldName: string, | ||
delimeter: string = '__gqltf__', | ||
) { | ||
this.typeName = typeName; | ||
this.path = path; | ||
this.newFieldName = newFieldName; | ||
this.delimeter = delimeter; | ||
|
||
this.pathToField = this.path.slice(); | ||
this.oldFieldName = this.pathToField.pop(); | ||
this.transformer = new MapFields({ | ||
[typeName]: { | ||
[newFieldName]: fieldNode => wrapFieldNode( | ||
renameFieldNode(fieldNode, this.oldFieldName), | ||
this.pathToField, | ||
this.delimeter | ||
), | ||
}, | ||
}); | ||
} | ||
|
||
public transformSchema(schema: GraphQLSchema): GraphQLSchema { | ||
const typeMap = schema.getTypeMap(); | ||
|
||
const innerType: GraphQLObjectType<any, any> = this.pathToField.reduce( | ||
(acc, pathSegment) => | ||
getNullableType(acc.getFields()[pathSegment].type) as GraphQLObjectType<any, any>, | ||
typeMap[this.typeName] as GraphQLObjectType<any, any> | ||
); | ||
|
||
const targetField = removeFields( | ||
typeMap, | ||
innerType.name, | ||
fieldName => fieldName === this.oldFieldName, | ||
)[this.oldFieldName]; | ||
|
||
const targetType = (targetField.type as GraphQLObjectType<any, any>); | ||
|
||
appendFields(typeMap, this.typeName, { | ||
[this.newFieldName]: { | ||
type: targetType, | ||
resolve: createMergedResolver({ fromPath: this.pathToField, delimeter: this.delimeter }), | ||
}, | ||
}); | ||
|
||
healSchema(schema); | ||
|
||
return this.transformer.transformSchema(schema); | ||
} | ||
|
||
public transformRequest(originalRequest: Request): Request { | ||
return this.transformer.transformRequest(originalRequest); | ||
} | ||
} |
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,62 @@ | ||
import { | ||
GraphQLFieldConfigMap, | ||
GraphQLObjectTypeConfig, | ||
GraphQLObjectType, | ||
GraphQLFieldConfig, | ||
} from 'graphql'; | ||
import { TypeMap } from 'graphql/type/schema'; | ||
|
||
export function appendFields( | ||
typeMap: TypeMap, | ||
typeName: string, | ||
fields: GraphQLFieldConfigMap<any, any>, | ||
): void { | ||
let type = typeMap[typeName]; | ||
if (type) { | ||
const typeConfig = type.toConfig() as GraphQLObjectTypeConfig<any, any>; | ||
const originalFields = typeConfig.fields; | ||
const newFields = {}; | ||
Object.keys(originalFields).forEach(fieldName => { | ||
newFields[fieldName] = originalFields[fieldName]; | ||
}); | ||
Object.keys(fields).forEach(fieldName => { | ||
newFields[fieldName] = fields[fieldName]; | ||
}); | ||
type = new GraphQLObjectType({ | ||
...typeConfig, | ||
fields: newFields, | ||
}); | ||
} else { | ||
type = new GraphQLObjectType({ | ||
name: typeName, | ||
fields, | ||
}); | ||
} | ||
typeMap[typeName] = type; | ||
} | ||
|
||
export function removeFields( | ||
typeMap: TypeMap, | ||
typeName: string, | ||
testFn: (fieldName: string, field: GraphQLFieldConfig<any, any>) => boolean, | ||
): GraphQLFieldConfigMap<any, any> { | ||
let type = typeMap[typeName]; | ||
const typeConfig = type.toConfig() as GraphQLObjectTypeConfig<any, any>; | ||
const originalFields = typeConfig.fields; | ||
const newFields = {}; | ||
const removedFields = {}; | ||
Object.keys(originalFields).forEach(fieldName => { | ||
if (testFn(fieldName, originalFields[fieldName])) { | ||
removedFields[fieldName] = originalFields[fieldName]; | ||
} else { | ||
newFields[fieldName] = originalFields[fieldName]; | ||
} | ||
}); | ||
type = new GraphQLObjectType({ | ||
...typeConfig, | ||
fields: newFields, | ||
}); | ||
typeMap[typeName] = type; | ||
|
||
return removedFields; | ||
} |
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