-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding
matchConditions
which can be used to further restrict …
…matches (#8) * adding which can be used to further restrict matching * adding docs on how to use match conditions * tweaking wording * tweaking wording
- Loading branch information
Showing
14 changed files
with
847 additions
and
33 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 |
---|---|---|
@@ -1,9 +1,3 @@ | ||
export { default as RewriteHandler } from './RewriteHandler'; | ||
export { default as Rewriter } from './rewriters/Rewriter'; | ||
export { default as FieldArgNameRewriter } from './rewriters/FieldArgNameRewriter'; | ||
export { default as FieldArgsToInputTypeRewriter } from './rewriters/FieldArgsToInputTypeRewriter'; | ||
export { default as FieldArgTypeRewriter } from './rewriters/FieldArgTypeRewriter'; | ||
export { default as NestFieldOutputsRewriter } from './rewriters/NestFieldOutputsRewriter'; | ||
export { | ||
default as ScalarFieldToObjectFieldRewriter | ||
} from './rewriters/ScalarFieldToObjectFieldRewriter'; | ||
export * from './rewriters'; | ||
export * from './matchConditions'; |
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,38 @@ | ||
import { FragmentDefinitionNode } from 'graphql'; | ||
import { extractPath } from '../ast'; | ||
import matchCondition from './matchCondition'; | ||
export interface FragmentMatchConditionOpts { | ||
fragmentNames?: string[]; | ||
fragmentTypes?: string[]; | ||
pathRegexes?: RegExp[]; | ||
} | ||
|
||
export default ({ | ||
fragmentNames, | ||
fragmentTypes, | ||
pathRegexes | ||
}: FragmentMatchConditionOpts = {}): matchCondition => { | ||
return ({ node }, parents) => { | ||
const fragmentDef = parents.find(({ kind }) => kind === 'FragmentDefinition') as | ||
| FragmentDefinitionNode | ||
| undefined; | ||
if (!fragmentDef) return false; | ||
|
||
if (fragmentNames && !fragmentNames.includes(fragmentDef.name.value)) { | ||
return false; | ||
} | ||
|
||
if (fragmentTypes && !fragmentTypes.includes(fragmentDef.typeCondition.name.value)) { | ||
return false; | ||
} | ||
|
||
if (pathRegexes) { | ||
const pathStr = extractPath([...parents, node]).join('.'); | ||
if (!pathRegexes.find(pathRegex => pathRegex.test(pathStr))) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
}; |
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,10 @@ | ||
export { default as matchCondition } from './matchCondition'; | ||
export { | ||
default as fragmentMatchCondition, | ||
FragmentMatchConditionOpts | ||
} from './fragmentMatchCondition'; | ||
export { default as queryMatchCondition, QueryMatchConditionOpts } from './queryMatchCondition'; | ||
export { | ||
default as mutationMatchCondition, | ||
MutationMatchConditionOpts | ||
} from './mutationMatchCondition'; |
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,6 @@ | ||
import { ASTNode } from 'graphql'; | ||
import { NodeAndVarDefs } from '../ast'; | ||
|
||
type matchCondition = (nodeAndVarDefs: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>) => boolean; | ||
|
||
export default matchCondition; |
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,17 @@ | ||
import matchCondition from './matchCondition'; | ||
import operationMatchCondition from './operationMatchCondition'; | ||
export interface MutationMatchConditionOpts { | ||
mutationNames?: string[]; | ||
pathRegexes?: RegExp[]; | ||
} | ||
|
||
export default ({ | ||
mutationNames, | ||
pathRegexes | ||
}: MutationMatchConditionOpts = {}): matchCondition => { | ||
return operationMatchCondition({ | ||
pathRegexes, | ||
operationNames: mutationNames, | ||
operationTypes: ['mutation'] | ||
}); | ||
}; |
Oops, something went wrong.