当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript ts-invariant.invariant函数代码示例

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


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

示例1: checkDocument

export function checkDocument(doc: DocumentNode) {
  invariant(
    doc && doc.kind === 'Document',
    `Expecting a parsed GraphQL document. Perhaps you need to wrap the query \
string in a "gql" tag? http://docs.apollostack.com/apollo-client/core.html#gql`,
  );

  const operations = doc.definitions
    .filter(d => d.kind !== 'FragmentDefinition')
    .map(definition => {
      if (definition.kind !== 'OperationDefinition') {
        throw new InvariantError(
          `Schema type definitions not allowed in queries. Found: "${
            definition.kind
          }"`,
        );
      }
      return definition;
    });

  invariant(
    operations.length <= 1,
    `Ambiguous GraphQL document: contains ${operations.length} operations`,
  );

  return doc;
}
开发者ID:apollostack,项目名称:apollo-client,代码行数:27,代码来源:getFromAST.ts

示例2: invariant

  return directives ? directives.filter(isInclusionDirective).map(directive => {
    const directiveArguments = directive.arguments;
    const directiveName = directive.name.value;

    invariant(
      directiveArguments && directiveArguments.length === 1,
      `Incorrect number of arguments for the @${directiveName} directive.`,
    );

    const ifArgument = directiveArguments[0];
    invariant(
      ifArgument.name && ifArgument.name.value === 'if',
      `Invalid argument for the @${directiveName} directive.`,
    );

    const ifValue: ValueNode = ifArgument.value;

    // means it has to be a variable value if this is a valid @skip or @include directive
    invariant(
      ifValue &&
        (ifValue.kind === 'Variable' || ifValue.kind === 'BooleanValue'),
      `Argument for the @${directiveName} directive must be a variable or a boolean value.`,
    );

    return { directive, ifArgument };
  }) : [];
开发者ID:apollostack,项目名称:apollo-client,代码行数:26,代码来源:directives.ts

示例3: match

  public match(
    idValue: IdValue,
    typeCondition: string,
    context: ReadStoreContext,
  ) {
    invariant(
      this.isReady,
      'FragmentMatcher.match() was called before FragmentMatcher.init()',
    );

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

    if (!obj) {
      // https://github.com/apollographql/apollo-client/pull/4620
      return idValue.id === 'ROOT_QUERY';
    }

    invariant(
      obj.__typename,
      `Cannot match fragment because __typename property is missing: ${JSON.stringify(
        obj,
      )}`,
    );

    if (obj.__typename === typeCondition) {
      return true;
    }

    const implementingTypes = this.possibleTypesMap[typeCondition];
    if (implementingTypes && implementingTypes.indexOf(obj.__typename) > -1) {
      return true;
    }

    return false;
  }
开发者ID:apollostack,项目名称:apollo-client,代码行数:35,代码来源:fragmentMatcher.ts

示例4: getOperationDefinitionOrDie

export function getOperationDefinitionOrDie(
  document: DocumentNode,
): OperationDefinitionNode {
  const def = getOperationDefinition(document);
  invariant(def, `GraphQL document is missing an operation`);
  return def;
}
开发者ID:apollostack,项目名称:apollo-client,代码行数:7,代码来源:getFromAST.ts

示例5: invariant

  public fetchMore<K extends keyof TVariables>(
    fetchMoreOptions: FetchMoreQueryOptions<TVariables, K> &
      FetchMoreOptions<TData, TVariables>,
  ): Promise<ApolloQueryResult<TData>> {
    // early return if no update Query
    invariant(
      fetchMoreOptions.updateQuery,
      'updateQuery option is required. This function defines how to update the query data with the new results.',
    );

    let combinedOptions: any;
    const qid = this.queryManager.generateQueryId();

    return Promise.resolve()
      .then(() => {
        if (fetchMoreOptions.query) {
          // fetch a new query
          combinedOptions = fetchMoreOptions;
        } else {
          // fetch the same query with a possibly new variables
          combinedOptions = {
            ...this.options,
            ...fetchMoreOptions,
            variables: Object.assign(
              {},
              this.variables,
              fetchMoreOptions.variables,
            ),
          };
        }

        combinedOptions.fetchPolicy = 'network-only';

        return this.queryManager.fetchQuery(
          qid,
          combinedOptions as WatchQueryOptions,
          FetchType.normal,
          this.queryId,
        );
      })
      .then(
        fetchMoreResult => {
          this.updateQuery((previousResult: any) =>
            fetchMoreOptions.updateQuery(previousResult, {
              fetchMoreResult: fetchMoreResult.data as TData,
              variables: combinedOptions.variables,
            }),
          );

          this.queryManager.stopQuery(qid);

          return fetchMoreResult as ApolloQueryResult<TData>;
        },
        error => {
          this.queryManager.stopQuery(qid);
          throw error;
        },
      );
  }
开发者ID:apollostack,项目名称:apollo-client,代码行数:59,代码来源:ObservableQuery.ts

示例6: getQueryDefinition

export function getQueryDefinition(doc: DocumentNode): OperationDefinitionNode {
  const queryDef = getOperationDefinition(doc) as OperationDefinitionNode;

  invariant(
    queryDef && queryDef.operation === 'query',
    'Must contain a query definition.',
  );

  return queryDef;
}
开发者ID:apollostack,项目名称:apollo-client,代码行数:10,代码来源:getFromAST.ts

示例7: return

 getCacheKey: (obj: { __typename: string; id: string | number }) => {
   if ((cache as any).config) {
     return (cache as any).config.dataIdFromObject(obj);
   } else {
     invariant(false,
       'To use context.getCacheKey, you need to use a cache that has ' +
         'a configurable dataIdFromObject, like apollo-cache-inmemory.',
     );
   }
 },
开发者ID:apollostack,项目名称:apollo-client,代码行数:10,代码来源:LocalState.ts

示例8: getFragmentDefinition

export function getFragmentDefinition(
  doc: DocumentNode,
): FragmentDefinitionNode {
  invariant(
    doc.kind === 'Document',
    `Expecting a parsed GraphQL document. Perhaps you need to wrap the query \
string in a "gql" tag? http://docs.apollostack.com/apollo-client/core.html#gql`,
  );

  invariant(
    doc.definitions.length <= 1,
    'Fragment must have exactly one definition.',
  );

  const fragmentDef = doc.definitions[0] as FragmentDefinitionNode;

  invariant(
    fragmentDef.kind === 'FragmentDefinition',
    'Must be a fragment definition.',
  );

  return fragmentDef as FragmentDefinitionNode;
}
开发者ID:apollostack,项目名称:apollo-client,代码行数:23,代码来源:getFromAST.ts


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