Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update buildQueryPlan for new Query Plan format #4448

Merged
merged 18 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 57 additions & 14 deletions packages/apollo-gateway/src/QueryPlan.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import {
import {
JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved
FragmentDefinitionNode,
GraphQLSchema,
OperationDefinitionNode,
SelectionSetNode,
VariableDefinitionNode,
Kind,
SelectionNode as GraphQLJSSelectionNode
} from 'graphql';
import { astSerializer, queryPlanSerializer } from './snapshotSerializers';
import prettyFormat from 'pretty-format';
import { queryPlanSerializer, astSerializer } from './snapshotSerializers';

export type ResponsePath = (string | number)[];

export type FragmentMap = { [fragmentName: string]: FragmentDefinitionNode };

export interface QueryPlan {
kind: 'QueryPlan';
node?: PlanNode;
}

export type OperationContext = {
schema: GraphQLSchema;
operation: OperationDefinitionNode;
fragments: FragmentMap;
};

export interface QueryPlan {
kind: 'QueryPlan';
node?: PlanNode;
}

JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved
export type PlanNode = SequenceNode | ParallelNode | FetchNode | FlattenNode;

export interface SequenceNode {
Expand All @@ -38,20 +38,63 @@ export interface ParallelNode {
export interface FetchNode {
kind: 'Fetch';
serviceName: string;
selectionSet: SelectionSetNode;
variableUsages?: { [name: string]: VariableDefinitionNode };
requires?: SelectionSetNode;
internalFragments: Set<FragmentDefinitionNode>;
source: string;
variableUsages?: string[];
requires?: SelectionNode[];
operation: string;
}

export interface FlattenNode {
kind: 'Flatten';
path: ResponsePath;
node: PlanNode;
}

export type SelectionNode = FieldNode | InlineFragmentNode;

export interface FieldNode {
readonly kind: 'Field';
readonly alias?: string;
readonly name: string;
readonly selections?: SelectionNode[];
}

export interface InlineFragmentNode {
readonly kind: 'InlineFragment';
readonly typeCondition?: string;
readonly selections: SelectionNode[];
}

export function serializeQueryPlan(queryPlan: QueryPlan) {
return prettyFormat(queryPlan, {
plugins: [queryPlanSerializer, astSerializer],
});
}

export function getResponseName(node: FieldNode): string {
return node.alias ? node.alias : node.name;
}

export const trimSelectionNodes = (selections: readonly GraphQLJSSelectionNode[]): SelectionNode[] => {
let remapped: SelectionNode[] = [];
JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved

// intentionally leaving out fragment spread from the input selections, because I don't
// THINK that's possible to see in the final query plan -- prove me wrong
JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved
selections.map(selection => {
if(selection.kind === Kind.FIELD){
remapped.push({
kind: Kind.FIELD,
name: selection.name.value,
selections: selection.selectionSet ? trimSelectionNodes(selection.selectionSet?.selections) : undefined
JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved
});
}
if(selection.kind === Kind.INLINE_FRAGMENT){
remapped.push({
kind: Kind.INLINE_FRAGMENT,
typeCondition: selection.typeCondition?.name.value,
selections: trimSelectionNodes(selection.selectionSet?.selections)
JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved
});
}
})

return remapped;
}
JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved
106 changes: 0 additions & 106 deletions packages/apollo-gateway/src/QueryPlanNew.ts

This file was deleted.

6 changes: 5 additions & 1 deletion packages/apollo-gateway/src/__tests__/buildQueryPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ describe('buildQueryPlan', () => {
`;

const queryPlan = buildQueryPlan(buildOperationContext(schema, query));

expect(queryPlan).toMatchInlineSnapshot(`
QueryPlan {
Parallel {
Expand Down Expand Up @@ -919,13 +918,15 @@ describe('buildQueryPlan', () => {
...__QueryPlanFragment_1__
}
}

JakeDawkins marked this conversation as resolved.
Show resolved Hide resolved
fragment __QueryPlanFragment_1__ on Review {
body
author
product {
...__QueryPlanFragment_0__
}
}

fragment __QueryPlanFragment_0__ on Product {
__typename
... on Book {
Expand Down Expand Up @@ -1064,6 +1065,7 @@ describe('buildQueryPlan', () => {
...__QueryPlanFragment_0__
}
}

fragment __QueryPlanFragment_0__ on Review {
id
body
Expand Down Expand Up @@ -1105,13 +1107,15 @@ describe('buildQueryPlan', () => {
...__QueryPlanFragment_1__
}
}

fragment __QueryPlanFragment_1__ on Review {
content: body
author
product {
...__QueryPlanFragment_0__
}
}

fragment __QueryPlanFragment_0__ on Product {
__typename
... on Book {
Expand Down
Loading