• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript apollo-utilities.isEqual函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中apollo-utilities.isEqual函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isEqual函数的具体用法?TypeScript isEqual怎么用?TypeScript isEqual使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了isEqual函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: isDifferentFromLastResult

 // Compares newResult to the snapshot we took of this.lastResult when it was
 // first received.
 public isDifferentFromLastResult(newResult: ApolloQueryResult<TData>) {
   const { lastResultSnapshot: snapshot } = this;
   return !(
     snapshot &&
     newResult &&
     snapshot.networkStatus === newResult.networkStatus &&
     snapshot.stale === newResult.stale &&
     isEqual(snapshot.data, newResult.data)
   );
 }
开发者ID:apollostack,项目名称:apollo-client,代码行数:12,代码来源:ObservableQuery.ts


示例2: refetch

  public refetch(variables?: any): Promise<ApolloQueryResult<T>> {
    const { fetchPolicy } = this.options;
    // early return if trying to read from cache during refetch
    if (fetchPolicy === 'cache-only') {
      return Promise.reject(
        new Error(
          'cache-only fetchPolicy option should not be used together with query refetch.',
        ),
      );
    }

    if (!isEqual(this.variables, variables)) {
      // update observable variables
      this.variables = {
        ...this.variables,
        ...variables,
      };
    }

    if (!isEqual(this.options.variables, this.variables)) {
      // Update the existing options with new variables
      this.options.variables = {
        ...this.options.variables,
        ...this.variables,
      };
    }

    // Override fetchPolicy for this call only
    // only network-only and no-cache are safe to use
    const isNetworkFetchPolicy =
      fetchPolicy === 'network-only' || fetchPolicy === 'no-cache';

    const combinedOptions: WatchQueryOptions = {
      ...this.options,
      fetchPolicy: isNetworkFetchPolicy ? fetchPolicy : 'network-only',
    };

    return this.queryManager
      .fetchQuery(this.queryId, combinedOptions, FetchType.refetch)
      .then(result => maybeDeepFreeze(result));
  }
开发者ID:NewSpring,项目名称:apollo-client,代码行数:41,代码来源:ObservableQuery.ts


示例3: setVariables

  /**
   * Update the variables of this observable query, and fetch the new results
   * if they've changed. If you want to force new results, use `refetch`.
   *
   * Note: if the variables have not changed, the promise will return the old
   * results immediately, and the `next` callback will *not* fire.
   *
   * Note: if the query is not active (there are no subscribers), the promise
   * will return null immediately.
   *
   * @param variables: The new set of variables. If there are missing variables,
   * the previous values of those variables will be used.
   *
   * @param tryFetch: Try and fetch new results even if the variables haven't
   * changed (we may still just hit the store, but if there's nothing in there
   * this will refetch)
   *
   * @param fetchResults: Option to ignore fetching results when updating variables
   *
   */
  public setVariables(
    variables: any,
    tryFetch: boolean = false,
    fetchResults = true,
  ): Promise<ApolloQueryResult<T>> {
    // since setVariables restarts the subscription, we reset the tornDown status
    this.isTornDown = false;

    const newVariables = {
      ...this.variables,
      ...variables,
    };

    if (isEqual(newVariables, this.variables) && !tryFetch) {
      // If we have no observers, then we don't actually want to make a network
      // request. As soon as someone observes the query, the request will kick
      // off. For now, we just store any changes. (See #1077)
      if (this.observers.length === 0 || !fetchResults) {
        return new Promise(resolve => resolve());
      }
      return this.result();
    } else {
      this.lastVariables = this.variables;
      this.variables = newVariables;
      this.options.variables = newVariables;

      // See comment above
      if (this.observers.length === 0) {
        return new Promise(resolve => resolve());
      }

      // Use the same options as before, but with new variables
      return this.queryManager
        .fetchQuery(this.queryId, {
          ...this.options,
          variables: this.variables,
        } as WatchQueryOptions)
        .then(result => maybeDeepFreeze(result));
    }
  }
开发者ID:dotansimha,项目名称:apollo-client,代码行数:60,代码来源:ObservableQuery.ts


示例4: getQueryDefinition

  /**
   * Given a store and a query, return as much of the result as possible and
   * identify if any data was missing from the store.
   * @param  {DocumentNode} query A parsed GraphQL query document
   * @param  {Store} store The Apollo Client store object
   * @param  {any} previousResult The previous result returned by this function for the same query
   * @return {result: Object, complete: [boolean]}
   */
  public diffQueryAgainstStore<T>({
    store,
    query,
    variables,
    previousResult,
    returnPartialData = true,
    rootId = 'ROOT_QUERY',
    fragmentMatcherFunction,
    config,
  }: DiffQueryAgainstStoreOptions): Cache.DiffResult<T> {
    // Throw the right validation error by trying to find a query in the document
    const queryDefinition = getQueryDefinition(query);

    variables = assign({}, getDefaultValues(queryDefinition), variables);

    const context: ReadStoreContext = {
      // Global settings
      store,
      dataIdFromObject: (config && config.dataIdFromObject) || null,
      cacheRedirects: (config && config.cacheRedirects) || {},
    };

    const execResult = this.executeStoreQuery({
      query,
      rootValue: {
        type: 'id',
        id: rootId,
        generated: true,
        typename: 'Query',
      },
      contextValue: context,
      variableValues: variables,
      fragmentMatcher: fragmentMatcherFunction,
    });

    const hasMissingFields =
      execResult.missing && execResult.missing.length > 0;

    if (hasMissingFields && ! returnPartialData) {
      execResult.missing.forEach(info => {
        if (info.tolerable) return;
        throw new InvariantError(
          `Can't find field ${info.fieldName} on object ${JSON.stringify(
            info.object,
            null,
            2,
          )}.`,
        );
      });
    }

    if (previousResult) {
      if (isEqual(previousResult, execResult.result)) {
        execResult.result = previousResult;
      }
    }

    return {
      result: execResult.result,
      complete: !hasMissingFields,
    };
  }
