本文整理汇总了TypeScript中apollo-cache-inmemory.InMemoryCache.writeData方法的典型用法代码示例。如果您正苦于以下问题:TypeScript InMemoryCache.writeData方法的具体用法?TypeScript InMemoryCache.writeData怎么用?TypeScript InMemoryCache.writeData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apollo-cache-inmemory.InMemoryCache
的用法示例。
在下文中一共展示了InMemoryCache.writeData方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should handle a simple query with both server and client fields', done => {
const query = gql`
query GetCount {
count @client
lastCount
}
`;
const cache = new InMemoryCache();
const link = new ApolloLink(operation => {
expect(operation.operationName).toBe('GetCount');
return Observable.of({ data: { lastCount: 1 } });
});
const client = new ApolloClient({
cache,
link,
resolvers: {},
});
cache.writeData({
data: {
count: 0,
},
});
client.watchQuery({ query }).subscribe({
next: ({ data }) => {
expect({ ...data }).toMatchObject({ count: 0, lastCount: 1 });
done();
},
});
});
示例2: InMemoryCache
() => {
const query = gql`
{
fie: foo @client {
bar
}
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
});
cache.writeData({
data: {
foo: {
bar: 'yo',
__typename: 'Foo',
},
},
});
return client.query({ query }).then(({ data }) => {
expect({ ...data }).toMatchObject({
fie: { bar: 'yo', __typename: 'Foo' },
});
});
},
示例3: currentAuthorPostCount
done => {
const query = gql`
query currentAuthorPostCount($authorId: Int!) {
appContainer @client {
systemDetails {
currentAuthor {
name
authorId @export(as: "authorId")
}
}
}
postCount(authorId: $authorId)
}
`;
const appContainer = {
systemDetails: {
currentAuthor: {
name: 'John Smith',
authorId: 100,
__typename: 'Author',
},
__typename: 'SystemDetails',
},
__typename: 'AppContainer',
};
const testPostCount = 200;
const link = new ApolloLink(() =>
Observable.of({
data: {
postCount: testPostCount,
},
}),
);
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link,
resolvers: {},
});
cache.writeData({
data: {
appContainer,
},
});
return client.query({ query }).then(({ data }: any) => {
expect({ ...data }).toMatchObject({
appContainer,
postCount: testPostCount,
});
done();
});
},
示例4: reviewerPost
done => {
const query = gql`
query reviewerPost($reviewerId: Int!) {
primaryReviewerId @client @export(as: "reviewerId")
secondaryReviewerId @client @export(as: "reviewerId")
post(reviewerId: $reviewerId) {
title
}
}
`;
const post = {
title: 'The One Post to Rule Them All',
__typename: 'Post',
};
const primaryReviewerId = 100;
const secondaryReviewerId = 200;
const link = new ApolloLink(({ variables }) => {
expect(variables).toMatchObject({ reviewerId: secondaryReviewerId });
return Observable.of({
data: {
post,
},
});
});
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link,
resolvers: {},
});
cache.writeData({
data: {
primaryReviewerId,
secondaryReviewerId,
},
});
return client.query({ query }).then(({ data }: any) => {
expect({ ...data }).toMatchObject({
post,
});
done();
});
},
示例5: it
it('should allow @client @export variables to be used with remote queries', done => {
const query = gql`
query currentAuthorPostCount($authorId: Int!) {
currentAuthor @client {
name
authorId @export(as: "authorId")
}
postCount(authorId: $authorId)
}
`;
const testAuthor = {
name: 'John Smith',
authorId: 100,
__typename: 'Author',
};
const testPostCount = 200;
const link = new ApolloLink(() =>
Observable.of({
data: {
postCount: testPostCount,
},
}),
);
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link,
resolvers: {},
});
cache.writeData({
data: {
currentAuthor: testAuthor,
},
});
return client.query({ query }).then(({ data }: any) => {
expect({ ...data }).toMatchObject({
currentAuthor: testAuthor,
postCount: testPostCount,
});
done();
});
});
示例6: upvotePost
done => {
const mutation = gql`
mutation upvotePost($postId: Int!) {
topPost @client @export(as: "postId")
upvotePost(postId: $postId) {
title
votes
}
}
`;
const testPostId = 100;
const testPost = {
title: 'The Day of the Jackal',
votes: 10,
__typename: 'post',
};
const link = new ApolloLink(({ variables }) => {
expect(variables).toMatchObject({ postId: testPostId });
return Observable.of({
data: {
upvotePost: testPost,
},
});
});
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link,
resolvers: {},
});
cache.writeData({
data: {
topPost: testPostId,
},
});
return client.mutate({ mutation }).then(({ data }: any) => {
expect({ ...data }).toMatchObject({
upvotePost: testPost,
});
done();
});
},
示例7: async
async () => {
const query = gql`
query Author {
author {
name
isLoggedIn @client(always: true)
}
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
});
cache.writeData({
data: {
author: {
name: 'John Smith',
isLoggedIn: false,
__typename: 'Author',
},
},
});
// When the resolver isn't defined, there isn't anything to force, so
// make sure the query resolves from the cache properly.
const { data: data1 } = await client.query({ query });
expect(data1.author.isLoggedIn).toEqual(false);
client.addResolvers({
Author: {
isLoggedIn() {
return true;
},
},
});
// A resolver is defined, so make sure it's forced, and the result
// resolves properly as a combination of cache and local resolver
// data.
const { data: data2 } = await client.query({ query });
expect(data2.author.isLoggedIn).toEqual(true);
},
示例8: InMemoryCache
done => {
const query = gql`
{
car @client {
engine {
torque @export(as: "torque")
}
}
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
resolvers: {},
});
cache.writeData({
data: {
car: {
engine: {
cylinders: 8,
torque: 7200,
__typename: 'Engine',
},
__typename: 'Car',
},
},
});
return client.query({ query }).then(({ data }: any) => {
expect({ ...data }).toMatchObject({
car: {
engine: {
torque: 7200,
},
},
});
done();
});
},