當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。