开发者ID:apollostack,项目名称:apollo-client,代码行数:70,代码来源:readFromStore.ts


示例5: assertIdValue

const readStoreResolver: Resolver = (
  fieldName: string,
  idValue: IdValueWithPreviousResult,
  args: any,
  context: ReadStoreContext,
  { resultKey, directives }: ExecInfo,
) => {
  assertIdValue(idValue);

  const objId = idValue.id;
  const obj = context.store.get(objId);
  const storeKeyName = getStoreKeyName(fieldName, args, directives);
  let fieldValue = (obj || {})[storeKeyName];

  if (typeof fieldValue === 'undefined') {
    if (
      context.cacheResolvers &&
      obj &&
      (obj.__typename || objId === 'ROOT_QUERY')
    ) {
      const typename = obj.__typename || 'Query';

      // Look for the type in the custom resolver map
      const type = context.cacheResolvers[typename];
      if (type) {
        // Look for the field in the custom resolver map
        const resolver = type[fieldName];
        if (resolver) {
          fieldValue = resolver(obj, args);
        }
      }
    }
  }

  if (typeof fieldValue === 'undefined') {
    if (!context.returnPartialData) {
      throw new Error(
        `Can't find field ${storeKeyName} on object (${objId}) ${JSON.stringify(
          obj,
          null,
          2,
        )}.`,
      );
    }

    context.hasMissingField = true;

    return fieldValue;
  }

  // if this is an object scalar, it must be a json blob and we have to unescape it
  if (isJsonValue(fieldValue)) {
    // If the JSON blob is the same now as in the previous result, return the previous result to
    // maintain referential equality.
    //
    // `isEqual` will first perform a referential equality check (with `===`) in case the JSON
    // value has not changed in the store, and then a deep equality check if that fails in case a
    // new JSON object was returned by the API but that object may still be the same.
    if (
      idValue.previousResult &&
      isEqual(idValue.previousResult[resultKey], fieldValue.json)
    ) {
      return idValue.previousResult[resultKey];
    }
    return fieldValue.json;
  }

  // If we had a previous result, try adding that previous result value for this field to our field
  // value. This will create a new value without mutating the old one.
  if (idValue.previousResult) {
    fieldValue = addPreviousResultToIdValues(
      fieldValue,
      idValue.previousResult[resultKey],
    );
  }

  return fieldValue;
};
开发者ID:dotansimha,项目名称:apollo-client,代码行数:78,代码来源:readFromStore.ts


示例6: assertIdValue

const readStoreResolver: Resolver = (
  fieldName: string,
  idValue: IdValueWithPreviousResult,
  args: any,
  context: ReadStoreContext,
  { resultKey, directives }: ExecInfo,
) => {
  assertIdValue(idValue);

  const objId = idValue.id;
  const obj = context.store.get(objId);

  let storeKeyName = fieldName;
  if (args || directives) {
    // We happen to know here that getStoreKeyName returns its first
    // argument unmodified if there are no args or directives, so we can
    // avoid calling the function at all in that case, as a small but
    // important optimization to this frequently executed code.
    storeKeyName = getStoreKeyName(storeKeyName, args, directives);
  }

  let fieldValue: StoreValue | string | void = void 0;

  if (obj) {
    fieldValue = obj[storeKeyName];

    if (
      typeof fieldValue === 'undefined' &&
      context.cacheRedirects &&
      (obj.__typename || objId === 'ROOT_QUERY')
    ) {
      const typename = obj.__typename || 'Query';

      // Look for the type in the custom resolver map
      const type = context.cacheRedirects[typename];
      if (type) {
        // Look for the field in the custom resolver map
        const resolver = type[fieldName];
        if (resolver) {
          fieldValue = resolver(obj, args, {
            getCacheKey(storeObj: StoreObject) {
              return toIdValue({
                id: context.dataIdFromObject(storeObj),
                typename: storeObj.__typename,
              });
            },
          });
        }
      }
    }
  }

  if (typeof fieldValue === 'undefined') {
    if (!context.returnPartialData) {
      throw new Error(
        `Can't find field ${storeKeyName} on object (${objId}) ${JSON.stringify(
          obj,
          null,
          2,
        )}.`,
      );
    }

    context.hasMissingField = true;

    return fieldValue;
  }

  // if this is an object scalar, it must be a json blob and we have to unescape it
  if (isJsonValue(fieldValue)) {
    // If the JSON blob is the same now as in the previous result, return the previous result to
    // maintain referential equality.
    //
    // `isEqual` will first perform a referential equality check (with `===`) in case the JSON
    // value has not changed in the store, and then a deep equality check if that fails in case a
    // new JSON object was returned by the API but that object may still be the same.
    if (
      idValue.previousResult &&
      isEqual(idValue.previousResult[resultKey], fieldValue.json)
    ) {
      return idValue.previousResult[resultKey];
    }
    return fieldValue.json;
  }

  // If we had a previous result, try adding that previous result value for this field to our field
  // value. This will create a new value without mutating the old one.
  if (idValue.previousResult) {
    fieldValue = addPreviousResultToIdValues(
      fieldValue,
      idValue.previousResult[resultKey],
    );
  }

  return fieldValue;
};
开发者ID:NewSpring,项目名称:apollo-client,代码行数:96,代码来源:readFromStore.ts



注:本文中的apollo-utilities.isEqual函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript apollo-utilities.isField函数代码示例发布时间:2022-05-25
下一篇:
TypeScript apollo-utilities.getMainDefinition函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap