本文整理汇总了TypeScript中apollo-link.ApolloLink.split方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ApolloLink.split方法的具体用法?TypeScript ApolloLink.split怎么用?TypeScript ApolloLink.split使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apollo-link.ApolloLink
的用法示例。
在下文中一共展示了ApolloLink.split方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('prints unhandled subscription errors to the console', done => {
let latestResult: any = null;
const wSLink = mockObservableLink(sub3);
const httpLink = mockSingleLink(req1);
const link = ApolloLink.split(isSub, wSLink, httpLink);
let counter = 0;
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
});
const obsHandle = client.watchQuery({
query,
});
const sub = obsHandle.subscribe({
next(queryResult) {
latestResult = queryResult;
counter++;
},
});
let errorCount = 0;
const consoleErr = console.error;
console.error = (_: Error) => {
errorCount += 1;
};
obsHandle.subscribeToMore({
document: gql`
subscription newValues {
name
}
`,
updateQuery: () => {
throw new Error('should not be called because of initial error');
},
});
setTimeout(() => {
sub.unsubscribe();
expect(latestResult).toEqual({
data: { entry: { value: 1 } },
loading: false,
networkStatus: 7,
stale: false,
});
expect(counter).toBe(1);
expect(errorCount).toBe(1);
console.error = consoleErr;
done();
}, 15);
for (let i = 0; i < 2; i++) {
wSLink.simulateResult(results3[i]);
}
});
示例2: getMainDefinition
const requestLink = (queryOrMutationLink: ApolloLink) => ApolloLink.split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
subscriptionLink,
queryOrMutationLink,
);
示例3: getMainDefinition
const requestLink = (queryOrMutationLink: ApolloLink) => {
return ApolloLink.split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
queryOrMutationLink['options'].headers = new HttpHeaders({
Authorization: localStorage.getItem('token') ? `Bearer ${localStorage.getItem('token')}` : ''
});
return kind === 'OperationDefinition' && operation === 'subscription';
},
subscriptionLink,
queryOrMutationLink,
);
}
示例4: it
it('triggers new result from subscription data', done => {
let latestResult: any = null;
const wSLink = mockObservableLink();
const httpLink = mockSingleLink(req1);
const link = ApolloLink.split(isSub, wSLink, httpLink);
let counter = 0;
const client = new ApolloClient({
cache: new InMemoryCache({ addTypename: false }),
link,
});
const obsHandle = client.watchQuery<typeof req1['result']['data']>({
query,
});
const sub = obsHandle.subscribe({
next(queryResult) {
latestResult = queryResult;
counter++;
},
});
obsHandle.subscribeToMore<SubscriptionData>({
document: gql`
subscription newValues {
name
}
`,
updateQuery: (_, { subscriptionData }) => {
return { entry: { value: subscriptionData.data.name } };
},
});
setTimeout(() => {
sub.unsubscribe();
expect(counter).toBe(3);
expect(stripSymbols(latestResult)).toEqual({
data: { entry: { value: 'Amanda Liu' } },
loading: false,
networkStatus: 7,
stale: false,
});
done();
}, 15);
for (let i = 0; i < 2; i++) {
wSLink.simulateResult(results[i]);
}
});
示例5: beforeAll
beforeAll(async () => {
const fetch = createApolloFetch({ uri: `http://localhost:${process.env['PORT']}/graphql` });
const cache = new InMemoryCache();
let link = ApolloLink.split(
operation => {
const operationAST = getOperationAST(operation.query, operation.operationName);
return !!operationAST && operationAST.operation === 'subscription';
},
new WebSocketLink({
uri: `ws://localhost:${process.env['PORT']}/graphql`,
webSocketImpl: WebSocket,
}) as any,
new BatchHttpLink({ uri: `http://localhost:${process.env['PORT']}/graphql` }),
);
apollo = new ApolloClient({
link: ApolloLink.from((true ? [new LoggingLink()] : []).concat([link])),
cache,
});
});
示例6: WebSocketLink
reconnect: true,
// connectionParams,
},
);
wsClient.use([
]);
wsClient.onDisconnected(() => { });
wsClient.onReconnected(() => { });
link = ApolloLink.split(
operation => {
const operationAST = getOperationAST(operation.query, operation.operationName);
return !!operationAST && operationAST.operation === 'subscription';
},
new WebSocketLink(wsClient) as any,
new BatchHttpLink({ uri: PUBLIC_SETTINGS.GRAPHQL_URL }),
);
} else {
link = new BatchHttpLink({ uri: PUBLIC_SETTINGS.LOCAL_GRAPHQL_URL });
}
// TODO Setup PersistQueries
// if (__PERSIST_GQL__) {
// import('@sample-stack/graphql-gql/extracted_queries.json').then(queryMap => {
// console.log(queryMap)
// }).catch(() => {
// console.warn('extracted_queries not found');
// });
// }