Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

chore(core): Clean up some code to reduce @urql/core size (#3035)

Changed files
+128 -127
exchanges
graphcache
packages
+9 -14
exchanges/graphcache/src/cacheExchange.ts
···
// Filter by operations that are cacheable and attempt to query them from the cache
const cacheOps$ = pipe(
sharedOps$,
-
filter(op => {
-
return (
-
op.kind === 'query' && op.context.requestPolicy !== 'network-only'
-
);
-
}),
+
filter(
+
op => op.kind === 'query' && op.context.requestPolicy !== 'network-only'
+
),
map(operationResultFromCache),
share
);
const nonCacheOps$ = pipe(
sharedOps$,
-
filter(op => {
-
return (
-
op.kind !== 'query' || op.context.requestPolicy === 'network-only'
-
);
-
})
+
filter(
+
op => op.kind !== 'query' || op.context.requestPolicy === 'network-only'
+
)
);
// Rebound operations that are incomplete, i.e. couldn't be queried just from the cache
const cacheMissOps$ = pipe(
cacheOps$,
-
filter(res => {
-
return (
+
filter(
+
res =>
res.outcome === 'miss' &&
res.operation.context.requestPolicy !== 'cache-only' &&
!isBlockedByOptimisticUpdate(res.dependencies) &&
!reexecutingOperations.has(res.operation.key)
-
);
-
}),
+
),
map(res => {
dispatchDebug({
type: 'cacheMiss',
+1 -1
package.json
···
"react-dom": "^17.0.2",
"react-is": "^17.0.2",
"styled-components": "^5.2.3",
-
"wonka": "^6.2.3"
+
"wonka": "^6.2.4"
}
},
"devDependencies": {
+3 -4
packages/core/src/client.ts
···
const makeResultSource = (operation: Operation) => {
let result$ = pipe(
results$,
-
filter((res: OperationResult) => {
-
return (
+
filter(
+
(res: OperationResult) =>
res.operation.kind === operation.kind &&
res.operation.key === operation.key &&
(!res.operation.context._instance ||
res.operation.context._instance === operation.context._instance)
-
);
-
})
+
)
);
// Mask typename properties if the option for it is turned on
+5 -12
packages/core/src/exchanges/cache.ts
···
return formattedOperation;
};
-
const isOperationCached = (operation: Operation) => {
-
const {
-
key,
-
kind,
-
context: { requestPolicy },
-
} = operation;
-
return (
-
kind === 'query' &&
-
requestPolicy !== 'network-only' &&
-
(requestPolicy === 'cache-only' || resultCache.has(key))
-
);
-
};
+
const isOperationCached = (operation: Operation) =>
+
operation.kind === 'query' &&
+
operation.context.requestPolicy !== 'network-only' &&
+
(operation.context.requestPolicy === 'cache-only' ||
+
resultCache.has(operation.key));
return ops$ => {
const sharedOps$ = share(ops$);
+33 -30
packages/core/src/exchanges/dedup.ts
···
import { filter, pipe, tap } from 'wonka';
-
import { Exchange, Operation, OperationResult } from '../types';
+
import { Exchange } from '../types';
/** Default deduplication exchange.
*
···
*/
export const dedupExchange: Exchange = ({ forward, dispatchDebug }) => {
const inFlightKeys = new Set<number>();
+
return ops$ =>
+
pipe(
+
forward(
+
pipe(
+
ops$,
+
filter(operation => {
+
if (
+
operation.kind === 'teardown' ||
+
operation.kind === 'mutation'
+
) {
+
inFlightKeys.delete(operation.key);
+
return true;
+
}
-
const filterIncomingOperation = (operation: Operation) => {
-
const { key, kind } = operation;
-
if (kind === 'teardown' || kind === 'mutation') {
-
inFlightKeys.delete(key);
-
return true;
-
}
+
const isInFlight = inFlightKeys.has(operation.key);
+
inFlightKeys.add(operation.key);
-
const isInFlight = inFlightKeys.has(key);
-
inFlightKeys.add(key);
+
if (isInFlight) {
+
dispatchDebug({
+
type: 'dedup',
+
message: 'An operation has been deduped.',
+
operation,
+
});
+
}
-
if (isInFlight) {
-
dispatchDebug({
-
type: 'dedup',
-
message: 'An operation has been deduped.',
-
operation,
-
});
-
}
-
-
return !isInFlight;
-
};
-
-
const afterOperationResult = ({ operation, hasNext }: OperationResult) => {
-
if (!hasNext) {
-
inFlightKeys.delete(operation.key);
-
}
-
};
-
-
return ops$ => {
-
const forward$ = pipe(ops$, filter(filterIncomingOperation));
-
return pipe(forward(forward$), tap(afterOperationResult));
-
};
+
return !isInFlight;
+
})
+
)
+
),
+
tap(result => {
+
if (!result.hasNext) {
+
inFlightKeys.delete(result.operation.key);
+
}
+
})
+
);
};
+25 -21
packages/core/src/exchanges/fallback.ts
···
import { filter, pipe, tap } from 'wonka';
-
import { Operation, ExchangeIO, ExchangeInput } from '../types';
+
import { ExchangeIO, ExchangeInput } from '../types';
/** Used by the `Client` as the last exchange to warn about unhandled operations.
*
···
dispatchDebug,
}: Pick<ExchangeInput, 'dispatchDebug'>) => ExchangeIO = ({
dispatchDebug,
-
}) => ops$ =>
-
pipe(
-
ops$,
-
tap<Operation>(operation => {
-
if (
-
operation.kind !== 'teardown' &&
-
process.env.NODE_ENV !== 'production'
-
) {
-
const message = `No exchange has handled operations of kind "${operation.kind}". Check whether you've added an exchange responsible for these operations.`;
+
}) => ops$ => {
+
if (process.env.NODE_ENV !== 'production') {
+
ops$ = pipe(
+
ops$,
+
tap(operation => {
+
if (
+
operation.kind !== 'teardown' &&
+
process.env.NODE_ENV !== 'production'
+
) {
+
const message = `No exchange has handled operations of kind "${operation.kind}". Check whether you've added an exchange responsible for these operations.`;
-
dispatchDebug({
-
type: 'fallbackCatch',
-
message,
-
operation,
-
});
-
console.warn(message);
-
}
-
}),
-
// All operations that skipped through the entire exchange chain should be filtered from the output
-
filter<any>(() => false)
-
);
+
dispatchDebug({
+
type: 'fallbackCatch',
+
message,
+
operation,
+
});
+
console.warn(message);
+
}
+
})
+
);
+
}
+
+
// All operations that skipped through the entire exchange chain should be filtered from the output
+
return filter((_x): _x is never => false)(ops$);
+
};
+1 -2
packages/core/src/exchanges/fetch.ts
···
return operation.kind === 'query' || operation.kind === 'mutation';
}),
mergeMap(operation => {
-
const { key } = operation;
const body = makeFetchBody(operation);
const url = makeFetchURL(operation, body);
const fetchOptions = makeFetchOptions(operation, body);
···
takeUntil(
pipe(
sharedOps$,
-
filter(op => op.kind === 'teardown' && op.key === key)
+
filter(op => op.kind === 'teardown' && op.key === operation.key)
)
)
);
+9 -7
packages/core/src/internal/fetchOptions.ts
···
if (!useGETMethod || !body) return operation.context.url;
const url = new URL(operation.context.url);
-
const search = url.searchParams;
-
if (body.operationName) search.set('operationName', body.operationName);
-
if (body.query) search.set('query', body.query);
-
if (body.variables)
-
search.set('variables', stringifyVariables(body.variables));
-
if (body.extensions)
-
search.set('extensions', stringifyVariables(body.extensions));
+
for (const key in body) {
+
const value = body[key];
+
if (value) {
+
url.searchParams.set(
+
key,
+
typeof value === 'object' ? stringifyVariables(value) : value
+
);
+
}
+
}
const finalUrl = url.toString();
if (finalUrl.length > 2047 && useGETMethod !== 'force') {
+42 -36
pnpm-lock.yaml
···
react-dom: ^17.0.2
react-is: ^17.0.2
styled-components: ^5.2.3
-
wonka: ^6.2.3
+
wonka: ^6.2.4
importers:
···
specifiers:
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
specifiers:
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
specifiers:
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
react: ^17.0.2
react-dom: ^17.0.2
urql: workspace:*
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
'@cypress/react': 7.0.1_ddmelm2ieimfs7lfnlxmtlhrry
'@urql/exchange-execute': link:../execute
···
'@urql/core': '>=3.2.2'
extract-files: ^11.0.0
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
extract-files: 11.0.0
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
specifiers:
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
specifiers:
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
'@types/react': ^17.0.39
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
'@types/react': 17.0.52
graphql: 16.0.1
···
specifiers:
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
specifiers:
'@urql/core': '>=3.2.2'
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../../packages/core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
packages/core:
specifiers:
graphql: ^16.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
···
'@urql/core': ^3.2.2
graphql: ^16.0.0
preact: ^10.5.5
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
'@testing-library/preact': 2.0.1_preact@10.5.13
graphql: 16.0.1
···
react-ssr-prepass: ^1.1.2
react-test-renderer: ^17.0.1
vite: ^3.0.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
'@cypress/react': 7.0.1_afg4ncukyuyevrurg5xxicvqy4
'@cypress/vite-dev-server': 4.0.1
···
typescript: '>=4.7.3'
urql: workspace:*
webpack: '>=4.4.6'
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../core
-
wonka: 6.2.3
+
wonka: 6.2.4
optionalDependencies:
'@storybook/addons': 6.2.9_sfoxds7t5ydpegc3knd667wn6m
'@urql/devtools': 2.0.3_6datu6bk5t3os3oamu6of3ptzi
···
'@urql/core': ^3.2.2
graphql: ^16.0.0
svelte: ^3.20.0
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
graphql: 16.0.1
svelte: 3.37.0
···
'@vue/test-utils': ^2.3.0
graphql: ^16.0.0
vue: ^3.2.47
-
wonka: ^6.2.3
+
wonka: ^6.2.4
dependencies:
'@urql/core': link:../core
-
wonka: 6.2.3
+
wonka: 6.2.4
devDependencies:
'@vue/test-utils': 2.3.0_vue@3.2.47
graphql: 16.0.1
···
'@octokit/types': 5.5.0
before-after-hook: 2.2.1
universal-user-agent: 5.0.0
+
transitivePeerDependencies:
+
- encoding
dev: true
/@octokit/core/3.6.0:
···
'@octokit/plugin-paginate-rest': 2.13.3_@octokit+core@2.5.4
'@octokit/plugin-request-log': 1.0.0
'@octokit/plugin-rest-endpoint-methods': 3.17.0
+
transitivePeerDependencies:
+
- encoding
dev: true
/@octokit/types/4.1.10:
···
dependencies:
'@urql/core': link:packages/core
graphql: 16.0.1
-
wonka: 6.2.3
+
wonka: 6.2.4
dev: false
optional: true
···
serve-handler: 6.1.3
strip-ansi: 6.0.1
surge: 0.21.7
+
transitivePeerDependencies:
+
- encoding
dev: true
/forwarded/0.1.2:
···
execa: 1.0.0
dev: true
-
/wonka/6.2.3:
-
resolution: {integrity: sha512-EFOYiqDeYLXSzGYt2X3aVe9Hq1XJG+Hz/HjTRRT4dZE9q95khHl5+7pzUSXI19dbMO1/2UMrTf7JT7/7JrSQSQ==}
+
/wonka/6.2.4:
+
resolution: {integrity: sha512-+q0VMDFqLzu+NAOdhmebQb46Fprip1zV1I/AhKYG4MAtru03R5L6MU89XQK59YDiPL7ELDZEEgZTGtCJ0BWL6g==}
dev: false
/word-wrap/1.2.3: