本文整理汇总了TypeScript中apollo-cache-inmemory.InMemoryCache.readQuery方法的典型用法代码示例。如果您正苦于以下问题:TypeScript InMemoryCache.readQuery方法的具体用法?TypeScript InMemoryCache.readQuery怎么用?TypeScript InMemoryCache.readQuery使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apollo-cache-inmemory.InMemoryCache
的用法示例。
在下文中一共展示了InMemoryCache.readQuery方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('returns the Query result after resetStore', async done => {
const stateLink = withClientState({
cache,
resolvers: {
Query: {
counter: () => 0,
},
Mutation: {
plus: (_, __, { cache }) => {
const { counter } = cache.readQuery({ query: counterQuery });
const data = {
counter: counter + 1,
};
cache.writeData({ data });
return null;
},
},
},
defaults: {
counter: 10,
},
});
const client = createClient(stateLink);
await client.mutate({ mutation: plusMutation });
expect(cache.readQuery({ query: counterQuery })).toMatchObject({
counter: 11,
});
await client.mutate({ mutation: plusMutation });
expect(cache.readQuery({ query: counterQuery })).toMatchObject({
counter: 12,
});
await expect(
client.query({ query: counterQuery }),
).resolves.toMatchObject({
data: { counter: 12 },
});
(client.resetStore() as Promise<null>)
.then(() => {
expect(client.query({ query: counterQuery }))
.resolves.toMatchObject({ data: { counter: 0 } })
.then(done)
.catch(done.fail);
})
.catch(done.fail);
});
示例2: expect
.then(() => {
expect(cache.readQuery({ query: counterQuery })).toMatchObject({
counter: 12,
});
expect(client.query({ query: counterQuery })).resolves.toMatchObject({
data: { counter: 12 },
});
})
示例3: return
counter: () => {
try {
return (cache.readQuery({ query: counterQuery }) as any)
.counter;
} catch (error) {
try {
expect(error.message).toMatch(/field counter/);
} catch (e) {
done.fail(e);
}
unsub.unsubscribe();
done();
}
return -1; // to remove warning from in-memory-cache
},
示例4: withClientState
it.skip('returns the default data from cache in a Query resolver with writeDefaults callback enabled', done => {
const stateLink = withClientState({
cache,
resolvers: {
Query: {
counter: () => {
//This cache read does not see any data
return (cache.readQuery({ query: counterQuery }) as any).counter;
},
},
Mutation: {
plus: (_, __, { cache }) => {
const { counter } = cache.readQuery({ query: counterQuery });
const data = {
counter: counter + 1,
};
cache.writeData({ data });
return null;
},
},
},
defaults: {
counter: 10,
},
});
const client = createClient(stateLink);
client.onResetStore(stateLink.writeDefaults);
client.mutate({ mutation: plusMutation });
client.mutate({ mutation: plusMutation });
expect(cache.readQuery({ query: counterQuery })).toMatchObject({
counter: 12,
});
expect(client.query({ query: counterQuery })).resolves.toMatchObject({
data: { counter: 12 },
});
let called = false;
const componentObservable = client.watchQuery({ query: counterQuery });
const unsub = componentObservable.subscribe({
next: ({ data }) => {
try {
//this fails
expect(data).toMatchObject({ counter: 10 });
called = true;
} catch (e) {
done.fail(e);
}
},
error: done.fail,
complete: done.fail,
});
(client.resetStore() as Promise<null>)
.then(() => {
expect(client.query({ query: counterQuery }))
.resolves.toMatchObject({ data: { counter: 10 } })
.then(
makeTerminatingCheck(
() => {
unsub.unsubscribe();
done();
},
() => {
expect(called);
},
),
)
.catch(done.fail);
})
.catch(done.fail);
});