Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix @_required directive when the schema is available #3685

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
100 changes: 100 additions & 0 deletions exchanges/graphcache/src/cacheExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,106 @@ 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 initialQuery = gql`
query {
latestTodo {
id
}
}
`;

const query = gql`
{
latestTodo {
id
author @_required {
id
name
}
}
}
`;

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

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

const initialQueryResult: OperationResult = {
...queryResponse,
operation: initialQueryOperation,
data: {
__typename: 'Query',
latestTodo: {
__typename: 'Todo',
id: '1',
},
},
};

const queryResult: OperationResult = {
...queryResponse,
operation: queryOperation,
data: {
__typename: 'Query',
latestTodo: {
__typename: 'Todo',
id: '1',
author: null,
},
},
};

const response = vi.fn((forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) {
return initialQueryResult;
} else if (forwardOp.key === 2) {
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(initialQueryOperation);
vi.runAllTimers();
next(queryOperation);
vi.runAllTimers();

expect(result.mock.calls[0][0].data).toEqual({
latestTodo: {
id: '1',
},
});
expect(result.mock.calls[1][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) ||
(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