本文整理汇总了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;
}
示例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 };
}) : [];
示例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;
}
示例4: getOperationDefinitionOrDie
export function getOperationDefinitionOrDie(
document: DocumentNode,
): OperationDefinitionNode {
const def = getOperationDefinition(document);
invariant(def, `GraphQL document is missing an operation`);
return def;
}
示例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;
},
);
}
示例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;
}
示例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.',
);
}
},
示例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;
}