本文整理汇总了TypeScript中apollo-link.ApolloLink.from方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ApolloLink.from方法的具体用法?TypeScript ApolloLink.from怎么用?TypeScript ApolloLink.from使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apollo-link.ApolloLink
的用法示例。
在下文中一共展示了ApolloLink.from方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('passes forward on', done => {
const link = ApolloLink.from([
new BatchLink({
batchInterval: 0,
batchMax: 1,
batchHandler: (operation, forward) => {
try {
expect(forward.length).toBe(1);
expect(operation.length).toBe(1);
} catch (e) {
done.fail(e);
}
return forward[0](operation[0]).map(result => [result]);
},
}),
new ApolloLink(operation => {
terminatingCheck(done, () => {
expect(operation.query).toEqual(query);
})();
return null;
}),
]);
execute(
link,
createOperation(
{},
{
query,
},
),
).subscribe(result => done.fail());
});
示例2: it
it(`does not affect different queries`, () => {
const document: DocumentNode = gql`
query test1($x: String) {
test(x: $x)
}
`;
const variables1 = { x: 'Hello World' };
const variables2 = { x: 'Goodbye World' };
const request1: GraphQLRequest = {
query: document,
variables: variables1,
operationName: getOperationName(document),
};
const request2: GraphQLRequest = {
query: document,
variables: variables2,
operationName: getOperationName(document),
};
let called = 0;
const deduper = ApolloLink.from([
new DedupLink(),
new ApolloLink(() => {
called += 1;
return null;
}),
]);
execute(deduper, request1);
execute(deduper, request2);
expect(called).toBe(2);
});
示例3: it
it('does a network request if fetchPolicy is cache-only then store is reset then fetchPolicy becomes not cache-only', done => {
let queryManager: QueryManager;
let observable: ObservableQuery<any>;
const testQuery = gql`
query {
author {
firstName
lastName
}
}
`;
const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};
let timesFired = 0;
const link: ApolloLink = ApolloLink.from([
() => {
return new Observable(observer => {
timesFired += 1;
observer.next({ data });
observer.complete();
return;
});
},
]);
queryManager = createQueryManager({ link });
observable = queryManager.watchQuery({ query: testQuery });
subscribeAndCount(done, observable, (handleCount, result) => {
try {
if (handleCount === 1) {
expect(result.data).toEqual(data);
expect(timesFired).toBe(1);
setTimeout(() => {
observable.setOptions({ fetchPolicy: 'cache-only' });
queryManager.resetStore();
}, 0);
} else if (handleCount === 2) {
expect(result.data).toEqual({});
expect(timesFired).toBe(1);
setTimeout(() => {
observable.setOptions({ fetchPolicy: 'cache-first' });
}, 0);
} else if (handleCount === 3) {
expect(result.data).toEqual(data);
expect(timesFired).toBe(2);
done();
}
} catch (e) {
done.fail(e);
}
});
});
示例4: createApolloClient
export default function createApolloClient() {
return new ApolloClient({
link: ApolloLink.from([restLink, middlewareLink, httpLink]),
cache: new InMemoryCache(),
connectToDevTools: devMode
});
}
示例5: constructor
constructor(
private apollo: Apollo,
private tokenService: TokenService,
private modalService: ModalService
) {
const uploadLink = createUploadLink({ uri: uri });
const authLink = createAuthLink(tokenService);
const errorLink = createErrorLink(modalService);
apollo.create({
link: ApolloLink.from([authLink, errorLink, uploadLink]),
cache: new InMemoryCache(),
queryDeduplication: true,
defaultOptions: {
mutate: {
errorPolicy: 'all'
},
query: {
errorPolicy: 'all'
},
watchQuery: {
errorPolicy: 'all'
}
}
});
}
示例6: it
it('calls unsubscribe on the appropriate downstream observable', async () => {
const retry = new RetryLink({
delay: { initial: 1 },
attempts: { max: 2 },
});
const data = { data: { hello: 'world' } };
const unsubscribeStub = jest.fn();
const firstTry = fromError(standardError);
// Hold the test hostage until we're hit
let secondTry;
const untilSecondTry = new Promise(resolve => {
secondTry = {
subscribe(observer) {
resolve(); // Release hold on test.
Promise.resolve().then(() => {
observer.next(data);
observer.complete();
});
return { unsubscribe: unsubscribeStub };
},
};
});
const stub = jest.fn();
stub.mockReturnValueOnce(firstTry);
stub.mockReturnValueOnce(secondTry);
const link = ApolloLink.from([retry, stub]);
const subscription = execute(link, { query }).subscribe({});
await untilSecondTry;
subscription.unsubscribe();
expect(unsubscribeStub).toHaveBeenCalledTimes(1);
});
示例7: it
it('includes the cache on the context for eviction links', done => {
const query = gql`
query CachedLuke {
people_one(id: 1) {
name
friends {
name
}
}
}
`;
const initialData = {
people_one: {
name: 'Luke Skywalker',
friends: [{ name: 'Leia Skywalker' }],
},
};
const evictionLink = (operation, forward) => {
const { cache } = operation.getContext();
expect(cache).toBeDefined();
return forward(operation).map(result => {
setTimeout(() => {
const cacheResult = cache.read({ query });
expect(cacheResult).toEqual(initialData);
expect(cacheResult).toEqual(result.data);
if (count === 1) {
done();
}
}, 10);
return result;
});
};
const mockLink = new MockSubscriptionLink();
const link = ApolloLink.from([evictionLink, mockLink]);
const queryManager = new QueryManager({
store: new DataStore(new InMemoryCache({ addTypename: false })),
link,
});
const observable = queryManager.watchQuery<any>({
query,
variables: {},
});
let count = 0;
observable.subscribe({
next: result => {
count++;
},
error: e => {
console.error(e);
},
});
// fire off first result
mockLink.simulateResult({ result: { data: initialData } });
});