-
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: ScalarFieldToObjectFieldRewriter (#7)
* wip * adding ability to rewrite nested fragment outputs properly * fixing linting * exporting ScalarFieldToObjectFieldRewriter properly * updating readme
- Loading branch information
Showing
9 changed files
with
590 additions
and
11 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
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,60 @@ | ||
import { ASTNode, FieldNode, SelectionSetNode } from 'graphql'; | ||
import { NodeAndVarDefs } from '../ast'; | ||
import Rewriter, { RewriterOpts } from './Rewriter'; | ||
|
||
interface ScalarFieldToObjectFieldRewriterOpts extends RewriterOpts { | ||
objectFieldName: string; | ||
} | ||
|
||
/** | ||
* Rewriter which nests output fields inside of a new output object | ||
* ex: change from `field { subField }` to `field { subField { objectfield } }` | ||
*/ | ||
class ScalarFieldToObjectFieldRewriter extends Rewriter { | ||
protected objectFieldName: string; | ||
|
||
constructor(options: ScalarFieldToObjectFieldRewriterOpts) { | ||
super(options); | ||
this.objectFieldName = options.objectFieldName; | ||
} | ||
|
||
public matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]): boolean { | ||
if (!super.matches(nodeAndVars, parents)) return false; | ||
const node = nodeAndVars.node as FieldNode; | ||
// make sure there's no subselections on this field | ||
if (node.selectionSet) return false; | ||
return true; | ||
} | ||
|
||
public rewriteQuery(nodeAndVarDefs: NodeAndVarDefs) { | ||
const node = nodeAndVarDefs.node as FieldNode; | ||
const { variableDefinitions } = nodeAndVarDefs; | ||
// if there's a subselection already, just return | ||
if (node.selectionSet) return nodeAndVarDefs; | ||
|
||
const selectionSet: SelectionSetNode = { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ | ||
kind: 'Field', | ||
name: { kind: 'Name', value: this.objectFieldName } | ||
} | ||
] | ||
}; | ||
|
||
return { | ||
variableDefinitions, | ||
node: { ...node, selectionSet } | ||
} as NodeAndVarDefs; | ||
} | ||
|
||
public rewriteResponse(response: any) { | ||
if (typeof response === 'object') { | ||
// undo the nesting in the response so it matches the original query | ||
return response[this.objectFieldName]; | ||
} | ||
return response; | ||
} | ||
} | ||
|
||
export default ScalarFieldToObjectFieldRewriter; |
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,2 +1,8 @@ | ||
/** @hidden */ | ||
export const identifyFunc = <T>(val: T) => val; | ||
|
||
/** @hidden */ | ||
export const pushToArrayAtKey = <T>(mapping: { [key: string]: T[] }, key: string, val: T): void => { | ||
if (!mapping[key]) mapping[key] = []; | ||
mapping[key].push(val); | ||
}; |
Oops, something went wrong.