-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use memoization when type merging with lists (#1728)
- Loading branch information
Showing
4 changed files
with
244 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { GraphQLSchema, FieldNode, GraphQLObjectType, GraphQLResolveInfo } from 'graphql'; | ||
|
||
import { collectFields, GraphQLExecutionContext } from '@graphql-tools/utils'; | ||
import { isSubschemaConfig } from './Subschema'; | ||
import { MergedTypeInfo, SubschemaConfig, StitchingInfo } from './types'; | ||
import { memoize3and1 } from './memoize'; | ||
|
||
function collectSubFields(info: GraphQLResolveInfo, typeName: string): Record<string, Array<FieldNode>> { | ||
let subFieldNodes: Record<string, Array<FieldNode>> = Object.create(null); | ||
const visitedFragmentNames = Object.create(null); | ||
|
||
const type = info.schema.getType(typeName) as GraphQLObjectType; | ||
const partialExecutionContext = ({ | ||
schema: info.schema, | ||
variableValues: info.variableValues, | ||
fragments: info.fragments, | ||
} as unknown) as GraphQLExecutionContext; | ||
|
||
info.fieldNodes.forEach(fieldNode => { | ||
subFieldNodes = collectFields( | ||
partialExecutionContext, | ||
type, | ||
fieldNode.selectionSet, | ||
subFieldNodes, | ||
visitedFragmentNames | ||
); | ||
}); | ||
|
||
const stitchingInfo = info.schema.extensions.stitchingInfo as StitchingInfo; | ||
const selectionSetsByType = stitchingInfo.selectionSetsByType; | ||
const selectionSetsByField = stitchingInfo.selectionSetsByField; | ||
|
||
Object.keys(subFieldNodes).forEach(responseName => { | ||
const fieldName = subFieldNodes[responseName][0].name.value; | ||
const typeSelectionSet = selectionSetsByType[typeName]; | ||
if (typeSelectionSet != null) { | ||
subFieldNodes = collectFields( | ||
partialExecutionContext, | ||
type, | ||
typeSelectionSet, | ||
subFieldNodes, | ||
visitedFragmentNames | ||
); | ||
} | ||
const fieldSelectionSet = selectionSetsByField?.[typeName]?.[fieldName]; | ||
if (fieldSelectionSet != null) { | ||
subFieldNodes = collectFields( | ||
partialExecutionContext, | ||
type, | ||
fieldSelectionSet, | ||
subFieldNodes, | ||
visitedFragmentNames | ||
); | ||
} | ||
}); | ||
|
||
return subFieldNodes; | ||
} | ||
|
||
export const getFieldsNotInSubschema = memoize3and1(function ( | ||
info: GraphQLResolveInfo, | ||
subschema: GraphQLSchema | SubschemaConfig, | ||
mergedTypeInfo: MergedTypeInfo, | ||
typeName: string | ||
): Array<FieldNode> { | ||
const typeMap = isSubschemaConfig(subschema) ? mergedTypeInfo.typeMaps.get(subschema) : subschema.getTypeMap(); | ||
const fields = (typeMap[typeName] as GraphQLObjectType).getFields(); | ||
|
||
const subFieldNodes = collectSubFields(info, typeName); | ||
|
||
let fieldsNotInSchema: Array<FieldNode> = []; | ||
Object.keys(subFieldNodes).forEach(responseName => { | ||
const fieldName = subFieldNodes[responseName][0].name.value; | ||
if (!(fieldName in fields)) { | ||
fieldsNotInSchema = fieldsNotInSchema.concat(subFieldNodes[responseName]); | ||
} | ||
}); | ||
|
||
return fieldsNotInSchema; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
export function memoize3and1< | ||
T1 extends Record<string, any>, | ||
T2 extends Record<string, any>, | ||
T3 extends Record<string, any>, | ||
T4 extends string, | ||
R extends any | ||
>(fn: (A1: T1, A2: T2, A3: T3, A4: T4) => R): (A1: T1, A2: T2, A3: T3, A4: T4) => R { | ||
let cache1: WeakMap<T1, WeakMap<T2, WeakMap<T3, Record<string, R>>>>; | ||
|
||
function memoized(a1: T1, a2: T2, a3: T3, a4: T4) { | ||
if (!cache1) { | ||
cache1 = new WeakMap(); | ||
const cache2: WeakMap<T2, WeakMap<T3, Record<T4, R>>> = new WeakMap(); | ||
cache1.set(a1, cache2); | ||
const cache3: WeakMap<T3, Record<T4, R>> = new WeakMap(); | ||
cache2.set(a2, cache3); | ||
const cache4 = Object.create(null); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4[a4] = newValue; | ||
return newValue; | ||
} | ||
|
||
let cache2 = cache1.get(a1); | ||
if (!cache2) { | ||
cache2 = new WeakMap(); | ||
cache1.set(a1, cache2); | ||
const cache3: WeakMap<T3, Record<T4, R>> = new WeakMap(); | ||
cache2.set(a2, cache3); | ||
const cache4 = Object.create(null); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4[a4] = newValue; | ||
return newValue; | ||
} | ||
|
||
let cache3 = cache2.get(a2); | ||
if (!cache3) { | ||
cache3 = new WeakMap(); | ||
cache2.set(a2, cache3); | ||
const cache4 = Object.create(null); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4[a4] = newValue; | ||
return newValue; | ||
} | ||
|
||
let cache4 = cache3.get(a3); | ||
if (!cache4) { | ||
cache4 = Object.create(null); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4[a4] = newValue; | ||
return newValue; | ||
} | ||
|
||
const cachedValue = cache4[a4]; | ||
if (cachedValue === undefined) { | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4[a4] = newValue; | ||
return newValue; | ||
} | ||
|
||
return cachedValue; | ||
} | ||
|
||
return memoized; | ||
} | ||
|
||
export function memoize4< | ||
T1 extends Record<string, any>, | ||
T2 extends Record<string, any>, | ||
T3 extends Record<string, any>, | ||
T4 extends Record<string, any>, | ||
R extends any | ||
>(fn: (A1: T1, A2: T2, A3: T3, A4: T4) => R): (A1: T1, A2: T2, A3: T3, A4: T4) => R { | ||
let cache1: WeakMap<T1, WeakMap<T2, WeakMap<T3, WeakMap<T4, R>>>>; | ||
|
||
function memoized(a1: T1, a2: T2, a3: T3, a4: T4) { | ||
if (!cache1) { | ||
cache1 = new WeakMap(); | ||
const cache2: WeakMap<T2, WeakMap<T3, WeakMap<T4, R>>> = new WeakMap(); | ||
cache1.set(a1, cache2); | ||
const cache3: WeakMap<T3, WeakMap<T4, R>> = new WeakMap(); | ||
cache2.set(a2, cache3); | ||
const cache4: WeakMap<T4, R> = new WeakMap(); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4.set(a4, newValue); | ||
return newValue; | ||
} | ||
|
||
let cache2 = cache1.get(a1); | ||
if (!cache2) { | ||
cache2 = new WeakMap(); | ||
cache1.set(a1, cache2); | ||
const cache3: WeakMap<T3, WeakMap<T4, R>> = new WeakMap(); | ||
cache2.set(a2, cache3); | ||
const cache4: WeakMap<T4, R> = new WeakMap(); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4.set(a4, newValue); | ||
return newValue; | ||
} | ||
|
||
let cache3 = cache2.get(a2); | ||
if (!cache3) { | ||
cache3 = new WeakMap(); | ||
cache2.set(a2, cache3); | ||
const cache4: WeakMap<T4, R> = new WeakMap(); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4.set(a4, newValue); | ||
return newValue; | ||
} | ||
|
||
let cache4 = cache3.get(a3); | ||
if (!cache4) { | ||
cache4 = new WeakMap(); | ||
cache3.set(a3, cache4); | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4.set(a4, newValue); | ||
return newValue; | ||
} | ||
|
||
const cachedValue = cache4.get(a4); | ||
if (cachedValue === undefined) { | ||
const newValue = fn(a1, a2, a3, a4); | ||
cache4.set(a4, newValue); | ||
return newValue; | ||
} | ||
|
||
return cachedValue; | ||
} | ||
|
||
return memoized; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters