@apollo/composition
Advanced tools
Comparing version 2.4.8 to 2.4.9
import { CompositeType, FieldDefinition, Operation, Schema, SchemaRootKind } from "@apollo/federation-internals"; | ||
import { Edge, QueryGraph, RootPath, Transition, ConditionResolver, TransitionPathWithLazyIndirectPaths, RootVertex } from "@apollo/query-graphs"; | ||
import { Edge, QueryGraph, RootPath, Transition, TransitionPathWithLazyIndirectPaths, RootVertex, ConditionResolver } from "@apollo/query-graphs"; | ||
import { CompositionHint } from "./hints"; | ||
@@ -36,3 +36,3 @@ import { GraphQLError } from "graphql"; | ||
subgraphs: QueryGraph; | ||
conditionResolver: ConditionValidationResolver; | ||
conditionResolver: ConditionResolver; | ||
}): ValidationState; | ||
@@ -51,11 +51,2 @@ validateTransition(context: ValidationContext, supergraphEdge: Edge): { | ||
} | ||
declare class ConditionValidationResolver { | ||
private readonly supergraphSchema; | ||
private readonly federatedQueryGraph; | ||
readonly resolver: ConditionResolver; | ||
constructor(supergraphSchema: Schema, federatedQueryGraph: QueryGraph); | ||
private validateConditions; | ||
private advanceState; | ||
} | ||
export {}; | ||
//# sourceMappingURL=validate.d.ts.map |
@@ -178,3 +178,3 @@ "use strict"; | ||
(0, federation_internals_1.assert)(!supergraphPath.hasAnyEdgeConditions(), () => `A supergraph path should not have edge condition paths (as supergraph edges should not have conditions): ${supergraphPath}`); | ||
const conditionResolver = new ConditionValidationResolver(supergraphSchema, subgraphs); | ||
const conditionResolver = (0, query_graphs_1.simpleValidationConditionResolver)({ supergraph: supergraphSchema, queryGraph: subgraphs, withCaching: true }); | ||
const initialState = ValidationState.initial({ supergraphAPI: supergraphPath.graph, kind: supergraphPath.root.rootKind, subgraphs, conditionResolver }); | ||
@@ -255,3 +255,3 @@ const context = new ValidationContext(supergraphSchema); | ||
static initial({ supergraphAPI, kind, subgraphs, conditionResolver, }) { | ||
return new ValidationState(query_graphs_1.GraphPath.fromGraphRoot(supergraphAPI, kind), initialSubgraphPaths(kind, subgraphs).map((p) => query_graphs_1.TransitionPathWithLazyIndirectPaths.initial(p, conditionResolver.resolver))); | ||
return new ValidationState(query_graphs_1.GraphPath.fromGraphRoot(supergraphAPI, kind), initialSubgraphPaths(kind, subgraphs).map((p) => query_graphs_1.TransitionPathWithLazyIndirectPaths.initial(p, conditionResolver))); | ||
} | ||
@@ -346,3 +346,7 @@ validateTransition(context, supergraphEdge) { | ||
this.validationHints = []; | ||
this.conditionResolver = new ConditionValidationResolver(supergraphSchema, subgraphs); | ||
this.conditionResolver = (0, query_graphs_1.simpleValidationConditionResolver)({ | ||
supergraph: supergraphSchema, | ||
queryGraph: subgraphs, | ||
withCaching: true, | ||
}); | ||
supergraphAPI.rootKinds().forEach((kind) => this.stack.push(ValidationState.initial({ | ||
@@ -405,51 +409,2 @@ supergraphAPI, | ||
} | ||
class ConditionValidationState { | ||
constructor(selection, subgraphOptions) { | ||
this.selection = selection; | ||
this.subgraphOptions = subgraphOptions; | ||
} | ||
toString() { | ||
return `${this.selection} <=> ${(0, query_graphs_1.advanceOptionsToString)(this.subgraphOptions)}`; | ||
} | ||
} | ||
class ConditionValidationResolver { | ||
constructor(supergraphSchema, federatedQueryGraph) { | ||
this.supergraphSchema = supergraphSchema; | ||
this.federatedQueryGraph = federatedQueryGraph; | ||
this.resolver = (0, query_graphs_1.cachingConditionResolver)(federatedQueryGraph, (edge, context, excludedEdges, excludedConditions) => this.validateConditions(edge, context, excludedEdges, excludedConditions)); | ||
} | ||
validateConditions(edge, context, excludedEdges, excludedConditions) { | ||
const conditions = edge.conditions; | ||
excludedConditions = (0, query_graphs_1.addConditionExclusion)(excludedConditions, conditions); | ||
const initialPath = query_graphs_1.GraphPath.create(this.federatedQueryGraph, edge.head); | ||
const initialOptions = [new query_graphs_1.SimultaneousPathsWithLazyIndirectPaths([initialPath], context, this.resolver, excludedEdges, excludedConditions)]; | ||
const stack = []; | ||
for (const selection of conditions.selections()) { | ||
stack.push(new ConditionValidationState(selection, initialOptions)); | ||
} | ||
while (stack.length > 0) { | ||
const state = stack.pop(); | ||
const newStates = this.advanceState(state); | ||
if (newStates === null) { | ||
return query_graphs_1.unsatisfiedConditionsResolution; | ||
} | ||
newStates.forEach(s => stack.push(s)); | ||
} | ||
return { satisfied: true, cost: 1 }; | ||
} | ||
advanceState(state) { | ||
let newOptions = []; | ||
for (const paths of state.subgraphOptions) { | ||
const pathsOptions = (0, query_graphs_1.advanceSimultaneousPathsWithOperation)(this.supergraphSchema, paths, state.selection.element); | ||
if (!pathsOptions) { | ||
continue; | ||
} | ||
newOptions = newOptions.concat(pathsOptions); | ||
} | ||
if (newOptions.length === 0) { | ||
return null; | ||
} | ||
return state.selection.selectionSet ? state.selection.selectionSet.selections().map(s => new ConditionValidationState(s, newOptions)) : []; | ||
} | ||
} | ||
//# sourceMappingURL=validate.js.map |
{ | ||
"name": "@apollo/composition", | ||
"version": "2.4.8", | ||
"version": "2.4.9", | ||
"description": "Apollo Federation composition utilities", | ||
@@ -30,4 +30,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@apollo/federation-internals": "2.4.8", | ||
"@apollo/query-graphs": "2.4.8" | ||
"@apollo/federation-internals": "2.4.9", | ||
"@apollo/query-graphs": "2.4.9" | ||
}, | ||
@@ -34,0 +34,0 @@ "peerDependencies": { |
@@ -46,7 +46,3 @@ import { | ||
Transition, | ||
OpGraphPath, | ||
advanceSimultaneousPathsWithOperation, | ||
ExcludedEdges, | ||
QueryGraphState, | ||
ExcludedConditions, | ||
Unadvanceables, | ||
@@ -56,12 +52,6 @@ isUnadvanceable, | ||
noConditionsResolution, | ||
ConditionResolution, | ||
unsatisfiedConditionsResolution, | ||
ConditionResolver, | ||
cachingConditionResolver, | ||
PathContext, | ||
addConditionExclusion, | ||
SimultaneousPathsWithLazyIndirectPaths, | ||
advanceOptionsToString, | ||
TransitionPathWithLazyIndirectPaths, | ||
RootVertex, | ||
simpleValidationConditionResolver, | ||
ConditionResolver, | ||
} from "@apollo/query-graphs"; | ||
@@ -335,3 +325,3 @@ import { CompositionHint, HINTS } from "./hints"; | ||
assert(!supergraphPath.hasAnyEdgeConditions(), () => `A supergraph path should not have edge condition paths (as supergraph edges should not have conditions): ${supergraphPath}`); | ||
const conditionResolver = new ConditionValidationResolver(supergraphSchema, subgraphs); | ||
const conditionResolver = simpleValidationConditionResolver({ supergraph: supergraphSchema, queryGraph: subgraphs, withCaching: true }); | ||
const initialState = ValidationState.initial({supergraphAPI: supergraphPath.graph, kind: supergraphPath.root.rootKind, subgraphs, conditionResolver}); | ||
@@ -437,7 +427,7 @@ const context = new ValidationContext(supergraphSchema); | ||
subgraphs: QueryGraph, | ||
conditionResolver: ConditionValidationResolver, | ||
conditionResolver: ConditionResolver, | ||
}) { | ||
return new ValidationState( | ||
GraphPath.fromGraphRoot(supergraphAPI, kind)!, | ||
initialSubgraphPaths(kind, subgraphs).map((p) => TransitionPathWithLazyIndirectPaths.initial(p, conditionResolver.resolver)), | ||
initialSubgraphPaths(kind, subgraphs).map((p) => TransitionPathWithLazyIndirectPaths.initial(p, conditionResolver)), | ||
); | ||
@@ -593,3 +583,3 @@ } | ||
class ValidationTraversal { | ||
private readonly conditionResolver: ConditionValidationResolver; | ||
private readonly conditionResolver: ConditionResolver; | ||
// The stack contains all states that aren't terminal. | ||
@@ -612,3 +602,7 @@ private readonly stack: ValidationState[] = []; | ||
) { | ||
this.conditionResolver = new ConditionValidationResolver(supergraphSchema, subgraphs); | ||
this.conditionResolver = simpleValidationConditionResolver({ | ||
supergraph: supergraphSchema, | ||
queryGraph: subgraphs, | ||
withCaching: true, | ||
}); | ||
supergraphAPI.rootKinds().forEach((kind) => this.stack.push(ValidationState.initial({ | ||
@@ -689,85 +683,1 @@ supergraphAPI, | ||
} | ||
class ConditionValidationState { | ||
constructor( | ||
// Selection that belongs to the condition we're validating. | ||
readonly selection: Selection, | ||
// All the possible "simultaneous paths" we could be in the subgraph when we reach this state selection. | ||
readonly subgraphOptions: SimultaneousPathsWithLazyIndirectPaths[] | ||
) {} | ||
toString(): string { | ||
return `${this.selection} <=> ${advanceOptionsToString(this.subgraphOptions)}`; | ||
} | ||
} | ||
class ConditionValidationResolver { | ||
readonly resolver: ConditionResolver; | ||
constructor( | ||
private readonly supergraphSchema: Schema, | ||
private readonly federatedQueryGraph: QueryGraph | ||
) { | ||
this.resolver = cachingConditionResolver( | ||
federatedQueryGraph, | ||
( | ||
edge: Edge, | ||
context: PathContext, | ||
excludedEdges: ExcludedEdges, | ||
excludedConditions: ExcludedConditions | ||
) => this.validateConditions(edge, context, excludedEdges, excludedConditions) | ||
); | ||
} | ||
private validateConditions( | ||
edge: Edge, | ||
context: PathContext, | ||
excludedEdges: ExcludedEdges, | ||
excludedConditions: ExcludedConditions | ||
): ConditionResolution { | ||
const conditions = edge.conditions!; | ||
excludedConditions = addConditionExclusion(excludedConditions, conditions); | ||
const initialPath: OpGraphPath = GraphPath.create(this.federatedQueryGraph, edge.head); | ||
const initialOptions = [new SimultaneousPathsWithLazyIndirectPaths([initialPath], context, this.resolver, excludedEdges, excludedConditions)]; | ||
const stack: ConditionValidationState[] = []; | ||
for (const selection of conditions.selections()) { | ||
stack.push(new ConditionValidationState(selection, initialOptions)); | ||
} | ||
while (stack.length > 0) { | ||
const state = stack.pop()!; | ||
const newStates = this.advanceState(state); | ||
if (newStates === null) { | ||
return unsatisfiedConditionsResolution; | ||
} | ||
newStates.forEach(s => stack.push(s)); | ||
} | ||
// If we exhaust the stack, it means we've been able to find "some" path for every possible selection in the condition, so the | ||
// condition is validated. Note that we use a cost of 1 for all conditions as we don't care about efficiency. | ||
return { satisfied: true, cost: 1 }; | ||
} | ||
private advanceState(state: ConditionValidationState): ConditionValidationState[] | null { | ||
let newOptions: SimultaneousPathsWithLazyIndirectPaths[] = []; | ||
for (const paths of state.subgraphOptions) { | ||
const pathsOptions = advanceSimultaneousPathsWithOperation( | ||
this.supergraphSchema, | ||
paths, | ||
state.selection.element, | ||
); | ||
if (!pathsOptions) { | ||
continue; | ||
} | ||
newOptions = newOptions.concat(pathsOptions); | ||
} | ||
// If we got no options, it means that particular selection of the conditions cannot be satisfied, so the | ||
// overall condition cannot. | ||
if (newOptions.length === 0) { | ||
return null; | ||
} | ||
return state.selection.selectionSet ? state.selection.selectionSet.selections().map(s => new ConditionValidationState(s, newOptions)) : []; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
528772
7620
+ Added@apollo/federation-internals@2.4.9(transitive)
+ Added@apollo/query-graphs@2.4.9(transitive)
- Removed@apollo/federation-internals@2.4.8(transitive)
- Removed@apollo/query-graphs@2.4.8(transitive)
Updated@apollo/query-graphs@2.4.9