Skip to content

Commit cd53bb1

Browse files
committed
Exclude query root from page rank calculation
1 parent f9a0a19 commit cd53bb1

File tree

4 files changed

+48
-42
lines changed

4 files changed

+48
-42
lines changed

src/components/settings/PageRankSlider.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default function pageRankSlider(props: PageRankSliderProps) {
1212
const marks = Array.from(schema.extensions.pageRanks.entries()).map(
1313
([label, value]) => ({ label, value }),
1414
);
15-
marks.push({ label: 'All types', value: 0 });
1615
return (
1716
<Slider
1817
aria-label="PageRank filter slider"

src/graph/type-graph.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
} from 'graphql/type';
77

88
import { VoyagerDisplayOptions } from '../components/Voyager.tsx';
9-
import { getEdgeTargets, isNode } from '../introspection/utils.ts';
9+
import {
10+
getEdgeTargets,
11+
isNode,
12+
visitReferencedTypes,
13+
} from '../introspection/utils.ts';
1014

1115
export interface TypeGraph {
1216
schema: GraphQLSchema;
@@ -105,28 +109,6 @@ export function getTypeGraph(
105109
}
106110
}
107111

108-
export function visitReferencedTypes(
109-
schema: GraphQLSchema,
110-
rootType: GraphQLNamedType,
111-
visitor: (type: GraphQLNamedType) => boolean,
112-
): void {
113-
const seenTypes = new Set<GraphQLNamedType>();
114-
walk(rootType);
115-
116-
function walk(type: GraphQLNamedType): void {
117-
if (!isNode(type) || seenTypes.has(type)) {
118-
return;
119-
}
120-
seenTypes.add(type);
121-
122-
if (visitor(type)) {
123-
for (const targetType of getEdgeTargets(schema, type)) {
124-
walk(targetType);
125-
}
126-
}
127-
}
128-
}
129-
130112
export function visitReferencedPaths(
131113
schema: GraphQLSchema,
132114
rootType: GraphQLNamedType,

src/introspection/introspection.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { VoyagerDisplayOptions } from '../components/Voyager.tsx';
2323
import { collectDirectlyReferencedTypes } from '../utils/collect-referenced-types.ts';
2424
import { mapValues } from '../utils/mapValues.ts';
2525
import { transformSchema } from '../utils/transformSchema.ts';
26-
import { getEdgeTargets, isNode } from './utils.ts';
26+
import { getEdgeTargets, isNode, visitReferencedTypes } from './utils.ts';
2727

2828
declare module 'graphql' {
2929
interface GraphQLFieldExtensions<_TSource, _TContext, _TArgs> {
@@ -259,12 +259,17 @@ function calculatePageRank(schema: GraphQLSchema): GraphQLSchema {
259259
const nodes: Array<GraphQLNamedType> = [];
260260
const nodeToIndex = new Map<GraphQLNamedType, number>();
261261

262-
for (const type of Object.values(schema.getTypeMap())) {
263-
if (isNode(type)) {
264-
nodeToIndex.set(type, nodes.length);
265-
nodes.push(type);
266-
}
267-
}
262+
visitReferencedTypes(
263+
schema,
264+
schema.getQueryType()!,
265+
(type: GraphQLNamedType) => {
266+
if (type != schema.getQueryType()) {
267+
nodeToIndex.set(type, nodes.length);
268+
nodes.push(type);
269+
}
270+
return true;
271+
},
272+
);
268273

269274
const count = nodes.length;
270275
const outgoing = new Array<Array<number>>(count);
@@ -306,23 +311,21 @@ function calculatePageRank(schema: GraphQLSchema): GraphQLSchema {
306311
}
307312

308313
let sum = 0;
309-
const pageRanks = new Map(
310-
nodes
311-
.map<[string, number]>((node, index) => [node.name, ranks[index]])
312-
.sort((a, b) => b[1] - a[1])
313-
.map(([name, rank]) => {
314-
sum += rank * 100;
315-
sum = sum >= 100.0 ? 100 : sum;
316-
return [name, sum];
317-
}),
318-
);
314+
const pairs: Array<[string, number]> = nodes
315+
.map<[string, number]>((node, index) => [node.name, ranks[index]])
316+
.sort((a, b) => b[1] - a[1])
317+
.map(([name, rank]) => {
318+
sum += rank * 100;
319+
sum = sum >= 100.0 ? 100 : sum;
320+
return [name, sum];
321+
});
319322

320323
const schemaConfig = schema.toConfig();
321324
return new GraphQLSchema({
322325
...schemaConfig,
323326
extensions: {
324327
...schemaConfig.extensions,
325-
pageRanks,
328+
pageRanks: new Map([[schema.getQueryType()!.name, 0], ...pairs]),
326329
},
327330
});
328331
}

src/introspection/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ export function getEdgeTargets(
134134
return new Set(fieldTypes);
135135
}
136136

137+
export function visitReferencedTypes(
138+
schema: GraphQLSchema,
139+
rootType: GraphQLNamedType,
140+
visitor: (type: GraphQLNamedType) => boolean,
141+
): void {
142+
const seenTypes = new Set<GraphQLNamedType>();
143+
walk(rootType);
144+
145+
function walk(type: GraphQLNamedType): void {
146+
if (!isNode(type) || seenTypes.has(type)) {
147+
return;
148+
}
149+
seenTypes.add(type);
150+
151+
if (visitor(type)) {
152+
for (const targetType of getEdgeTargets(schema, type)) {
153+
walk(targetType);
154+
}
155+
}
156+
}
157+
}
158+
137159
export function isNode(type: GraphQLNamedType): type is GraphQLCompositeType {
138160
return (
139161
isCompositeType(type) &&

0 commit comments

Comments
 (0)