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] Add transformers to rename, filter, and arbitrarily transform …
…object fields, fixes ardatan#819.
- Loading branch information
Showing
7 changed files
with
399 additions
and
4 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
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,25 @@ | ||
import { GraphQLField, GraphQLSchema } from 'graphql'; | ||
import { Transform } from './transforms'; | ||
import TransformObjectFields from './TransformObjectFields'; | ||
|
||
export type ObjectFilter = (typeName: string, fieldName: string, field: GraphQLField<any, any>) => boolean; | ||
|
||
export default class FilterObjectFields implements Transform { | ||
private transformer: TransformObjectFields; | ||
|
||
constructor(filter: ObjectFilter) { | ||
this.transformer = new TransformObjectFields( | ||
(typeName: string, fieldName: string, field: GraphQLField<any, any>) => { | ||
if (filter(typeName, fieldName, field)) { | ||
return undefined; | ||
} else { | ||
return null; | ||
} | ||
} | ||
); | ||
} | ||
|
||
public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema { | ||
return this.transformer.transformSchema(originalSchema); | ||
} | ||
} |
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,29 @@ | ||
import { GraphQLNamedType, GraphQLField, GraphQLSchema } from 'graphql'; | ||
import { Transform } from './transforms'; | ||
import { createResolveType, fieldToFieldConfig } from '../stitching/schemaRecreation'; | ||
import { Request } from '../Interfaces'; | ||
import TransformObjectFields from './TransformObjectFields'; | ||
|
||
export default class RenameObjectFields implements Transform { | ||
private transformer: TransformObjectFields; | ||
|
||
constructor(renamer: (typeName: string, fieldName: string, field: GraphQLField<any, any>) => string) { | ||
const resolveType = createResolveType((name: string, type: GraphQLNamedType): GraphQLNamedType => type); | ||
this.transformer = new TransformObjectFields( | ||
(typeName: string, fieldName: string, field: GraphQLField<any, any>) => { | ||
return { | ||
name: renamer(typeName, fieldName, field), | ||
field: fieldToFieldConfig(field, resolveType, true) | ||
}; | ||
} | ||
); | ||
} | ||
|
||
public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema { | ||
return this.transformer.transformSchema(originalSchema); | ||
} | ||
|
||
public transformRequest(originalRequest: Request): Request { | ||
return this.transformer.transformRequest(originalRequest); | ||
} | ||
} |
Oops, something went wrong.