Skip to content

Commit

Permalink
Add test and changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewIngram committed Oct 2, 2024
1 parent 8bab375 commit ed68fe5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/breezy-tomatoes-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': minor
---

Allow @_required directive to be used in combination with configured schemas
65 changes: 65 additions & 0 deletions exchanges/graphcache/src/cacheExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,71 @@ describe('directives', () => {
expect(reexecuteOperation).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0].data).toEqual(null);
});

it('does not return missing fields when nullable fields from a defined schema are marked as required in the query', () => {
const client = createClient({
url: 'http://0.0.0.0',
exchanges: [],
});
const { source: ops$, next } = makeSubject<Operation>();

const query = gql`
{
latestTodo {
id
text
completed @_required
}
}
`;

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

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

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({
schema: minifyIntrospectionQuery(
// eslint-disable-next-line
require('./test-utils/simple_schema.json')
),
})({ forward, client, dispatchDebug })(ops$),
tap(result),
publish
);

next(operation);

console.log(result.mock.calls[0][0])

Check failure on line 1608 in exchanges/graphcache/src/cacheExchange.test.ts

View workflow job for this annotation

GitHub Actions / Checks

Unexpected console statement

Check failure on line 1608 in exchanges/graphcache/src/cacheExchange.test.ts

View workflow job for this annotation

GitHub Actions / Checks

Insert `;`

expect(result.mock.calls[0][0].data).toEqual(null);
});
});

describe('optimistic updates', () => {
Expand Down
3 changes: 2 additions & 1 deletion exchanges/graphcache/src/operations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ const readSelection = (
(directives.optional ||
(optionalRef && !directives.required) ||
!!getFieldError(ctx) ||
(!directives.required && store.schema &&
(!directives.required &&
store.schema &&
isFieldNullable(store.schema, typename, fieldName, ctx.store.logger)))
) {
// The field is uncached or has errored, so it'll be set to null and skipped
Expand Down

0 comments on commit ed68fe5

Please sign in to comment.