Skip to content

Commit

Permalink
Use Partial<TData> rather than TData | {} (apollographql#2313)
Browse files Browse the repository at this point in the history
* Use Partial<TData> rather than TData | {}

* Fix a few more typings
  • Loading branch information
tgriesser authored and hwillson committed Sep 20, 2018
1 parent c21e005 commit 2f15d9f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 13 deletions.
10 changes: 1 addition & 9 deletions src/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ function observableQueryFields<TData, TVariables>(
export interface QueryResult<TData = any, TVariables = OperationVariables>
extends ObservableQueryFields<TData, TVariables> {
client: ApolloClient<any>;
// we create an empty object to make checking for data
// easier for consumers (i.e. instead of data && data.user
// you can just check data.user) this also makes destructring
// easier (i.e. { data: { user } })
// however, this isn't realy possible with TypeScript that
// I'm aware of. So intead we enforce checking for data
// like so result.data!.user. This tells TS to use TData
// XXX is there a better way to do this?
data: TData | {};
data: Partial<TData>;
error?: ApolloError;
loading: boolean;
networkStatus: NetworkStatus;
Expand Down
2 changes: 1 addition & 1 deletion src/query-hoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function query<
// we massage the Query components shape here to replicate that
const result = Object.assign(r, data || {});
const name = operationOptions.name || 'data';
let childProps = { [name]: result };
let childProps: TChildProps = { [name]: result } as any;
if (operationOptions.props) {
const newResult: OptionProps<TProps, TData, TGraphQLVariables> = {
[name]: result,
Expand Down
2 changes: 1 addition & 1 deletion test/client/Query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ describe('Query component', () => {
function Container() {
return (
<AllPeopleQuery2 query={query} notifyOnNetworkStatusChange>
{(result: any) => {
{result => {
try {
switch (count++) {
case 0:
Expand Down
4 changes: 2 additions & 2 deletions test/client/getDataFromTree.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ describe('SSR', () => {

const WrappedElement = () => (
<CurrentUserQuery query={query}>
{({ data, loading }: { data: Data; loading: boolean }) => (
{({ data, loading }) => (
<div>{loading || !data ? 'loading' : data.currentUser!.firstName}</div>
)}
</CurrentUserQuery>
Expand Down Expand Up @@ -1291,7 +1291,7 @@ describe('SSR', () => {

const Element = (props: { id: string }) => (
<CurrentUserQuery query={query} ssr={false} variables={props}>
{({ data, loading }: { data: Data; loading: boolean }) => (
{({ data, loading }) => (
<div>{loading || !data ? 'loading' : data.currentUser!.firstName}</div>
)}
</CurrentUserQuery>
Expand Down

0 comments on commit 2f15d9f

Please sign in to comment.