Skip to content

Commit

Permalink
Fix out-of-date querygraph unit test
Browse files Browse the repository at this point in the history
Fixes #73.
  • Loading branch information
Sylvain Lebresne committed Aug 10, 2021
1 parent 5b0e122 commit f075299
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
12 changes: 6 additions & 6 deletions query-graphs-js/src/__tests__/querygraph.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FieldSpec, freeTransition } from "@apollo/query-graphs";
import { ObjectType } from "@apollo/core";
import { FieldCollection, freeTransition } from "@apollo/query-graphs";
import { namedEdges, testGraphFromSchemaString } from './testUtils';

test('building query graphs from schema handles object types', () => {
Expand Down Expand Up @@ -32,13 +33,12 @@ test('building query graphs from schema handles object types', () => {
expect(rootEdge.index).toBe(0);
expect(rootEdge.head).toStrictEqual(root);
expect(graph.outEdge(root, rootEdge.index)).toStrictEqual(rootEdge);
expect(graph.outEdges(root, freeTransition)).toStrictEqual([]);
expect(graph.outEdges(root, new FieldSpec('t1'))).toStrictEqual(rootEdges);
expect(graph.outEdges(root, new FieldSpec('t'))).toStrictEqual([]);

expect(rootEdge.label()).toBe('t1');
expect(rootEdge.matches(new FieldSpec('t1'))).toBe(true);
expect(rootEdge.matches(freeTransition)).toBe(false);
const schema = [...graph.sources.values()][0];
const t1Field = (schema.type('Query')! as ObjectType).field('t1')!;
expect(rootEdge.matchesTransition(new FieldCollection(t1Field))).toBe(true);
expect(rootEdge.matchesTransition(freeTransition)).toBe(false);

const t1 = rootEdge.tail;
expect(t1.type.name).toBe('T1');
Expand Down
14 changes: 10 additions & 4 deletions query-graphs-js/src/__tests__/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { buildSchema } from '@apollo/core';
import { buildGraph, Edge, FieldSpec, Graph, Vertex } from '@apollo/query-graphs';
import { buildSchema, InterfaceType, ObjectType } from '@apollo/core';
import { buildGraph, Edge, FieldCollection, Graph, Vertex } from '@apollo/query-graphs';

export function testGraphFromSchemaString(schemaSDL: string): Graph {
return buildGraph("test", buildSchema(schemaSDL));
}

function singleEdge(graph: Graph, vertex: Vertex, fieldName: string): Edge {
const edges = graph.outEdges(vertex, new FieldSpec(fieldName));
const type = vertex.type as (ObjectType | InterfaceType)
const f = type.field(fieldName);
expect(f).toBeDefined();
const edges = graph.outEdges(vertex).filter(e => e.matchesTransition(new FieldCollection(f!)));
expect(edges.length).toBe(1);
return edges[0];
}
Expand All @@ -17,7 +20,10 @@ export function namedEdges(graph: Graph, vertex: Vertex, ...fieldNames: string[]
const edge = singleEdge(graph, vertex, name);
expect(edges[edge.index]).toBe(edge);
expect(edge.head).toBe(vertex);
expect(edge.transition).toStrictEqual(new FieldSpec(name));
const type = vertex.type as (ObjectType | InterfaceType)
const f = type.field(name);
expect(f).toBeDefined();
expect(edge.transition).toStrictEqual(new FieldCollection(f!));
return edge;
});
}
21 changes: 0 additions & 21 deletions query-graphs-js/src/querygraph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ArgumentDefinition,
assert,
MultiMap,
InterfaceType,
Expand Down Expand Up @@ -71,26 +70,6 @@ export function isRootVertex(vertex: Vertex): vertex is RootVertex {
return vertex instanceof RootVertex;
}

export class FieldSpec {
readonly args: ReadonlyMap<string, ArgumentDefinition<any>>;

constructor(readonly name: string, args: ArgumentDefinition<any>[] = []) {
const m = new Map<string, ArgumentDefinition<any>>();
for (const arg of args) {
m.set(arg.name, arg);
}
this.args = m;
}

toString(): string {
let str = this.name;
if (this.args.size > 0) {
str = str + '(' + [...this.args.values()].map(arg => `${arg.name}: ${arg.type}`) + ')';
}
return str;
}
}

export class Edge {
private _conditions?: SelectionSet;

Expand Down

0 comments on commit f075299

Please sign in to comment.