@pothos/selection-mapper 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.swcrc +10 -0
- package/LICENSE +6 -0
- package/README.md +92 -0
- package/dts/adapter.d.ts +74 -0
- package/dts/adapter.d.ts.map +1 -0
- package/dts/deep-equal.d.ts +7 -0
- package/dts/deep-equal.d.ts.map +1 -0
- package/dts/index.d.ts +13 -0
- package/dts/index.d.ts.map +1 -0
- package/dts/loader-map.d.ts +63 -0
- package/dts/loader-map.d.ts.map +1 -0
- package/dts/matches.d.ts +113 -0
- package/dts/matches.d.ts.map +1 -0
- package/dts/node.d.ts +85 -0
- package/dts/node.d.ts.map +1 -0
- package/dts/plan.d.ts +176 -0
- package/dts/plan.d.ts.map +1 -0
- package/dts/types.d.ts +97 -0
- package/dts/types.d.ts.map +1 -0
- package/dts/walk.d.ts +24 -0
- package/dts/walk.d.ts.map +1 -0
- package/esm/adapter.d.ts +74 -0
- package/esm/adapter.d.ts.map +1 -0
- package/esm/adapter.js +47 -0
- package/esm/adapter.js.map +1 -0
- package/esm/deep-equal.d.ts +7 -0
- package/esm/deep-equal.d.ts.map +1 -0
- package/esm/deep-equal.js +50 -0
- package/esm/deep-equal.js.map +1 -0
- package/esm/index.d.ts +13 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +12 -0
- package/esm/index.js.map +1 -0
- package/esm/loader-map.d.ts +63 -0
- package/esm/loader-map.d.ts.map +1 -0
- package/esm/loader-map.js +197 -0
- package/esm/loader-map.js.map +1 -0
- package/esm/matches.d.ts +113 -0
- package/esm/matches.d.ts.map +1 -0
- package/esm/matches.js +419 -0
- package/esm/matches.js.map +1 -0
- package/esm/node.d.ts +85 -0
- package/esm/node.d.ts.map +1 -0
- package/esm/node.js +232 -0
- package/esm/node.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/plan.d.ts +176 -0
- package/esm/plan.d.ts.map +1 -0
- package/esm/plan.js +339 -0
- package/esm/plan.js.map +1 -0
- package/esm/types.d.ts +97 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +10 -0
- package/esm/types.js.map +1 -0
- package/esm/walk.d.ts +24 -0
- package/esm/walk.d.ts.map +1 -0
- package/esm/walk.js +442 -0
- package/esm/walk.js.map +1 -0
- package/lib/adapter.js +43 -0
- package/lib/adapter.js.map +1 -0
- package/lib/deep-equal.js +60 -0
- package/lib/deep-equal.js.map +1 -0
- package/lib/index.js +57 -0
- package/lib/index.js.map +1 -0
- package/lib/loader-map.js +209 -0
- package/lib/loader-map.js.map +1 -0
- package/lib/matches.js +427 -0
- package/lib/matches.js.map +1 -0
- package/lib/node.js +245 -0
- package/lib/node.js.map +1 -0
- package/lib/plan.js +349 -0
- package/lib/plan.js.map +1 -0
- package/lib/types.js +9 -0
- package/lib/types.js.map +1 -0
- package/lib/walk.js +455 -0
- package/lib/walk.js.map +1 -0
- package/package.json +55 -0
- package/src/adapter.ts +96 -0
- package/src/deep-equal.ts +67 -0
- package/src/index.ts +32 -0
- package/src/loader-map.ts +287 -0
- package/src/matches.ts +681 -0
- package/src/node.ts +327 -0
- package/src/plan.ts +454 -0
- package/src/types.ts +123 -0
- package/src/walk.ts +696 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { createContextCache } from '@pothos/core';
|
|
2
|
+
/** The mapping of a static selection: nothing can ever be recorded beneath one. */ export const EMPTY_MAPPING = Object.freeze({
|
|
3
|
+
nested: Object.freeze({})
|
|
4
|
+
});
|
|
5
|
+
/**
|
|
6
|
+
* Adopts the first mapping recorded for a key; a later one deep-unions into a copy, so a recorded
|
|
7
|
+
* mapping is never changed after the fact (two plays may accept a different subset of the fields).
|
|
8
|
+
*/ export function unionMappings(into, from) {
|
|
9
|
+
if (!into || into === EMPTY_MAPPING) {
|
|
10
|
+
return from;
|
|
11
|
+
}
|
|
12
|
+
const nested = {
|
|
13
|
+
...into.nested
|
|
14
|
+
};
|
|
15
|
+
for (const key of Object.keys(from.nested)){
|
|
16
|
+
nested[key] = unionMappings(nested[key], from.nested[key]);
|
|
17
|
+
}
|
|
18
|
+
return into.position === undefined ? {
|
|
19
|
+
nested
|
|
20
|
+
} : {
|
|
21
|
+
nested,
|
|
22
|
+
position: into.position
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Per request context: the mappings recorded so far, and the keys they were recorded under, by
|
|
27
|
+
* the path prefix that rehomed them. `responsePath` drops list indices, so every row of a list
|
|
28
|
+
* rebuilds the same key strings; a prefix builds them once.
|
|
29
|
+
*
|
|
30
|
+
* `mappings` holds what a plan recorded for a whole field: every row of that field was loaded by
|
|
31
|
+
* it, so one mapping per path answers for all of them. `rows` holds a mapping that answers for
|
|
32
|
+
* one row only — because two plans disagreed about a key, or because the plan loaded that row
|
|
33
|
+
* alone. A mapping carries the position a connection pages with, so a row answering from another
|
|
34
|
+
* row's mapping would page with arguments its own data was never fetched with. `rowScoped` stays
|
|
35
|
+
* false while nothing has been recorded there, so a request whose rows were all loaded the same
|
|
36
|
+
* way pays nothing for the row tier.
|
|
37
|
+
*/ const cache = createContextCache(()=>({
|
|
38
|
+
mappings: new Map(),
|
|
39
|
+
rehomed: new Map(),
|
|
40
|
+
rows: new WeakMap(),
|
|
41
|
+
rowScoped: false,
|
|
42
|
+
scopes: new WeakMap()
|
|
43
|
+
}));
|
|
44
|
+
/**
|
|
45
|
+
* Memoised per path link, which is created once per field per row and never mutated. graphql-js
|
|
46
|
+
* shares the links above a list between every row of it, so the prefix of a field resolved for
|
|
47
|
+
* each of N rows is built once rather than N times. Weak, so the links go with the request.
|
|
48
|
+
*/ const pathKeys = new WeakMap();
|
|
49
|
+
/** The string keys of a response path joined by `.`; list indices are dropped. */ export function responsePath(path) {
|
|
50
|
+
if (!path) {
|
|
51
|
+
return '';
|
|
52
|
+
}
|
|
53
|
+
let key = pathKeys.get(path);
|
|
54
|
+
if (key === undefined) {
|
|
55
|
+
const prefix = responsePath(path.prev);
|
|
56
|
+
// A list index contributes nothing, so the link's key is its prefix's.
|
|
57
|
+
key = typeof path.key === 'string' ? prefix ? `${prefix}.${path.key}` : path.key : prefix;
|
|
58
|
+
pathKeys.set(path, key);
|
|
59
|
+
}
|
|
60
|
+
return key;
|
|
61
|
+
}
|
|
62
|
+
export function cacheKey(type, path) {
|
|
63
|
+
return `${type}@${responsePath(path)}`;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The keys `mappings` is recorded under when it is rehomed beneath `prefix`. Built once per
|
|
67
|
+
* prefix and shared by both tiers: the strings depend on the path and the plan's own keys, never
|
|
68
|
+
* on which row is being recorded for.
|
|
69
|
+
*/ function rehomedKeys(cached, prefix) {
|
|
70
|
+
let keys = cached.rehomed.get(prefix);
|
|
71
|
+
if (!keys) {
|
|
72
|
+
keys = new Map();
|
|
73
|
+
cached.rehomed.set(prefix, keys);
|
|
74
|
+
}
|
|
75
|
+
return keys;
|
|
76
|
+
}
|
|
77
|
+
/** `key`, a plan's own `Type@relative.path`, moved beneath `prefix`. Built once per prefix. */ function rehome(keys, key, prefix) {
|
|
78
|
+
let under = keys.get(key);
|
|
79
|
+
if (under === undefined) {
|
|
80
|
+
const at = key.indexOf('@');
|
|
81
|
+
under = `${key.slice(0, at)}@${prefix}.${key.slice(at + 1)}`;
|
|
82
|
+
keys.set(key, under);
|
|
83
|
+
}
|
|
84
|
+
return under;
|
|
85
|
+
}
|
|
86
|
+
/** `row` if a mapping can be hung off it. A root field resolves with no parent value at all. */ function ownerOf(row) {
|
|
87
|
+
return typeof row === 'object' && row !== null ? row : null;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Records `mapping` at `key` for `row`. An unclaimed key takes it, and a key already holding this
|
|
91
|
+
* very mapping needs no write, so the rows after the first write nothing. Only a key two plans
|
|
92
|
+
* disagree about gives the row a mapping of its own — or, with no row to hang it off, nothing, so
|
|
93
|
+
* the resolvers beneath fall back and load their own data.
|
|
94
|
+
*/ function claim(cached, key, mapping, row, path) {
|
|
95
|
+
const held = cached.mappings.get(key);
|
|
96
|
+
if (held === undefined) {
|
|
97
|
+
cached.mappings.set(key, mapping);
|
|
98
|
+
} else if (held !== mapping && row) {
|
|
99
|
+
claimForRow(cached, key.slice(0, key.indexOf('@')), mapping, row, path);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Records `mapping` for this field execution against `row` alone, never in the shared tier. The caller loaded
|
|
104
|
+
* `row` by itself, so an unclaimed key is not the caller's to take: the shared tier answers for
|
|
105
|
+
* every row of a field, and a sibling that was never loaded would read the entry as proof that
|
|
106
|
+
* it had been. With no row to hang it off there is nothing to record, and the resolvers beneath
|
|
107
|
+
* fall back and load their own data.
|
|
108
|
+
*/ function claimForRow(cached, type, mapping, row, path) {
|
|
109
|
+
let byPath = cached.rows.get(row);
|
|
110
|
+
if (!byPath) {
|
|
111
|
+
byPath = new WeakMap();
|
|
112
|
+
cached.rows.set(row, byPath);
|
|
113
|
+
cached.rowScoped = true;
|
|
114
|
+
}
|
|
115
|
+
let own = byPath.get(path);
|
|
116
|
+
if (!own) {
|
|
117
|
+
own = new Map();
|
|
118
|
+
byPath.set(path, own);
|
|
119
|
+
}
|
|
120
|
+
own.set(type, mapping);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Records the mappings of a plan rooted at the field `info` resolves, under that field's path.
|
|
124
|
+
* The plan loaded the whole field, so its mappings answer for every row of it: they go to the
|
|
125
|
+
* shared tier. Multiple plans can contribute to one field (Relay nodes of different model
|
|
126
|
+
* types share the list's path), so later contributions replace matching keys and retain others.
|
|
127
|
+
*/ export function setLoaderMappings(ctx, info, mappings) {
|
|
128
|
+
const cached = cache(ctx);
|
|
129
|
+
const prefix = responsePath(info.path);
|
|
130
|
+
const keys = rehomedKeys(cached, prefix);
|
|
131
|
+
const previous = cached.scopes.get(info.path);
|
|
132
|
+
cached.scopes.set(info.path, previous ? {
|
|
133
|
+
...previous,
|
|
134
|
+
...mappings
|
|
135
|
+
} : mappings);
|
|
136
|
+
for (const key of Object.keys(mappings)){
|
|
137
|
+
cached.mappings.set(rehome(keys, key, prefix), mappings[key]);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Records a row's mappings and scopes its descendants to this concrete field execution. A
|
|
142
|
+
* descendant's parent is a different object, so row identity alone cannot carry its plan through
|
|
143
|
+
* a relation, connection wrapper, or list. The execution path retains indices to isolate siblings.
|
|
144
|
+
*/ export function setRowMappings(ctx, info, mappings) {
|
|
145
|
+
cache(ctx).scopes.set(info.path, mappings);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Records the mapping of the field `info` resolves (under its own parent type) along with the
|
|
149
|
+
* mappings beneath it, so both the field's resolver and the resolvers below it can look
|
|
150
|
+
* themselves up. `row` is the value the field is about to resolve with: the parent row when it
|
|
151
|
+
* was loaded by the planned query, and the reloaded row when the fallback loader fetched it.
|
|
152
|
+
*/ export function setFieldMapping(ctx, info, mapping, row) {
|
|
153
|
+
const cached = cache(ctx);
|
|
154
|
+
const owner = ownerOf(row);
|
|
155
|
+
claim(cached, cacheKey(info.parentType.name, info.path), mapping, owner, info.path);
|
|
156
|
+
cached.scopes.set(info.path, mapping.nested);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* `setFieldMapping` for a plan that loaded `row` alone: the field's own mapping is recorded
|
|
160
|
+
* against `row` rather than claimed for the whole field, so a sibling row the plan never loaded
|
|
161
|
+
* keeps falling back instead of reading the entry and resolving against data it does not carry.
|
|
162
|
+
*
|
|
163
|
+
* Descendants inherit the mapping through this field's execution scope, even when their parent
|
|
164
|
+
* is a child object or a connection wrapper rather than the row that was reloaded.
|
|
165
|
+
*/ export function setRowFieldMapping(ctx, info, mapping, row) {
|
|
166
|
+
const cached = cache(ctx);
|
|
167
|
+
const owner = ownerOf(row);
|
|
168
|
+
if (owner) {
|
|
169
|
+
claimForRow(cached, info.parentType.name, mapping, owner, info.path);
|
|
170
|
+
}
|
|
171
|
+
cached.scopes.set(info.path, mapping.nested);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The mapping recorded for the field at `path` on `row`, preferring one recorded for that row
|
|
175
|
+
* over the plan's. `row` is the value whose data the mapping describes — the resolver's parent.
|
|
176
|
+
*/ export function getLoaderMapping(ctx, path, type, row) {
|
|
177
|
+
const cached = cache(ctx);
|
|
178
|
+
const key = cacheKey(type, path);
|
|
179
|
+
if (cached.rowScoped) {
|
|
180
|
+
const owner = ownerOf(row);
|
|
181
|
+
const own = owner && cached.rows.get(owner)?.get(path)?.get(type);
|
|
182
|
+
if (own) {
|
|
183
|
+
return own;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
for(let ancestor = path.prev; ancestor; ancestor = ancestor.prev){
|
|
187
|
+
const scope = cached.scopes.get(ancestor);
|
|
188
|
+
if (scope) {
|
|
189
|
+
// A different plan may have omitted this field. Do not treat a sibling's shared mapping
|
|
190
|
+
// as proof that this execution loaded it.
|
|
191
|
+
return scope[`${type}@${responsePath(path).slice(responsePath(ancestor).length + 1)}`] ?? null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return cached.mappings.get(key) ?? null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
//# sourceMappingURL=loader-map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/loader-map.ts"],"names":["createContextCache","EMPTY_MAPPING","Object","freeze","nested","unionMappings","into","from","key","keys","position","undefined","cache","mappings","Map","rehomed","rows","WeakMap","rowScoped","scopes","pathKeys","responsePath","path","get","prefix","prev","set","cacheKey","type","rehomedKeys","cached","rehome","under","at","indexOf","slice","ownerOf","row","claim","mapping","held","claimForRow","byPath","own","setLoaderMappings","ctx","info","previous","setRowMappings","setFieldMapping","owner","parentType","name","setRowFieldMapping","getLoaderMapping","ancestor","scope","length"],"mappings":"AAAA,SAASA,kBAAkB,QAAQ,eAAe;AAsBlD,iFAA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@
|
|
|
1
|
+
import { type FieldNode, type FragmentDefinitionNode, type FragmentSpreadNode, type GraphQLNamedType, type GraphQLResolveInfo, type GraphQLSchema, type InlineFragmentNode } from 'graphql';
|
|
2
|
+
export interface IndirectPathSegment {
|
|
3
|
+
type?: string;
|
|
4
|
+
name: string;
|
|
5
|
+
}
|
|
6
|
+
export type PathSegment = string | IndirectPathSegment;
|
|
7
|
+
/**
|
|
8
|
+
* Set on a type's extensions (by the errors plugin, or by hand) to say that a selection on the
|
|
9
|
+
* type is really a selection on `getType()`, found under `path` (or any of `paths`).
|
|
10
|
+
*/
|
|
11
|
+
export interface IndirectInclude {
|
|
12
|
+
getType: () => string;
|
|
13
|
+
path?: IndirectPathSegment[];
|
|
14
|
+
paths?: IndirectPathSegment[][];
|
|
15
|
+
}
|
|
16
|
+
export interface Match {
|
|
17
|
+
/** The GraphQL type of the matched field's return type */
|
|
18
|
+
type: GraphQLNamedType;
|
|
19
|
+
field: FieldNode;
|
|
20
|
+
/** Aliased field names from the starting selection to the matched field */
|
|
21
|
+
path: string[];
|
|
22
|
+
deferred: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface MatchOptions {
|
|
25
|
+
/** A type-level path prepended to every path. */
|
|
26
|
+
prefix?: IndirectPathSegment[];
|
|
27
|
+
/** The alias path the returned matches' paths start with. */
|
|
28
|
+
path?: string[];
|
|
29
|
+
deferred?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Just enough of an `Adapter` to look a type's model up. */
|
|
32
|
+
export interface ModelLookup<Model> {
|
|
33
|
+
modelFor: (type: GraphQLNamedType) => Model | undefined;
|
|
34
|
+
}
|
|
35
|
+
/** The include a type carries, if any. */
|
|
36
|
+
export declare function includeOf(type: GraphQLNamedType): IndirectInclude | undefined;
|
|
37
|
+
/** Follows `pothosIndirectInclude.getType()` until a type without one. */
|
|
38
|
+
export declare function resolveType(schema: GraphQLSchema, type: GraphQLNamedType): GraphQLNamedType;
|
|
39
|
+
/**
|
|
40
|
+
* The model of a type, following indirect includes, which `Adapter.modelFor` does not. Every
|
|
41
|
+
* model question the plan asks goes through here.
|
|
42
|
+
*/
|
|
43
|
+
export declare function modelOf<Model>(adapter: ModelLookup<Model>, schema: GraphQLSchema, type: GraphQLNamedType): Model | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* `matches` without the ones whose field returns a model other than `targetType`'s. Several
|
|
46
|
+
* implementations of an interface may share a field name while returning different models, and
|
|
47
|
+
* their selections must never be merged into the same query. Types without a model always match,
|
|
48
|
+
* and a target without one filters nothing.
|
|
49
|
+
*/
|
|
50
|
+
export declare function matchesForModel<Model>(adapter: ModelLookup<Model>, schema: GraphQLSchema, matches: Match[], targetType: GraphQLNamedType): Match[];
|
|
51
|
+
/**
|
|
52
|
+
* One selection set being read, as `eachSelectedField` hands it to a visitor.
|
|
53
|
+
*
|
|
54
|
+
* `type` is the type of the selection set. `expectedType` is the type a field must be selected on
|
|
55
|
+
* for it to count: a field applies only while the two are the same, which is what stops a
|
|
56
|
+
* same-named field under an unrelated fragment from being seen. `resolveFragmentTypes` moves both.
|
|
57
|
+
*/
|
|
58
|
+
export interface SelectionLevel {
|
|
59
|
+
type: GraphQLNamedType;
|
|
60
|
+
expectedType: GraphQLNamedType;
|
|
61
|
+
/** Aliased field names from the starting selection down to this level. */
|
|
62
|
+
path: string[];
|
|
63
|
+
/** Whether a `@defer` was crossed on the way to this level. */
|
|
64
|
+
deferred: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* What `eachSelectedField` calls at every field that applies. Returning `true` stops the
|
|
68
|
+
* traversal; anything else continues it.
|
|
69
|
+
*/
|
|
70
|
+
export type FieldVisitor = (field: FieldNode, level: SelectionLevel) => boolean | void;
|
|
71
|
+
/**
|
|
72
|
+
* Every field selected at the end of one of `paths`, each with the `deferred` flag
|
|
73
|
+
* `eachSelectedField` computed for it.
|
|
74
|
+
*/
|
|
75
|
+
export declare function findMatches(info: GraphQLResolveInfo, type: GraphQLNamedType, selection: FieldNode | FragmentDefinitionNode | InlineFragmentNode, paths: IndirectPathSegment[][], options?: MatchOptions): Match[];
|
|
76
|
+
/**
|
|
77
|
+
* The first field selected at the end of `path`, in document order, or undefined. The traversal
|
|
78
|
+
* stops at it. The match's `deferred` flag is reported but this function's caller — the
|
|
79
|
+
* `selectedFieldNode` handed to a select function — discards it; see `eachSelectedField` for why
|
|
80
|
+
* over-reporting a deferred selection there is safe.
|
|
81
|
+
*/
|
|
82
|
+
export declare function firstMatch(info: GraphQLResolveInfo, type: GraphQLNamedType, selection: FieldNode, path: IndirectPathSegment[], { prefix }?: Pick<MatchOptions, 'prefix'>): Match | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* The names of the fields the document selects directly beneath the field being resolved, seen
|
|
85
|
+
* through any wrapper on its return type. Every row of a list resolves the field with the same
|
|
86
|
+
* field nodes, so the result is memoised on them for the execution — on the nodes rather than on
|
|
87
|
+
* `info.fieldNodes`, which graphql-js 17 builds anew for every resolve, and beneath that on
|
|
88
|
+
* `info.variableValues` (one object per execution) and the return type.
|
|
89
|
+
*
|
|
90
|
+
* `@skip` and `@include` are honoured, `@defer` is not: a name selected only under a deferred
|
|
91
|
+
* fragment is reported as selected even when the plan that loads the row will not walk it. This
|
|
92
|
+
* is the over-reporting side of the divergence `eachSelectedField` describes, which is the safe
|
|
93
|
+
* side to gate a column or a count on. A caller that needs to know what the plan will actually
|
|
94
|
+
* load must ask the plan.
|
|
95
|
+
*/
|
|
96
|
+
export declare function selectedFieldNames(context: object, info: GraphQLResolveInfo): ReadonlySet<string>;
|
|
97
|
+
export declare function collectSelectedFieldNames(info: GraphQLResolveInfo, returnType?: import("graphql").GraphQLNamedOutputType, fieldNodes?: readonly FieldNode[]): ReadonlySet<string>;
|
|
98
|
+
/**
|
|
99
|
+
* Turns a path handed to `nestedSelection` into an include. A string segment is a field of the
|
|
100
|
+
* type the previous segment reached; the walker narrows through fragments on its own. A
|
|
101
|
+
* `{ name, type }` segment pins the fragment type condition the field must be found under, and
|
|
102
|
+
* is a field of that type.
|
|
103
|
+
*/
|
|
104
|
+
export declare function normalizeInclude(path: PathSegment[], type: GraphQLNamedType, expectedType: GraphQLNamedType | undefined, schema: GraphQLSchema): IndirectInclude;
|
|
105
|
+
/**
|
|
106
|
+
* Whether a field or fragment is under `@skip(if: true)` or `@include(if: false)`. Asked of every
|
|
107
|
+
* selection the traversal reads, so it answers a selection carrying no directive at all — nearly
|
|
108
|
+
* every one — without building a directive's argument values twice.
|
|
109
|
+
*/
|
|
110
|
+
export declare function isSkipped(info: GraphQLResolveInfo, selection: FieldNode | FragmentSpreadNode | InlineFragmentNode): boolean;
|
|
111
|
+
/** Whether a fragment is under `@defer` (when the schema declares the directive). */
|
|
112
|
+
export declare function isDeferred(info: GraphQLResolveInfo, node: FragmentSpreadNode | InlineFragmentNode): boolean;
|
|
113
|
+
//# sourceMappingURL=matches.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matches.d.ts","sourceRoot":"","sources":["../src/matches.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,EAGvB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAIlB,KAAK,kBAAkB,EAKxB,MAAM,SAAS,CAAC;AAEjB,MAAM,WAAW,mBAAmB;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,mBAAmB,CAAC;AAEvD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,mBAAmB,EAAE,CAAC;IAC7B,KAAK,CAAC,EAAE,mBAAmB,EAAE,EAAE,CAAC;CACjC;AAED,MAAM,WAAW,KAAK;IACpB,0DAA0D;IAC1D,IAAI,EAAE,gBAAgB,CAAC;IACvB,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