Skip to content

Commit

Permalink
add generic types to apollo client for Query and Mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Jurgens committed Nov 15, 2016
1 parent 99bc005 commit 0c2aa46
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 98 deletions.
20 changes: 10 additions & 10 deletions src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ function defaultReduxRootSelector(state: any) {
* receive results from the server and cache the results in a Redux store. It also delivers updates
* to GraphQL queries through {@link Observable} instances.
*/
export default class ApolloClient {
export default class ApolloClient<QueryType, MutationType> {
public networkInterface: NetworkInterface;
public store: ApolloStore;
public reduxRootKey: string;
public reduxRootSelector: ApolloStateSelector | null;
public initialState: any;
public queryManager: QueryManager;
public queryManager: QueryManager<QueryType, MutationType>;
public reducerConfig: ApolloReducerConfig;
public addTypename: boolean;
public resultTransformer: ResultTransformer;
public resultComparator: ResultComparator;
public resultTransformer: ResultTransformer<QueryType | MutationType>;
public resultComparator: ResultComparator<QueryType | MutationType>;
public shouldForceFetch: boolean;
public dataId: IdGetter;
public fieldWithArgs: (fieldName: string, args?: Object) => string;
Expand Down Expand Up @@ -151,8 +151,8 @@ export default class ApolloClient {
reduxRootSelector?: string | ApolloStateSelector,
initialState?: any,
dataIdFromObject?: IdGetter,
resultTransformer?: ResultTransformer,
resultComparator?: ResultComparator,
resultTransformer?: ResultTransformer<QueryType | MutationType>,
resultComparator?: ResultComparator<QueryType | MutationType>,
ssrMode?: boolean,
ssrForceFetchDelay?: number
mutationBehaviorReducers?: MutationBehaviorReducerMap,
Expand Down Expand Up @@ -233,7 +233,7 @@ export default class ApolloClient {
* a description of store reactivity.
*
*/
public watchQuery(options: DeprecatedWatchQueryOptions): ObservableQuery {
public watchQuery(options: DeprecatedWatchQueryOptions): ObservableQuery<QueryType, MutationType> {
this.initStore();

if (!this.shouldForceFetch && options.forceFetch) {
Expand Down Expand Up @@ -267,7 +267,7 @@ export default class ApolloClient {
* how this query should be treated e.g. whether it is a polling query, whether it should hit the
* server at all or just resolve from the cache, etc.
*/
public query(options: DeprecatedWatchQueryOptions): Promise<ApolloQueryResult> {
public query(options: DeprecatedWatchQueryOptions): Promise<ApolloQueryResult<QueryType>> {
this.initStore();

// XXX what if I pass pollInterval? Will it just keep running?
Expand Down Expand Up @@ -326,7 +326,7 @@ export default class ApolloClient {
* for this, you can simply refetch the queries that will be affected and achieve a consistent
* store once these queries return.
*/
public mutate(options: MutationOptions): Promise<ApolloQueryResult> {
public mutate(options: MutationOptions): Promise<ApolloQueryResult<MutationType>> {
this.initStore();

// We add the fragments to the document to pass only the document around internally.
Expand Down Expand Up @@ -427,7 +427,7 @@ export default class ApolloClient {

this.store = store;

this.queryManager = new QueryManager({
this.queryManager = new QueryManager<QueryType, MutationType>({
networkInterface: this.networkInterface,
reduxRootSelector: reduxRootSelector,
store,
Expand Down
30 changes: 15 additions & 15 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface UpdateQueryOptions {
variables: Object;
}

export class ObservableQuery extends Observable<ApolloQueryResult> {
export class ObservableQuery<QueryType, MutationType> extends Observable<ApolloQueryResult<QueryType | MutationType>> {
public options: WatchQueryOptions;
public queryId: string;
/**
Expand All @@ -59,28 +59,28 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {

private isPollingQuery: boolean;
private shouldSubscribe: boolean;
private scheduler: QueryScheduler;
private queryManager: QueryManager;
private observers: Observer<ApolloQueryResult>[];
private scheduler: QueryScheduler<QueryType, MutationType>;
private queryManager: QueryManager<QueryType, MutationType>;
private observers: Observer<ApolloQueryResult<QueryType | MutationType>>[];
private subscriptionHandles: Subscription[];

private lastResult: ApolloQueryResult;
private lastResult: ApolloQueryResult<QueryType | MutationType>;
private lastError: ApolloError;

constructor({
scheduler,
options,
shouldSubscribe = true,
}: {
scheduler: QueryScheduler,
scheduler: QueryScheduler<QueryType, MutationType>,
options: WatchQueryOptions,
shouldSubscribe?: boolean,
}) {
const queryManager = scheduler.queryManager;
const queryId = queryManager.generateQueryId();
const isPollingQuery = !!options.pollInterval;

const subscriberFunction = (observer: Observer<ApolloQueryResult>) => {
const subscriberFunction = (observer: Observer<ApolloQueryResult<QueryType | MutationType>>) => {
return this.onSubscribe(observer);
};

Expand All @@ -97,7 +97,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {
this.subscriptionHandles = [];
}

public result(): Promise<ApolloQueryResult> {
public result(): Promise<ApolloQueryResult<QueryType | MutationType>> {
return new Promise((resolve, reject) => {
const subscription = this.subscribe({
next(result) {
Expand Down Expand Up @@ -149,7 +149,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {
return { data, loading, networkStatus };
}

public refetch(variables?: any): Promise<ApolloQueryResult> {
public refetch(variables?: any): Promise<ApolloQueryResult<QueryType | MutationType>> {
this.variables = assign({}, this.variables, variables);

if (this.options.noFetch) {
Expand All @@ -172,7 +172,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {

public fetchMore(
fetchMoreOptions: FetchMoreQueryOptions & FetchMoreOptions
): Promise<ApolloQueryResult> {
): Promise<ApolloQueryResult<QueryType | MutationType>> {
return Promise.resolve()
.then(() => {
const qid = this.queryManager.generateQueryId();
Expand Down Expand Up @@ -262,7 +262,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {
};
}

public setOptions(opts: ModifiableWatchQueryOptions): Promise<ApolloQueryResult> {
public setOptions(opts: ModifiableWatchQueryOptions): Promise<ApolloQueryResult<QueryType | MutationType>> {
const oldOptions = this.options;
this.options = assign({}, this.options, opts) as WatchQueryOptions;

Expand Down Expand Up @@ -291,7 +291,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {
* @param variables: The new set of variables. If there are missing variables,
* the previous values of those variables will be used.
*/
public setVariables(variables: any): Promise<ApolloQueryResult> {
public setVariables(variables: any): Promise<ApolloQueryResult<QueryType | MutationType>> {
const newVariables = assign({}, this.variables, variables);

if (isEqual(newVariables, this.variables)) {
Expand Down Expand Up @@ -346,7 +346,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {
this.scheduler.startPollingQuery(this.options, this.queryId, false);
}

private onSubscribe(observer: Observer<ApolloQueryResult>) {
private onSubscribe(observer: Observer<ApolloQueryResult<QueryType | MutationType>>) {
this.observers.push(observer);

// Deliver initial result
Expand Down Expand Up @@ -391,8 +391,8 @@ export class ObservableQuery extends Observable<ApolloQueryResult> {
);
}

const observer: Observer<ApolloQueryResult> = {
next: (result: ApolloQueryResult) => {
const observer: Observer<ApolloQueryResult<QueryType | MutationType>> = {
next: (result: ApolloQueryResult<QueryType | MutationType>) => {
this.observers.forEach((obs) => {
if (obs.next) {
obs.next(result);
Expand Down
Loading

0 comments on commit 0c2aa46

Please sign in to comment.