Skip to content

Commit

Permalink
feat(graphcache): allow for defining inline-fragment/fragment-definit…
Browse files Browse the repository at this point in the history
…ion client controlled nullability directives (#3502)

Co-authored-by: Phil Pluckthun <phil@kitten.sh>
  • Loading branch information
JoviDeCroock and kitten committed Mar 2, 2024
1 parent 973da7f commit ceeb73b
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-peas-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': minor
---

Allow `@_optional` and `@_required` to be placed on fragment definitions and inline fragments
16 changes: 16 additions & 0 deletions exchanges/graphcache/src/ast/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ export const isDeferred = (

return false;
};

/** Resolves @_optional and @_required directive to determine whether the fields in a fragment are conaidered optional. */
export const isOptional = (
node: FormattedNode<FragmentSpreadNode | InlineFragmentNode>
): boolean | undefined => {
const { optional, required } = getDirectives(node);
if (required) {
return false;
}

if (optional) {
return true;
}

return undefined;
};
346 changes: 346 additions & 0 deletions exchanges/graphcache/src/cacheExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,352 @@ describe('directives', () => {
});
});

it('Does not return partial data for nested selections', () => {
const client = createClient({
url: 'http://0.0.0.0',
exchanges: [],
});
const { source: ops$, next } = makeSubject<Operation>();

const query = gql`
{
todo {
... on Todo @_optional {
id
text
author {
id
name
}
}
}
}
`;

const operation = client.createRequestOperation('query', {
key: 1,
query,
variables: undefined,
});

const queryResult: OperationResult = {
...queryResponse,
operation,
data: {
__typename: 'Query',
todo: {
id: '1',
text: 'learn urql',
__typename: 'Todo',
author: {
__typename: 'Author',
},
},
},
};

const reexecuteOperation = vi
.spyOn(client, 'reexecuteOperation')
.mockImplementation(next);

const response = vi.fn((forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) return queryResult;
return undefined as any;
});

const result = vi.fn();
const forward: ExchangeIO = ops$ => pipe(ops$, map(response), share);

pipe(
cacheExchange({})({ forward, client, dispatchDebug })(ops$),
tap(result),
publish
);

next(operation);

expect(response).toHaveBeenCalledTimes(1);
expect(result).toHaveBeenCalledTimes(1);
expect(reexecuteOperation).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0].data).toEqual(null);
});

it('returns partial results when an inline-fragment is marked as optional', () => {
const client = createClient({
url: 'http://0.0.0.0',
exchanges: [],
});
const { source: ops$, next } = makeSubject<Operation>();

const query = gql`
{
todos {
id
text
... on Todo @_optional {
completed
}
}
}
`;

const operation = client.createRequestOperation('query', {
key: 1,
query,
variables: undefined,
});

const queryResult: OperationResult = {
...queryResponse,
operation,
data: {
__typename: 'Query',
todos: [
{
id: '1',
text: 'learn urql',
__typename: 'Todo',
},
],
},
};

const reexecuteOperation = vi
.spyOn(client, 'reexecuteOperation')
.mockImplementation(next);

const response = vi.fn((forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) return queryResult;
return undefined as any;
});

const result = vi.fn();
const forward: ExchangeIO = ops$ => pipe(ops$, map(response), share);

pipe(
cacheExchange({})({ forward, client, dispatchDebug })(ops$),
tap(result),
publish
);

next(operation);

expect(response).toHaveBeenCalledTimes(1);
expect(result).toHaveBeenCalledTimes(1);
expect(reexecuteOperation).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0].data).toEqual({
todos: [
{
completed: null,
id: '1',
text: 'learn urql',
},
],
});
});

it('does not return partial results when an inline-fragment is marked as optional with a required child fragment', () => {
const client = createClient({
url: 'http://0.0.0.0',
exchanges: [],
});
const { source: ops$, next } = makeSubject<Operation>();

const query = gql`
{
todos {
id
... on Todo @_optional {
text
... on Todo @_required {
completed
}
}
}
}
`;

const operation = client.createRequestOperation('query', {
key: 1,
query,
variables: undefined,
});

const queryResult: OperationResult = {
...queryResponse,
operation,
data: {
__typename: 'Query',
todos: [
{
id: '1',
text: 'learn urql',
__typename: 'Todo',
},
],
},
};

const reexecuteOperation = vi
.spyOn(client, 'reexecuteOperation')
.mockImplementation(next);

const response = vi.fn((forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) return queryResult;
return undefined as any;
});

const result = vi.fn();
const forward: ExchangeIO = ops$ => pipe(ops$, map(response), share);

pipe(
cacheExchange({})({ forward, client, dispatchDebug })(ops$),
tap(result),
publish
);

next(operation);

expect(response).toHaveBeenCalledTimes(1);
expect(result).toHaveBeenCalledTimes(1);
expect(reexecuteOperation).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0].data).toEqual(null);
});

it('does not return partial results when an inline-fragment is marked as optional with a required field', () => {
const client = createClient({
url: 'http://0.0.0.0',
exchanges: [],
});
const { source: ops$, next } = makeSubject<Operation>();

const query = gql`
{
todos {
id
... on Todo @_optional {
text
completed @_required
}
}
}
`;

const operation = client.createRequestOperation('query', {
key: 1,
query,
variables: undefined,
});

const queryResult: OperationResult = {
...queryResponse,
operation,
data: {
__typename: 'Query',
todos: [
{
id: '1',
text: 'learn urql',
__typename: 'Todo',
},
],
},
};

const reexecuteOperation = vi
.spyOn(client, 'reexecuteOperation')
.mockImplementation(next);

const response = vi.fn((forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) return queryResult;
return undefined as any;
});

const result = vi.fn();
const forward: ExchangeIO = ops$ => pipe(ops$, map(response), share);

pipe(
cacheExchange({})({ forward, client, dispatchDebug })(ops$),
tap(result),
publish
);

next(operation);

expect(response).toHaveBeenCalledTimes(1);
expect(result).toHaveBeenCalledTimes(1);
expect(reexecuteOperation).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0].data).toEqual(null);
});

it('returns partial results when a fragment-definition is marked as optional', () => {
const client = createClient({
url: 'http://0.0.0.0',
exchanges: [],
});
const { source: ops$, next } = makeSubject<Operation>();

const query = gql`
{
todos {
id
text
...Fields
}
}
fragment Fields on Todo @_optional {
completed
}
`;

const operation = client.createRequestOperation('query', {
key: 1,
query,
variables: undefined,
});

const queryResult: OperationResult = {
...queryResponse,
operation,
data: {
__typename: 'Query',
todos: [
{
id: '1',
text: 'learn urql',
__typename: 'Todo',
},
],
},
};

const reexecuteOperation = vi
.spyOn(client, 'reexecuteOperation')
.mockImplementation(next);

const response = vi.fn((forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) return queryResult;
return undefined as any;
});

const result = vi.fn();
const forward: ExchangeIO = ops$ => pipe(ops$, map(response), share);

pipe(
cacheExchange({})({ forward, client, dispatchDebug })(ops$),
tap(result),
publish
);

next(operation);

expect(response).toHaveBeenCalledTimes(1);
expect(result).toHaveBeenCalledTimes(1);
expect(reexecuteOperation).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0].data).toEqual(null);
});

it('does not return missing required fields', () => {
const client = createClient({
url: 'http://0.0.0.0',
Expand Down
Loading

0 comments on commit ceeb73b

Please sign in to comment.