Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 4a9ffec

Browse files
author
James Baxley
authored
added link to recompose's compose method (#194)
* added link to recompose's compose method * update changelog and version
1 parent 76a2108 commit 4a9ffec

File tree

6 files changed

+95
-44
lines changed

6 files changed

+95
-44
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 1 or 2 months), so that we can take advantage of SemVer to signify breaking changes from that point on.
44

5+
### vNext
6+
7+
### v0.5.1
8+
9+
- Feature: Added link to [recompose](https://github.com/acdlite/recompose) to use the `compose` function. This makes it easy to combine multiple queries on a single component. [#194](https://github.com/apollostack/react-apollo/pull/194)
510

611
### v0.5.0
712

global.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ declare module 'lodash.isequal' {
1111
export = main.isEqual;
1212
}
1313

14+
declare module 'recompose/compose' {
15+
function hoc(component: any): any;
16+
export default (...hocs) => hoc;
17+
}
1418

1519
declare module 'hoist-non-react-statics' {
1620
/**

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-apollo",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "React data container for Apollo Client",
55
"main": "index.js",
66
"scripts": {
@@ -101,6 +101,7 @@
101101
"lodash.flatten": "^4.2.0",
102102
"lodash.isequal": "^4.1.1",
103103
"lodash.isobject": "^3.0.2",
104-
"object-assign": "^4.0.1"
104+
"object-assign": "^4.0.1",
105+
"recompose": "^0.20.2"
105106
}
106107
}

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import ApolloProvider from './ApolloProvider';
22
import graphql, { withApollo } from './graphql';
33

4-
export { ApolloProvider, graphql, withApollo };
4+
// expose easy way to join queries from recompose
5+
import compose from 'recompose/compose';
6+
7+
export { ApolloProvider, graphql, withApollo, compose };

test/react-web/client/graphql/shared-operations.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from '../../../mocks/components';
2121

2222
import graphql, { withApollo } from '../../../../src/graphql';
23+
import { compose } from '../../../../src/';
2324

2425
describe('shared opertations', () => {
2526

@@ -199,4 +200,39 @@ describe('shared opertations', () => {
199200
}, 25);
200201
});
201202

203+
describe('compose', () => {
204+
it('binds two queries to props with different syntax', () => {
205+
const peopleQuery = gql`query people { allPeople(first: 1) { people { name } } }`;
206+
const peopleData = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
207+
208+
const shipsQuery = gql`query ships { allships(first: 1) { ships { name } } }`;
209+
const shipsData = { allships: { ships: [ { name: 'Tie Fighter' } ] } };
210+
211+
212+
const networkInterface = mockNetworkInterface(
213+
{ request: { query: peopleQuery }, result: { data: peopleData } },
214+
{ request: { query: shipsQuery }, result: { data: shipsData } }
215+
);
216+
const client = new ApolloClient({ networkInterface });
217+
218+
const enhanced = compose(
219+
graphql(peopleQuery, { name: 'people' }),
220+
graphql(shipsQuery, { name: 'ships' })
221+
);
222+
223+
const ContainerWithData = enhanced((props) => {
224+
const { people, ships } = props;
225+
expect(people).to.exist;
226+
expect(people.loading).to.be.true;
227+
228+
expect(ships).to.exist;
229+
expect(ships.loading).to.be.true;
230+
return null;
231+
});
232+
233+
const wrapper = mount(<ProviderMock client={client}><ContainerWithData /></ProviderMock>);
234+
(wrapper as any).unmount();
235+
});
236+
});
237+
202238
});

typings/modules/apollo-client/index.d.ts

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export interface NetworkInterface {
5252
[others: string]: any;
5353
query(request: Request): Promise<GraphQLResult>;
5454
}
55+
export interface SubscriptionNetworkInterface extends NetworkInterface {
56+
subscribe(request: Request, handler: (error, result) => void): number;
57+
unsubscribe(id: Number): void;
58+
}
5559
export interface BatchedNetworkInterface extends NetworkInterface {
5660
batchQuery(requests: Request[]): Promise<GraphQLResult[]>;
5761
}
@@ -389,6 +393,14 @@ import { Observer, Subscription } from '~apollo-client/util/Observable';
389393
import { WatchQueryOptions } from '~apollo-client/watchQueryOptions';
390394
import { ObservableQuery } from '~apollo-client/ObservableQuery';
391395
export type QueryListener = (queryStoreValue: QueryStoreValue) => void;
396+
export interface SubscriptionOptions {
397+
query: Document;
398+
variables?: {
399+
[key: string]: any;
400+
};
401+
fragments?: FragmentDefinition[];
402+
handler: (error: Object, result: Object) => void;
403+
}
392404
export class QueryManager {
393405
pollingTimers: {
394406
[queryId: string]: NodeJS.Timer | any;
@@ -441,6 +453,7 @@ export class QueryManager {
441453
removeObservableQuery(queryId: string): void;
442454
resetStore(): void;
443455
startQuery(queryId: string, options: WatchQueryOptions, listener: QueryListener): string;
456+
startSubscription(options: SubscriptionOptions): number;
444457
stopQuery(queryId: string): void;
445458
getQueryWithPreviousResult(queryId: string, isOptimistic?: boolean): {
446459
previousResult: Object;
@@ -451,7 +464,10 @@ export class QueryManager {
451464
queryFragments: FragmentDefinition[];
452465
};
453466
private collectResultBehaviorsFromUpdateQueries(updateQueries, mutationResult, isOptimistic?);
454-
private fetchQueryOverInterface(queryId, options, network);
467+
private transformQueryDocument(options);
468+
private handleDiffQuery({queryDef, rootId, variables, fragmentMap, noFetch});
469+
private fetchRequest({requestId, queryId, query, querySS, options, fragmentMap, networkInterface});
470+
private fetchQueryOverInterface(queryId, options, networkInterface);
455471
private refetchQueryByName(queryName);
456472
private isDifferentResult(queryId, result);
457473
private broadcastQueries();
@@ -465,7 +481,7 @@ export * from '~apollo-client/QueryManager';
465481
// Generated by typings
466482
// Source: node_modules/apollo-client/ObservableQuery.d.ts
467483
declare module '~apollo-client/ObservableQuery' {
468-
import { WatchQueryOptions, FetchMoreQueryOptions } from '~apollo-client/watchQueryOptions';
484+
import { WatchQueryOptions, FetchMoreQueryOptions, GraphQLSubscriptionOptions } from '~apollo-client/watchQueryOptions';
469485
import { Observable } from '~apollo-client/util/Observable';
470486
import { QueryScheduler } from '~apollo-client/scheduler';
471487
import { ApolloQueryResult } from '~apollo-client/index';
@@ -475,9 +491,14 @@ export interface FetchMoreOptions {
475491
queryVariables: Object;
476492
}) => Object;
477493
}
494+
export interface UpdateQueryOptions {
495+
queryVariables: Object;
496+
}
478497
export class ObservableQuery extends Observable<ApolloQueryResult> {
479498
refetch: (variables?: any) => Promise<ApolloQueryResult>;
480499
fetchMore: (options: FetchMoreQueryOptions & FetchMoreOptions) => Promise<any>;
500+
startGraphQLSubscription: (options: GraphQLSubscriptionOptions) => number;
501+
updateQuery: (mapFn: (previousQueryResult: any, options: UpdateQueryOptions) => any) => void;
481502
stopPolling: () => void;
482503
startPolling: (p: number) => void;
483504
options: WatchQueryOptions;
@@ -517,6 +538,17 @@ export interface FetchMoreQueryOptions {
517538
[key: string]: any;
518539
};
519540
}
541+
export interface GraphQLSubscriptionOptions {
542+
subscription: Document;
543+
variables?: {
544+
[key: string]: any;
545+
};
546+
fragments?: FragmentDefinition[];
547+
updateQuery: (previousQueryResult: Object, options: {
548+
subscriptionResult: Object;
549+
queryVariables: Object;
550+
}) => Object;
551+
}
520552
}
521553
declare module 'apollo-client/watchQueryOptions' {
522554
export * from '~apollo-client/watchQueryOptions';
@@ -722,7 +754,7 @@ export * from '~apollo-client/data/mutationResults';
722754
// Source: node_modules/apollo-client/index.d.ts
723755
declare module '~apollo-client/index' {
724756
import { NetworkInterface, createNetworkInterface, addQueryMerging } from '~apollo-client/networkInterface';
725-
import { Document, FragmentDefinition, SelectionSet } from 'graphql';
757+
import { Document, FragmentDefinition } from 'graphql';
726758
import { print } from 'graphql-tag/printer';
727759
import { createApolloStore, ApolloStore, createApolloReducer, ApolloReducerConfig } from '~apollo-client/store';
728760
import { QueryManager } from '~apollo-client/QueryManager';
@@ -732,7 +764,7 @@ import { readQueryFromStore, readFragmentFromStore } from '~apollo-client/data/r
732764
import { writeQueryToStore, writeFragmentToStore } from '~apollo-client/data/writeToStore';
733765
import { IdGetter } from '~apollo-client/data/extensions';
734766
import { QueryTransformer, addTypenameToSelectionSet } from '~apollo-client/queries/queryTransform';
735-
import { MutationBehaviorReducerMap } from '~apollo-client/data/mutationResults';
767+
import { MutationBehavior, MutationBehaviorReducerMap, MutationQueryReducersMap } from '~apollo-client/data/mutationResults';
736768
export { createNetworkInterface, addQueryMerging, createApolloStore, createApolloReducer, readQueryFromStore, readFragmentFromStore, addTypenameToSelectionSet as addTypename, writeQueryToStore, writeFragmentToStore, print as printAST };
737769
export type ApolloQueryResult = {
738770
data: any;
@@ -770,51 +802,21 @@ export default class ApolloClient {
770802
mutationBehaviorReducers?: MutationBehaviorReducerMap;
771803
batchInterval?: number;
772804
});
773-
watchQuery: (options: WatchQueryOptions) => ObservableQuery;
774-
query: (options: WatchQueryOptions) => Promise<{
775-
data: any;
776-
loading: boolean;
777-
}>;
778-
mutate: (options: {
805+
watchQuery(options: WatchQueryOptions): ObservableQuery;
806+
query(options: WatchQueryOptions): Promise<ApolloQueryResult>;
807+
mutate(options: {
779808
mutation: Document;
780809
variables?: Object;
781-
resultBehaviors?: ({
782-
type: "ARRAY_INSERT";
783-
resultPath: (string | number)[];
784-
storePath: (string | number)[];
785-
where: "PREPEND" | "APPEND";
786-
} | {
787-
type: "ARRAY_DELETE";
788-
storePath: (string | number)[];
789-
dataId: string;
790-
} | {
791-
type: "DELETE";
792-
dataId: string;
793-
} | {
794-
type: "QUERY_RESULT";
795-
queryVariables: any;
796-
querySelectionSet: SelectionSet;
797-
queryFragments: FragmentDefinition[];
798-
newResult: Object;
799-
})[];
810+
resultBehaviors?: MutationBehavior[];
800811
fragments?: FragmentDefinition[];
801812
optimisticResponse?: Object;
802-
updateQueries?: {
803-
[queryName: string]: (previousResult: Object, options: {
804-
mutationResult: Object;
805-
queryName: Object;
806-
queryVariables: Object;
807-
}) => Object;
808-
};
813+
updateQueries?: MutationQueryReducersMap;
809814
refetchQueries?: string[];
810-
}) => Promise<{
811-
data: any;
812-
loading: boolean;
813-
}>;
815+
}): Promise<ApolloQueryResult>;
814816
reducer(): Function;
815817
middleware: () => (store: ApolloStore) => (next: any) => (action: any) => any;
816818
initStore(): void;
817-
private setStore;
819+
private setStore(store);
818820
}
819821
}
820822
declare module 'apollo-client/index' {

0 commit comments

Comments
 (0)