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(stitching): precompile fragment replacements
- Loading branch information
Showing
9 changed files
with
380 additions
and
108 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,78 @@ | ||
import { | ||
DocumentNode, | ||
GraphQLSchema, | ||
GraphQLType, | ||
Kind, | ||
SelectionSetNode, | ||
TypeInfo, | ||
visit, | ||
visitWithTypeInfo, | ||
} from 'graphql'; | ||
import { Request, ReplacementFragmentMapping } from '../Interfaces'; | ||
import { Transform } from './transforms'; | ||
|
||
export default class AddReplacementFragments implements Transform { | ||
private targetSchema: GraphQLSchema; | ||
private fragments: ReplacementFragmentMapping; | ||
|
||
constructor( | ||
targetSchema: GraphQLSchema, | ||
fragments: ReplacementFragmentMapping, | ||
) { | ||
this.targetSchema = targetSchema; | ||
this.fragments = fragments; | ||
} | ||
|
||
public transformRequest(originalRequest: Request): Request { | ||
const document = replaceFieldsWithFragments( | ||
this.targetSchema, | ||
originalRequest.document, | ||
this.fragments, | ||
); | ||
return { | ||
...originalRequest, | ||
document, | ||
}; | ||
} | ||
} | ||
|
||
function replaceFieldsWithFragments( | ||
targetSchema: GraphQLSchema, | ||
document: DocumentNode, | ||
fragments: ReplacementFragmentMapping, | ||
): DocumentNode { | ||
const typeInfo = new TypeInfo(targetSchema); | ||
return visit( | ||
document, | ||
visitWithTypeInfo(typeInfo, { | ||
[Kind.SELECTION_SET]( | ||
node: SelectionSetNode, | ||
): SelectionSetNode | null | undefined { | ||
const parentType: GraphQLType = typeInfo.getParentType(); | ||
if (parentType) { | ||
const parentTypeName = parentType.name; | ||
let selections = node.selections; | ||
|
||
if (fragments[parentTypeName]) { | ||
node.selections.forEach(selection => { | ||
if (selection.kind === Kind.FIELD) { | ||
const name = selection.name.value; | ||
const fragment = fragments[parentTypeName][name]; | ||
if (fragment) { | ||
selections = selections.concat(fragment); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
if (selections !== node.selections) { | ||
return { | ||
...node, | ||
selections, | ||
}; | ||
} | ||
} | ||
}, | ||
}), | ||
); | ||
} |
Oops, something went wrong.