Skip to content

Commit 23a96c8

Browse files
committed
[visitor-plugin-common] chore: replace casts with type guards in touched code
`transformSelectionSet` filters its results with type guards instead of `as string[]` and an annotated `.map` param. `getPossibleTypes` takes `GraphQLType | undefined`, so unwrapping lists and non-nulls needs no cast, and copies the schema's readonly possible-types array. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Adze75p1tHBWWaHdc4fpXk
1 parent c7157be commit 23a96c8

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

‎.changeset/strict-visitor-plugin-common-documents.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Fix type errors in `visitor-plugin-common` (`selection-set-to-object`, `client-s
1010
- `SelectionSetToObject.transformSelectionSet` declares its return type, and throws a descriptive error when the instance has no selection set.
1111
- `LinkField`'s `alias` is optional, matching the link fields built for fields without an alias.
1212
- `BaseSelectionSetProcessor.typeCache` is keyed by `Location | undefined`, since selection sets parsed with `noLocation` have no `loc`.
13-
- `getPossibleTypes` and `OperationVariablesToObject.transform` accept `undefined`, which they already handle by returning no types and `null`.
13+
- `getPossibleTypes` accepts any `GraphQLType` or `undefined`, which it already handles by unwrapping lists and non-nulls and returning no types, and returns a copy of the schema's possible types instead of the schema's own readonly array.
14+
- `OperationVariablesToObject.transform` accepts `undefined`, which it already handles by returning `null`.
1415

1516
Generated output is unchanged.

‎packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,11 +1035,11 @@ export class SelectionSetToObject<
10351035
...(transformedLinkFields || []),
10361036
].filter(Boolean);
10371037

1038-
const allStrings: string[] = transformed.filter(t => typeof t === 'string') as string[];
1038+
const allStrings = transformed.filter((t): t is string => typeof t === 'string');
10391039

1040-
const allObjectsMerged: string[] = transformed
1041-
.filter(t => typeof t !== 'string')
1042-
.map((t: NameAndType) => `${t.name}: ${t.type}`);
1040+
const allObjectsMerged = transformed
1041+
.filter((t): t is NameAndType => typeof t !== 'string')
1042+
.map(t => `${t.name}: ${t.type}`);
10431043

10441044
let mergedObjectsAsString = '';
10451045

‎packages/plugins/other/visitor-plugin-common/src/utils.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
GraphQLOutputType,
1111
GraphQLScalarType,
1212
GraphQLSchema,
13+
GraphQLType,
1314
InlineFragmentNode,
1415
InputValueDefinitionNode,
1516
isAbstractType,
@@ -510,16 +511,16 @@ export function separateSelectionSet(selections: ReadonlyArray<SelectionNode>):
510511

511512
export function getPossibleTypes(
512513
schema: GraphQLSchema,
513-
type: GraphQLNamedType | undefined,
514+
type: GraphQLType | undefined,
514515
): GraphQLObjectType[] {
515516
if (isListType(type) || isNonNullType(type)) {
516-
return getPossibleTypes(schema, type.ofType as GraphQLNamedType);
517+
return getPossibleTypes(schema, type.ofType);
517518
}
518519
if (isObjectType(type)) {
519520
return [type];
520521
}
521522
if (isAbstractType(type)) {
522-
return schema.getPossibleTypes(type) as Array<GraphQLObjectType>;
523+
return [...schema.getPossibleTypes(type)];
523524
}
524525

525526
return [];

0 commit comments

Comments
 (0)