Skip to content

Commit

Permalink
Don't use GraphQLResult anymore since it doesn't have errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashko Stubailo committed Jul 11, 2016
1 parent 185293b commit d4bd618
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
42 changes: 22 additions & 20 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

import {
NetworkInterface,
Request,
} from './networkInterface';


import forOwn = require('lodash.forown');
import assign = require('lodash.assign');
import isEqual = require('lodash.isequal');
Expand Down Expand Up @@ -67,20 +65,24 @@ import {
QueryScheduler,
} from './scheduler';

import {
ApolloQueryResult,
} from './index';

import { Observable, Observer, Subscription, SubscriberFunction } from './util/Observable';

import {
ApolloError,
} from './errors';

export class ObservableQuery extends Observable<GraphQLResult> {
public refetch: (variables?: any) => Promise<GraphQLResult>;
export class ObservableQuery extends Observable<ApolloQueryResult> {
public refetch: (variables?: any) => Promise<ApolloQueryResult>;
public stopPolling: () => void;
public startPolling: (p: number) => void;

constructor(options: {
subscriberFunction: SubscriberFunction<GraphQLResult>,
refetch: (variables?: any) => Promise<GraphQLResult>,
subscriberFunction: SubscriberFunction<ApolloQueryResult>,
refetch: (variables?: any) => Promise<ApolloQueryResult>,
stopPolling: () => void,
startPolling: (p: number) => void
}) {
Expand All @@ -90,11 +92,11 @@ export class ObservableQuery extends Observable<GraphQLResult> {
this.startPolling = options.startPolling;
}

public subscribe(observer: Observer<GraphQLResult>): Subscription {
public subscribe(observer: Observer<ApolloQueryResult>): Subscription {
return super.subscribe(observer);
}

public result(): Promise<GraphQLResult> {
public result(): Promise<ApolloQueryResult> {
return new Promise((resolve, reject) => {
const subscription = this.subscribe({
next(result) {
Expand Down Expand Up @@ -141,8 +143,8 @@ export class QueryManager {
// track of queries that are inflight and reject them in case some
// destabalizing action occurs (e.g. reset of the Apollo store).
private fetchQueryPromises: { [requestId: string]: {
promise: Promise<GraphQLResult>;
resolve: (result: GraphQLResult) => void;
promise: Promise<ApolloQueryResult>;
resolve: (result: ApolloQueryResult) => void;
reject: (error: Error) => void;
} };

Expand Down Expand Up @@ -221,7 +223,7 @@ export class QueryManager {
variables?: Object,
resultBehaviors?: MutationBehavior[],
fragments?: FragmentDefinition[],
}): Promise<GraphQLResult> {
}): Promise<ApolloQueryResult> {
const mutationId = this.generateQueryId();

let mutationDef = getMutationDefinition(mutation);
Expand Down Expand Up @@ -274,7 +276,7 @@ export class QueryManager {
// results (or lack thereof) for a particular query.
public queryListenerForObserver(
options: WatchQueryOptions,
observer: Observer<GraphQLResult>
observer: Observer<ApolloQueryResult>
): QueryListener {
return (queryStoreValue: QueryStoreValue) => {
// The query store value can be undefined in the event of a store
Expand Down Expand Up @@ -335,7 +337,7 @@ export class QueryManager {

let observableQuery;

const subscriberFunction = (observer: Observer<GraphQLResult>) => {
const subscriberFunction = (observer: Observer<ApolloQueryResult>) => {
const retQuerySubscription = {
unsubscribe: () => {
this.stopQuery(queryId);
Expand Down Expand Up @@ -392,7 +394,7 @@ export class QueryManager {
return observableQuery;
}

public query(options: WatchQueryOptions): Promise<GraphQLResult> {
public query(options: WatchQueryOptions): Promise<ApolloQueryResult> {
if (options.returnPartialData) {
throw new Error('returnPartialData option only supported on watchQuery.');
}
Expand All @@ -417,7 +419,7 @@ export class QueryManager {
return resPromise;
}

public fetchQuery(queryId: string, options: WatchQueryOptions): Promise<GraphQLResult> {
public fetchQuery(queryId: string, options: WatchQueryOptions): Promise<ApolloQueryResult> {
return this.fetchQueryOverInterface(queryId, options, this.networkInterface);
}

Expand Down Expand Up @@ -447,8 +449,8 @@ export class QueryManager {
}

// Adds a promise to this.fetchQueryPromises for a given request ID.
public addFetchQueryPromise(requestId: number, promise: Promise<GraphQLResult>,
resolve: (result: GraphQLResult) => void,
public addFetchQueryPromise(requestId: number, promise: Promise<ApolloQueryResult>,
resolve: (result: ApolloQueryResult) => void,
reject: (error: Error) => void) {
this.fetchQueryPromises[requestId.toString()] = { promise, resolve, reject };
}
Expand Down Expand Up @@ -512,7 +514,7 @@ export class QueryManager {
queryId: string,
options: WatchQueryOptions,
network: NetworkInterface
): Promise<GraphQLResult> {
): Promise<ApolloQueryResult> {
const {
query,
variables,
Expand Down Expand Up @@ -631,12 +633,12 @@ export class QueryManager {
operationName: request.operationName,
};

const retPromise = new Promise<GraphQLResult>((resolve, reject) => {
const retPromise = new Promise<ApolloQueryResult>((resolve, reject) => {
this.addFetchQueryPromise(requestId, retPromise, resolve, reject);

return this.batcher.enqueueRequest(fetchRequest)
.then((result: GraphQLResult) => {
// XXX handle multiple GraphQLResults
// XXX handle multiple ApolloQueryResults
this.store.dispatch({
type: 'APOLLO_QUERY_RESULT',
result,
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from './networkInterface';

import {
GraphQLResult,
Document,
FragmentDefinition,
} from 'graphql';
Expand Down Expand Up @@ -77,6 +76,12 @@ export {
print as printAST,
};

export type ApolloQueryResult = {
data: any;
// Right now only has one property, but will later include loading state, and possibly other info
// This is different from the GraphQLResult type because it doesn't include errors - those are
// thrown via the standard promise/observer catch mechanism
}

// A map going from the name of a fragment to that fragment's definition.
// The point is to keep track of fragments that exist and print a warning if we encounter two
Expand Down Expand Up @@ -204,7 +209,7 @@ export default class ApolloClient {
return this.queryManager.watchQuery(options);
};

public query = (options: WatchQueryOptions): Promise<GraphQLResult> => {
public query = (options: WatchQueryOptions): Promise<ApolloQueryResult> => {
this.initStore();

if (!this.shouldForceFetch && options.forceFetch) {
Expand All @@ -226,7 +231,7 @@ export default class ApolloClient {
resultBehaviors?: MutationBehavior[],
variables?: Object,
fragments?: FragmentDefinition[],
}): Promise<GraphQLResult> => {
}): Promise<ApolloQueryResult> => {
this.initStore();
return this.queryManager.mutate(options);
};
Expand Down

0 comments on commit d4bd618

Please sign in to comment.