Skip to content

Commit

Permalink
Add support for client schema extensions in readUpdatableQuery
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D32908602

fbshipit-source-id: 46600f31f0ca8bbac4b3410265c1487a4d9f122a
  • Loading branch information
rbalicki2 authored and facebook-github-bot committed Dec 12, 2021
1 parent 9a9dc85 commit 45913fc
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 30 deletions.

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

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const updatableQuery = graphql`
name
author {
client_best_friend {
...readUpdatableQueryEXPERIMENTALTest_user
name
}
}
Expand Down Expand Up @@ -716,7 +717,7 @@ describe('readUpdatableQuery', () => {
});
});

it('throws when accessing a client extension field', () => {
it('does not throw when accessing a client extension field', () => {
environment.commitPayload(operation, {
me: {
__typename: 'User',
Expand Down Expand Up @@ -744,11 +745,144 @@ describe('readUpdatableQuery', () => {
expect(() => {
// The author field contains client_best_friend, which is a client extension
updatableData.me?.author;
}).toThrowError();
}).not.toThrowError();
expect(() => {
// The author field contains client_nickname, which is a client extension
updatableData.me?.author2;
}).toThrowError();
}).not.toThrowError();
});
});

it('lets you update client extension linked fields', () => {
environment.commitPayload(operation, {
me: {
__typename: 'User',
id: '4',
name: 'Mark',
author: {
id: '5',
client_best_friend: {
id: '6',
name: 'Sheryl',
},
client_nickname: 'Zucc',
},
},
node: {
id: '4',
__typename: 'User',
name: 'Mark',
},
node2: null,
});

commitLocalUpdate(environment, store => {
const updatableData =
store.readUpdatableQuery_EXPERIMENTAL<readUpdatableQueryEXPERIMENTALTestUpdatableQuery>(
updatableQuery,
{},
);

const source = environment.getStore().getSource();
const selector = operation.fragment;
const readOnlyData = ((RelayReader.read(source, selector) // $FlowFixMe[unclear-type] Just to cast it to a better type!
.data: any): readUpdatableQueryEXPERIMENTALTestRegularQuery['response']);

if (updatableData.me?.author != null) {
updatableData.me.author.client_best_friend = readOnlyData.node;
} else {
throw new Error('Expected author to exist');
}
expect(readOnlyData.node?.name).toBe('Mark');
expect(updatableData.me?.author?.client_best_friend?.name).toBe('Mark');
});
const source = environment.getStore().getSource();
const selector = operation.fragment;
const readOnlyData = ((RelayReader.read(source, selector) // $FlowFixMe[unclear-type] Just to cast it to a better type!
.data: any): readUpdatableQueryEXPERIMENTALTestRegularQuery['response']);
expect(readOnlyData.me?.author?.client_best_friend?.name).toBe('Mark');
});

it('lets you update client extension scalar fields', () => {
environment.commitPayload(operation, {
me: {
__typename: 'User',
id: '4',
name: 'Mark',
author: {
id: '5',
client_best_friend: {
id: '6',
name: 'Sheryl',
},
client_nickname: 'Zucc',
},
},
node: null,
node2: null,
});

commitLocalUpdate(environment, store => {
const updatableData =
store.readUpdatableQuery_EXPERIMENTAL<readUpdatableQueryEXPERIMENTALTestUpdatableQuery>(
updatableQuery,
{},
);

if (updatableData.me?.author2 != null) {
updatableData.me.author2.client_nickname = 'Mr. Right';
} else {
throw new Error('Expected author to exist');
}
expect(updatableData.me?.author2?.client_nickname).toBe('Mr. Right');
});
const source = environment.getStore().getSource();
const selector = operation.fragment;
const readOnlyData = ((RelayReader.read(source, selector) // $FlowFixMe[unclear-type] Just to cast it to a better type!
.data: any): readUpdatableQueryEXPERIMENTALTestRegularQuery['response']);
expect(readOnlyData.me?.author?.client_nickname).toBe('Mr. Right');
});

it('lets you navigate through client extension fields and update nested scalar fields', () => {
environment.commitPayload(operation, {
me: {
__typename: 'User',
id: '4',
name: 'Mark',
author: {
id: '5',
client_best_friend: {
id: '6',
name: 'Sheryl',
},
client_nickname: 'Zucc',
},
},
node: null,
node2: null,
});

commitLocalUpdate(environment, store => {
const updatableData =
store.readUpdatableQuery_EXPERIMENTAL<readUpdatableQueryEXPERIMENTALTestUpdatableQuery>(
updatableQuery,
{},
);

if (updatableData.me?.author?.client_best_friend != null) {
updatableData.me.author.client_best_friend.name = 'Mr. Right';
} else {
throw new Error('Expected author to exist');
}
expect(updatableData.me?.author?.client_best_friend.name).toBe(
'Mr. Right',
);
});

const source = environment.getStore().getSource();
const selector = operation.fragment;
const readOnlyData = ((RelayReader.read(source, selector) // $FlowFixMe[unclear-type] Just to cast it to a better type!
.data: any): readUpdatableQueryEXPERIMENTALTestRegularQuery['response']);
expect(readOnlyData.me?.author?.client_best_friend?.name).toBe('Mr. Right');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ function updateProxyFromSelections<TQuery: OperationType>(
);
}
break;
case 'ClientExtension':
updateProxyFromSelections(
mutableUpdatableProxy,
recordProxy,
queryVariables,
selection.selections,
root,
);
break;
case 'FragmentSpread':
// Explicitly ignore
break;
Expand Down

0 comments on commit 45913fc

Please sign in to comment.