-
Notifications
You must be signed in to change notification settings - Fork 16
New Generic Field Rewriter & Custom Rewriter #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AndresPrez
wants to merge
23
commits into
graphql-query-rewriter:master
Choose a base branch
from
AndresPrez:feature/ap/field-rewriter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
dfb15cf
enhances for argtype coercion
AndresPrez b73c68a
Adds unit tests for new functionalities
AndresPrez 6717867
fixes linting issues
AndresPrez d924691
Creates a generic FieldRewriter
AndresPrez f67ca80
forgot to export new rewriter
AndresPrez ac91ec4
Adds ability to add arguments to the field in FieldRewriter
AndresPrez b05e39c
fixes issue when renamed object fields returned an array of values
AndresPrez 9da7701
Adds support for alises. Implements new CustomRewriter
AndresPrez 5a2b3d3
matchAnyPath moved from rewrite handler to rewrite claass
AndresPrez 5d1e8fc
adds support to rename fields with alises
AndresPrez 843d057
Adds matching node to the rewriteResponse fn
AndresPrez 8ebdb48
Merge branch 'master' into feature/ap/field-rewriter
AndresPrez 54e7491
Fixes issue with CustomRewriter default matchConditions array
AndresPrez 40eda8b
Merge branch 'feature/ap/field-rewriter' of github.com:AndresPrez/gra…
AndresPrez a267589
Adds rewriteVariables to CustomRewriter
AndresPrez b7073f0
fixes reviewed code
AndresPrez d021875
variable trim and rename
AndresPrez 0b67967
adds fix for when rewritten field results are empty arrays
AndresPrez 7873cfa
v0.0.16
AndresPrez ccbb642
fixes "rewriteResultsAtPath" for non-field paths
AndresPrez a752523
v0.0.17
AndresPrez 0d313a5
v0.0.1
AndresPrez 8ce15ad
fixes unintended change to package.json
AndresPrez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
import { ASTNode } from 'graphql'; | ||
import { NodeAndVarDefs } from '../ast'; | ||
import Rewriter, { RewriterOpts, Variables } from './Rewriter'; | ||
|
||
interface CustomRewriterOpts extends RewriterOpts { | ||
matchesFn?: (nodeAndVarDefs: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>) => boolean; | ||
rewriteQueryFn?: (nodeAndVarDefs: NodeAndVarDefs, variables: Variables) => NodeAndVarDefs; | ||
rewriteVariablesFn?: (nodeAndVarDefs: NodeAndVarDefs, variables: Variables) => Variables; | ||
rewriteResponseFn?: ( | ||
response: any, | ||
key: string, | ||
index?: number, | ||
nodeMatchAndParents?: ASTNode[] | ||
) => NodeAndVarDefs; | ||
} | ||
|
||
/** | ||
* A Custom rewriter with its Rewriter functions received as arguments. | ||
* This Rewriter allows users to write their own rewriter functions. | ||
*/ | ||
class CustomRewriter extends Rewriter { | ||
AndresPrez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
protected matchesFn: (nodeAndVarDefs: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>) => boolean; | ||
protected rewriteQueryFn: ( | ||
nodeAndVarDefs: NodeAndVarDefs, | ||
variables: Variables | ||
) => NodeAndVarDefs; | ||
protected rewriteVariablesFn: (nodeAndVarDefs: NodeAndVarDefs, variables: Variables) => Variables; | ||
protected rewriteResponseFn: ( | ||
response: any, | ||
key: string, | ||
index?: number, | ||
nodeMatchAndParents?: ASTNode[] | ||
) => NodeAndVarDefs; | ||
|
||
constructor(options: CustomRewriterOpts) { | ||
const { | ||
matchesFn, | ||
rewriteQueryFn, | ||
rewriteVariablesFn, | ||
rewriteResponseFn, | ||
matchConditions = [() => true], | ||
...rewriterOpts | ||
} = options; | ||
super({ ...rewriterOpts, matchConditions }); | ||
this.matchesFn = matchesFn || super.matches; | ||
this.rewriteQueryFn = rewriteQueryFn || super.rewriteQuery; | ||
this.rewriteVariablesFn = rewriteVariablesFn || super.rewriteVariables; | ||
this.rewriteResponseFn = rewriteResponseFn || super.rewriteResponse; | ||
} | ||
|
||
public matches(nodeAndVarDefs: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>): boolean { | ||
return this.matchesFn(nodeAndVarDefs, parents); | ||
} | ||
|
||
public rewriteQuery(nodeAndVarDefs: NodeAndVarDefs, variables: Variables) { | ||
return this.rewriteQueryFn(nodeAndVarDefs, variables); | ||
} | ||
|
||
public rewriteResponse( | ||
response: any, | ||
key: string, | ||
index?: number, | ||
nodeMatchAndParents?: ASTNode[] | ||
) { | ||
return this.rewriteResponseFn(response, key, index, nodeMatchAndParents); | ||
} | ||
|
||
public rewriteVariables(nodeAndVarDefs: NodeAndVarDefs, variables: Variables): Variables { | ||
return this.rewriteVariablesFn(nodeAndVarDefs, variables); | ||
} | ||
} | ||
|
||
export default CustomRewriter; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.