Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sashko Stubailo
committed
Aug 17, 2017
1 parent
5b3fe90
commit e5e368a
Showing
5 changed files
with
138 additions
and
87 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ASTNode, GraphQLSchema, visit, FieldNode, Kind } from 'graphql'; | ||
|
||
export default function addTypenameForFragments<T extends ASTNode>(node: T, schema: GraphQLSchema): T { | ||
return visit(node, { | ||
[Kind.FIELD]: { | ||
leave(field: FieldNode) { | ||
if (field.selectionSet) { | ||
const selections = field.selectionSet.selections; | ||
|
||
let hasFragment = false; | ||
let hasTypename = false; | ||
|
||
selections.forEach((selection) => { | ||
if (selection.kind === Kind.INLINE_FRAGMENT || selection.kind === Kind.FRAGMENT_SPREAD) { | ||
hasFragment = true; | ||
} else if (selection.name.value === '__typename') { | ||
hasTypename = true; | ||
} | ||
}); | ||
|
||
if (hasFragment && (!hasTypename)) { | ||
return { | ||
...field, | ||
selectionSet: { | ||
...field.selectionSet, | ||
selections: [ | ||
...field.selectionSet.selections, | ||
{ | ||
kind: 'Field', | ||
name: { | ||
kind: 'Name', | ||
value: '__typename', | ||
} | ||
} as FieldNode | ||
] | ||
} | ||
} as FieldNode; | ||
} | ||
|
||
return field; | ||
} | ||
return field; | ||
}, | ||
}, | ||
}); | ||
} |
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
Oops, something went wrong.