All files / src Query.tsx

98.93% Statements 185/187
95.79% Branches 91/95
100% Functions 26/26
99.4% Lines 166/167
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 47527x 27x 27x                         27x 27x     27x 27x                               287x   2296x 1026x     2296x                 594x                       594x                                                                                       27x     27x           27x                                             237x             237x       237x   237x 237x       27x 32x     31x 31x 31x 31x 31x 31x 31x 31x 31x 31x     31x 31x 29x 2x     29x       29x   29x     27x 138x 138x   128x 128x 1x 1x           27x   44x 4x 4x     40x   40x       40x 8x 8x 8x 8x 8x     40x 3x     40x 38x 36x     27x 54x 54x     27x 234x 234x 7x 7x 7x 3x 4x 1x         467x 467x 467x 467x 70x   397x     27x   293x 293x 293x 293x 293x 293x 586x 586x     293x   293x             287x                       27x 245x   241x 241x     27x 281x 270x             27x   48x 8x   40x     48x         1x     237x 181x   159x 159x 213x     213x 15x 15x     198x 198x     17x   17x   17x         237x 86x 82x 82x       27x 17x   17x 17x         17x 17x 17x     237x   215x     237x 594x   594x         594x 23x               571x 571x 571x       571x 2x     571x   571x 280x 291x 36x       255x 255x 255x                           1x 1x 1x     254x 254x                                             593x 366x   366x 2x 1x   1x 1x           593x 593x   27x  
import * as React from 'react';
import * as PropTypes from 'prop-types';
import ApolloClient, {
  ObservableQuery,
  ApolloError,
  FetchPolicy,
  ErrorPolicy,
  ApolloQueryResult,
  NetworkStatus,
  FetchMoreOptions,
  FetchMoreQueryOptions,
} from 'apollo-client';
import { DocumentNode } from 'graphql';
import { ZenObservable } from 'zen-observable-ts';
import { OperationVariables, GraphqlQueryControls } from './types';
import { parser, DocumentType, IDocumentDefinition } from './parser';
import { getClient } from './component-utils';
import { RenderPromises } from './getDataFromTree';
 
const shallowEqual = require('fbjs/lib/shallowEqual');
const invariant = require('invariant');
 
export type ObservableQueryFields<TData, TVariables> = Pick<
  ObservableQuery<TData, TVariables>,
  'startPolling' | 'stopPolling' | 'subscribeToMore' | 'updateQuery' | 'refetch' | 'variables'
> & {
  fetchMore: (<K extends keyof TVariables>(
    fetchMoreOptions: FetchMoreQueryOptions<TVariables, K> & FetchMoreOptions<TData, TVariables>,
  ) => Promise<ApolloQueryResult<TData>>) &
    (<TData2, TVariables2, K extends keyof TVariables2>(
      fetchMoreOptions: { query: DocumentNode } & FetchMoreQueryOptions<TVariables2, K> &
        FetchMoreOptions<TData2, TVariables2>,
    ) => Promise<ApolloQueryResult<TData2>>);
};
 
function compact(obj: any) {
  return Object.keys(obj).reduce(
    (acc, key) => {
      if (obj[key] !== undefined) {
        acc[key] = obj[key];
      }
 
      return acc;
    },
    {} as any,
  );
}
 
function observableQueryFields<TData, TVariables>(
  observable: ObservableQuery<TData>,
): ObservableQueryFields<TData, TVariables> {
  const fields = {
    variables: observable.variables,
    refetch: observable.refetch.bind(observable),
    fetchMore: observable.fetchMore.bind(observable),
    updateQuery: observable.updateQuery.bind(observable),
    startPolling: observable.startPolling.bind(observable),
    stopPolling: observable.stopPolling.bind(observable),
    subscribeToMore: observable.subscribeToMore.bind(observable),
  };
  // TODO: Need to cast this because we improved the type of `updateQuery` to be parametric
  // on variables, while the type in Apollo client just has object.
  // Consider removing this when that is properly typed
  return fields as 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 | undefined;
  error?: ApolloError;
  loading: boolean;
  networkStatus: NetworkStatus;
}
 
