Skip to content

Commit

Permalink
Perform three directive removals in one traversal.
Browse files Browse the repository at this point in the history
Faced with the prospect of performing a third whole-document traversal
to remove the `@nonreactive` directive, in addition to the traversals
already responsible for removing `@client` and `@connection`, I decided
it was time to use the full power of `removeDirectivesFromDocument` to
remove all three directives in one traversal.
  • Loading branch information
benjamn committed Apr 4, 2023
1 parent e913767 commit 3030094
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hasDirectives,
isExecutionPatchIncrementalResult,
isExecutionPatchResult,
removeDirectivesFromDocument,
} from '../utilities';
import { Cache, ApolloCache, canonicalStringify } from '../cache';

Expand All @@ -20,7 +21,6 @@ import {
hasClientExports,
graphQLResultHasError,
getGraphQLErrorsFromResult,
removeConnectionDirectiveFromDocument,
canUseWeakMap,
ObservableSubscription,
Observable,
Expand Down Expand Up @@ -618,11 +618,12 @@ export class QueryManager<TStore> {

if (!transformCache.has(document)) {
const transformed = this.cache.transformDocument(document);
const noConnection = removeConnectionDirectiveFromDocument(transformed);
const serverQuery = removeDirectivesFromDocument([
removeClientFields ? { name: 'client', remove: true } : {},
{ name: 'connection' },
{ name: 'nonreactive' },
], transformed);
const clientQuery = this.localState.clientQuery(transformed);
const serverQuery =
noConnection &&
this.localState.serverQuery(noConnection, { removeClientFields });

const cacheEntry: TransformCacheEntry = {
document: transformed,
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/graphql/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export function removeDirectivesFromDocument(
directives: RemoveDirectiveConfig[],
doc: DocumentNode,
): DocumentNode | null {
checkDocument(doc);

// Passing empty strings to makeInUseGetterFunction means we handle anonymous
// operations as if their names were "". Anonymous fragment definitions are
// not supposed to be possible, but the same default naming strategy seems
Expand Down

0 comments on commit 3030094

Please sign in to comment.