Skip to content

Commit 7e147a8

Browse files
committed
Expose flow type for typeFromAST and fix exposed bug
1 parent 2adbe22 commit 7e147a8

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/utilities/typeFromAST.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ import type { GraphQLSchema } from '../type/schema';
2525
* found in the schema, then undefined will be returned.
2626
*/
2727
/* eslint-disable no-redeclare */
28-
declare function typeFromASTType(
28+
declare function typeFromAST(
2929
schema: GraphQLSchema,
3030
typeNode: NamedTypeNode,
31-
): void | GraphQLNamedType;
32-
declare function typeFromASTType(
31+
): GraphQLNamedType | void;
32+
declare function typeFromAST(
3333
schema: GraphQLSchema,
3434
typeNode: ListTypeNode,
35-
): void | GraphQLList<*>;
36-
declare function typeFromASTType(
35+
): GraphQLList<any> | void;
36+
declare function typeFromAST(
3737
schema: GraphQLSchema,
3838
typeNode: NonNullTypeNode,
39-
): void | GraphQLNonNull<*>;
40-
function typeFromASTImpl(schema, typeNode) {
39+
): GraphQLNonNull<any> | void;
40+
export function typeFromAST(schema, typeNode) {
4141
/* eslint-enable no-redeclare */
4242
let innerType;
4343
if (typeNode.kind === Kind.LIST_TYPE) {
@@ -54,7 +54,3 @@ function typeFromASTImpl(schema, typeNode) {
5454
/* istanbul ignore next */
5555
throw new Error(`Unexpected type kind: ${(typeNode.kind: empty)}.`);
5656
}
57-
// This will export typeFromAST with the correct type, but currently exposes
58-
// ~26 errors: https://gist.github.com/4a29403a99a8186fcb15064d69c5f3ae
59-
// export var typeFromAST: typeof typeFromASTType = typeFromASTImpl;
60-
export const typeFromAST: $FlowFixMe = typeFromASTImpl;

src/validation/__tests__/PossibleFragmentSpreads-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ describe('Validate: Possible fragment spreads', () => {
138138
);
139139
});
140140

141+
it('ignores incorrect type (caught by FragmentsOnCompositeTypes)', () => {
142+
expectPassesRule(
143+
PossibleFragmentSpreads,
144+
`
145+
fragment petFragment on Pet { ...badInADifferentWay }
146+
fragment badInADifferentWay on String { name }
147+
`,
148+
);
149+
});
150+
141151
it('different object into object', () => {
142152
expectFailsRule(
143153
PossibleFragmentSpreads,

src/validation/rules/PossibleFragmentSpreads.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,10 @@ export function PossibleFragmentSpreads(context: ValidationContext): any {
8282

8383
function getFragmentType(context, name) {
8484
const frag = context.getFragment(name);
85-
return frag && typeFromAST(context.getSchema(), frag.typeCondition);
85+
if (frag) {
86+
const type = typeFromAST(context.getSchema(), frag.typeCondition);
87+
if (isCompositeType(type)) {
88+
return type;
89+
}
90+
}
8691
}

0 commit comments

Comments
 (0)