export interface QueryProps<TData = any, TVariables = OperationVariables> {
  children: (result: QueryResult<TData, TVariables>) => React.ReactNode;
  fetchPolicy?: FetchPolicy;
  errorPolicy?: ErrorPolicy;
  notifyOnNetworkStatusChange?: boolean;
  pollInterval?: number;
  query: DocumentNode;
  variables?: TVariables;
  ssr?: boolean;
  displayName?: string;
  skip?: boolean;
  client?: ApolloClient<Object>;
  context?: Record<string, any>;
  partialRefetch?: boolean;
  onCompleted?: (data: TData | {}) => void;
  onError?: (error: ApolloError) => void;
}
 
export interface QueryContext {
  client?: ApolloClient<Object>;
  operations?: Map<string, { query: DocumentNode; variables: any }>;
  renderPromises?: RenderPromises;
}
 
export default class Query<TData = any, TVariables = OperationVariables> extends React.Component<
  QueryProps<TData, TVariables>
> {
  static contextTypes = {
    client: PropTypes.object,
    operations: PropTypes.object,
    renderPromises: PropTypes.object,
  };
 
  static propTypes = {
    client: PropTypes.object,
    children: PropTypes.func.isRequired,
    fetchPolicy: PropTypes.string,
    notifyOnNetworkStatusChange: PropTypes.bool,
    onCompleted: PropTypes.func,
    onError: PropTypes.func,
    pollInterval: PropTypes.number,
    query: PropTypes.object.isRequired,
    variables: PropTypes.object,
    ssr: PropTypes.bool,
    partialRefetch: PropTypes.bool,
  };
 
  context: QueryContext | undefined;
 
  private client: ApolloClient<Object>;
 
  // request / action storage. Note that we delete querySubscription if we
  // unsubscribe but never delete queryObservable once it is created. We
  // only delete queryObservable when we unmount the component.
  private queryObservable?: ObservableQuery<TData> | null;
  private querySubscription?: ZenObservable.Subscription;
  private previousData: any = {};
  private refetcherQueue?: {
    args: any;
    resolve: (value?: any | PromiseLike<any>) => void;
    reject: (reason?: any) => void;
  };
 
  private hasMounted: boolean = false;
  private operation?: IDocumentDefinition;
 
  constructor(props: QueryProps<TData, TVariables>, context: QueryContext) {
    super(props, context);
 
    this.client = getClient(props, context);
    this.initializeQueryObservable(props);
  }
 
  // For server-side rendering (see getDataFromTree.ts)
  fetchData(): Promise<ApolloQueryResult<any>> | boolean {
    if (this.props.skip) return false;
 
    // pull off react options
    const {
      children,
      ssr,
      displayName,
      skip,
      client,
      onCompleted,
      onError,
      partialRefetch,
      ...opts
    } = this.props;
 
    let { fetchPolicy } = opts;
    if (ssr === false) return false;
    if (fetchPolicy === 'network-only' || fetchPolicy === 'cache-and-network') {
      fetchPolicy = 'cache-first'; // ignore force fetch in SSR;
    }
 
    const observable = this.client.watchQuery({
      ...opts,
      fetchPolicy,
    });
    const result = this.queryObservable!.currentResult();
 
    return result.loading ? observable.result() : false;
  }
 
  componentDidMount() {
    this.hasMounted = true;
    if (this.props.skip) return;
 
    this.startQuerySubscription();
    if (this.refetcherQueue) {
      const { args, resolve, reject } = this.refetcherQueue;
      this.queryObservable!.refetch(args)
        .then(resolve)
        .catch(reject);
    }
  }
 
  componentWillReceiveProps(nextProps: QueryProps<TData, TVariables>, nextContext: QueryContext) {
    // the next render wants to skip
    if (nextProps.skip && !this.props.skip) {
      this.removeQuerySubscription();
      return;
    }
 
    const nextClient = getClient(nextProps, nextContext);
 
    Iif (shallowEqual(this.props, nextProps) && this.client === nextClient) {
      return;
    }
 
    if (this.client !== nextClient) {
      this.client = nextClient;
      this.removeQuerySubscription();
      this.queryObservable = null;
      this.previousData = {};
      this.updateQuery(nextProps);
    }
 
    if (this.props.query !== nextProps.query) {
      this.removeQuerySubscription();
    }
 
    this.updateQuery(nextProps);
    if (nextProps.skip) return;
    this.startQuerySubscription();
  }
 
  componentWillUnmount() {
    this.removeQuerySubscription();
    this.hasMounted = false;
  }
 
  componentDidUpdate() {
    const { onCompleted, onError } = this.props;
    if (onCompleted || onError) {
      const currentResult = this.queryObservable!.currentResult();
      const { loading, error, data } = currentResult;
      if (onCompleted && !loading && !error) {
        onCompleted(data);
      } else if (onError && !loading && error) {
        onError(error);
      }
    }
  }
 
  render(): React.ReactNode {
    const { context } = this;
    const finish = () => this.props.children(this.getQueryResult());
    if (context && context.renderPromises) {
      return context.renderPromises.addQueryPromise(this, finish);
    }
    return finish();
  }
 
  private extractOptsFromProps(props: QueryProps<TData, TVariables>) {
    const {
      variables,
      pollInterval,
      fetchPolicy,
      errorPolicy,
      notifyOnNetworkStatusChange,
      query,
      displayName = 'Query',
      context = {},
    } = props;
 
    this.operation = parser(query);
 
    invariant(
      this.operation.type === DocumentType.Query,
      `The <Query /> component requires a graphql query, but got a ${
        this.operation.type === DocumentType.Mutation ? 'mutation' : 'subscription'
      }.`,
    );
 
    return compact({
      variables,
      pollInterval,
      query,
      fetchPolicy,
      errorPolicy,
      notifyOnNetworkStatusChange,
      metadata: { reactComponent: { displayName } },
      context,
    });
  }
 
  private initializeQueryObservable(props: QueryProps<TData, TVariables>) {
    const opts = this.extractOptsFromProps(props);
    // save for backwards compat of refetcherQueries without a recycler
    this.setOperations(opts);
    this.queryObservable = this.client.watchQuery(opts);
  }
 
  private setOperations(props: QueryProps<TData, TVariables>) {
    if (this.context!.operations) {
      this.context!.operations!.set(this.operation!.name, {
        query: props.query,
        variables: props.variables,
      });
    }
  }
 
  private updateQuery(props: QueryProps<TData, TVariables>) {
    // if we skipped initially, we may not have yet created the observable
    if (!this.queryObservable) {
      this.initializeQueryObservable(props);
    } else {
      this.setOperations(props);
    }
 
    this.queryObservable!.setOptions(this.extractOptsFromProps(props))
      // The error will be passed to the child container, so we don't
      // need to log it here. We could conceivably log something if
      // an option was set. OTOH we don't log errors w/ the original
      // query. See https://github.com/apollostack/react-apollo/issues/404
      .catch(() => null);
  }
 
  private startQuerySubscription = () => {
    if (this.querySubscription) return;
    // store the initial renders worth of result
    let initial: QueryResult<TData, TVariables> | undefined = this.getQueryResult();
    this.querySubscription = this.queryObservable!.subscribe({
      next: ({ data }) => {
        // to prevent a quick second render from the subscriber
        // we compare to see if the original started finished (from cache) and is unchanged
        if (initial && initial.networkStatus === 7 && shallowEqual(initial.data, data)) {
          initial = undefined;
          return;
        }
 
        initial = undefined;
        this.updateCurrentData();
      },
      error: error => {
        this.resubscribeToQuery();
        // Quick fix for https://github.com/apollostack/react-apollo/issues/378
        Iif (!error.hasOwnProperty('graphQLErrors')) throw error;
 
        this.updateCurrentData();
      },
    });
  };
 
  private removeQuerySubscription = () => {
    if (this.querySubscription) {
      this.querySubscription.unsubscribe();
      delete this.querySubscription;
    }
  };
 
  private resubscribeToQuery() {
    this.removeQuerySubscription();
 
    const lastError = this.queryObservable!.getLastError();
    const lastResult = this.queryObservable!.getLastResult();
    // If lastError is set, the observable will immediately
    // send it, causing the stream to terminate on initialization.
    // We clear everything here and restore it afterward to
    // make sure the new subscription sticks.
    this.queryObservable!.resetLastResults();
    this.startQuerySubscription();
    Object.assign(this.queryObservable!, { lastError, lastResult });
  }
 
  private updateCurrentData = () => {
    // force a rerender that goes through shouldComponentUpdate
    Eif (this.hasMounted) this.forceUpdate();
  };
 
  private getQueryResult = (): QueryResult<TData, TVariables> => {
    let data = { data: Object.create(null) as TData } as any;
    // Attach bound methods
    Object.assign(data, observableQueryFields(this.queryObservable!));
 
    // When skipping a query (ie. we're not querying for data but still want
    // to render children), make sure the `data` is cleared out and
    // `loading` is set to `false` (since we aren't loading anything).
    if (this.props.skip) {
      data = {
        ...data,
        data: undefined,
        error: undefined,
        loading: false,
      };
    } else {
      // Fetch the current result (if any) from the store.
      const currentResult = this.queryObservable!.currentResult();
      const { loading, partial, networkStatus, errors } = currentResult;
      let { error } = currentResult;
 
      // Until a set naming convention for networkError and graphQLErrors is
      // decided upon, we map errors (graphQLErrors) to the error props.
      if (errors && errors.length > 0) {
        error = new ApolloError({ graphQLErrors: errors });
      }
 
      Object.assign(data, { loading, networkStatus, error });
 
      if (loading) {
        Object.assign(data.data, this.previousData, currentResult.data);
      } else if (error) {
        Object.assign(data, {
          data: (this.queryObservable!.getLastResult() || {}).data,
        });
      } else {
        const { fetchPolicy } = this.queryObservable!.options;
        const { partialRefetch } = this.props;
        if (
          partialRefetch &&
          Object.keys(currentResult.data).length === 0 &&
          partial &&
          fetchPolicy !== 'cache-only'
        ) {
          // When a `Query` component is mounted, and a mutation is executed
          // that returns the same ID as the mounted `Query`, but has less
          // fields in its result, Apollo Client's `QueryManager` returns the
          // data as an empty Object since a hit can't be found in the cache.
          // This can lead to application errors when the UI elements rendered by
          // the original `Query` component are expecting certain data values to
          // exist, and they're all of a sudden stripped away. To help avoid
          // this we'll attempt to refetch the `Query` data.
          Object.assign(data, { loading: true, networkStatus: NetworkStatus.loading });
          data.refetch();
          return data;
        }
 
        Object.assign(data.data, currentResult.data);
        this.previousData = currentResult.data;
      }
    }
 
    // Handle race condition where refetch is called on child mount or later
    // Normal execution model:
    // render(loading) -> mount -> start subscription -> get data -> render(with data)
    //
    // SSR with synchronous refetch:
    // render(with data) -> refetch -> mount -> start subscription
    //
    // SSR with asynchronous refetch:
    // render(with data) -> mount -> start subscription -> refetch
    //
    // If a subscription has not started, then the synchronous call to refetch
    // must be made at a time when an active network request is being made, so
    // we ensure that the network requests are deduped, to avoid an
    // inconsistent UI state that displays different data for the current query
    // alongside a refetched query.
    //
    // Once the Query component is mounted and the subscription is made, we
    // always hit the network with refetch, since the components data will be
    // updated and a network request is not currently active.
    if (!this.querySubscription) {
      const oldRefetch = (data as GraphqlQueryControls).refetch;
 
      (data as GraphqlQueryControls).refetch = args => {
        if (this.querySubscription) {
          return oldRefetch(args);
        } else {
          return new Promise((r, f) => {
            this.refetcherQueue = { resolve: r, reject: f, args };
          });
        }
      };
    }
 
    data.client = this.client;
    return data;
  };
}