Skip to content

Commit

Permalink
6/n - Add tests for errorResponseFields
Browse files Browse the repository at this point in the history
Reviewed By: tyao1

Differential Revision: D51944691

fbshipit-source-id: 61b3fcfd50ac7927514a9913d53ceb8fb486cf7c
  • Loading branch information
itamark authored and facebook-github-bot committed Jan 22, 2024
1 parent 27a0623 commit 399bf92
Show file tree
Hide file tree
Showing 8 changed files with 872 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall relay
*/

'use strict';

const {graphql} = require('../../query/GraphQLTag');
const RelayFeatureFlags = require('../../util/RelayFeatureFlags');
const {
createOperationDescriptor,
} = require('../RelayModernOperationDescriptor');
const {read} = require('../RelayReader');
const RelayRecordSource = require('../RelayRecordSource');

describe('RelayReader error fields', () => {
describe('when field error handling is enabled', () => {
beforeAll(() => {
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING = true;
});

const wasFieldErrorHandlingEnabled =
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING;

it('adds the errors to errorResponseFields', () => {
const source = RelayRecordSource.create({
'client:root': {
__id: 'client:root',
__typename: '__Root',
me: {__ref: '1'},
},
'1': {
__id: '1',
id: '1',
__typename: 'User',
lastName: null,
__errors: {
lastName: [
{
message: 'There was an error!',
path: ['me', 'lastName'],
},
],
},
},
});

const FooQuery = graphql`
query RelayReaderRelayErrorHandlingTest1Query {
me {
lastName
}
}
`;
const operation = createOperationDescriptor(FooQuery, {id: '1'});
const {data, errorResponseFields} = read(source, operation.fragment);
expect(data).toEqual({me: {lastName: null}});
expect(errorResponseFields).toEqual([
{
owner: 'RelayReaderRelayErrorHandlingTest1Query',
path: 'me.lastName',
error: {message: 'There was an error!', path: ['me', 'lastName']},
},
]);
});

afterAll(() => {
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING =
wasFieldErrorHandlingEnabled;
});
});

describe('when field error handling is disabled', () => {
beforeAll(() => {
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING = false;
});

const wasFieldErrorHandlingEnabled =
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING;

it('errorResponseFields is null', () => {
const source = RelayRecordSource.create({
'client:root': {
__id: 'client:root',
__typename: '__Root',
me: {__ref: '1'},
},
'1': {
__id: '1',
id: '1',
__typename: 'User',
lastName: null,
__errors: {
lastName: [
{
message: 'There was an error!',
path: ['me', 'lastName'],
},
],
},
},
});

const FooQuery = graphql`
query RelayReaderRelayErrorHandlingTest2Query {
me {
lastName
}
}
`;
const operation = createOperationDescriptor(FooQuery, {id: '1'});
const {data, errorResponseFields} = read(source, operation.fragment);
expect(data).toEqual({me: {lastName: null}});
expect(errorResponseFields).toEqual(null);
});

afterAll(() => {
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING =
wasFieldErrorHandlingEnabled;
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,90 @@ describe('Relay Resolver', () => {
);
});

describe('Relay resolver - Field Error Handling', () => {
it('propagates errors from the resolver up to the reader', () => {
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING = true;
const source = RelayRecordSource.create({
'client:root': {
__id: 'client:root',
__typename: '__Root',
me: {__ref: '1'},
},
'1': {
__id: '1',
id: '1',
__typename: 'User',
lastName: null,
__errors: {
lastName: [
{
message: 'There was an error!',
path: ['me', 'lastName'],
},
],
},
},
});

const FooQuery = graphql`
query RelayReaderResolverTestFieldErrorQuery {
me {
lastName
}
}
`;

const operation = createOperationDescriptor(FooQuery, {});
const store = new RelayModernStore(source, {gcReleaseBufferSize: 0});
const {errorResponseFields} = store.lookup(operation.fragment);
expect(errorResponseFields).toEqual([
{
error: {message: 'There was an error!', path: ['me', 'lastName']},
owner: 'RelayReaderResolverTestFieldErrorQuery',
path: 'me.lastName',
},
]);
});

it("doesn't propagate errors from the resolver up to the reader when flag is disabled", () => {
RelayFeatureFlags.ENABLE_FIELD_ERROR_HANDLING = false;
const source = RelayRecordSource.create({
'client:root': {
__id: 'client:root',
__typename: '__Root',
me: {__ref: '1'},
},
'1': {
__id: '1',
id: '1',
__typename: 'User',
lastName: null,
__errors: {
lastName: [
{
message: 'There was an error!',
path: ['me', 'lastName'],
},
],
},
},
});

const FooQuery = graphql`
query RelayReaderResolverTestFieldError1Query {
me {
lastName
}
}
`;

const operation = createOperationDescriptor(FooQuery, {});
const store = new RelayModernStore(source, {gcReleaseBufferSize: 0});
const {errorResponseFields} = store.lookup(operation.fragment);
expect(errorResponseFields).toEqual(null);
});
});

it('propagates @required errors from the resolver up to the reader', () => {
const source = RelayRecordSource.create({
'client:root': {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 399bf92

Please sign in to comment.