当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript apollo-cache-inmemory.InMemoryCache类代码示例

本文整理汇总了TypeScript中apollo-cache-inmemory.InMemoryCache的典型用法代码示例。如果您正苦于以下问题:TypeScript InMemoryCache类的具体用法?TypeScript InMemoryCache怎么用?TypeScript InMemoryCache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了InMemoryCache类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: 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' },
        });
      });
    },
开发者ID:apollostack,项目名称:apollo-client,代码行数:30,代码来源:resolvers.ts

示例2: 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();
      },
    });
  });
开发者ID:apollostack,项目名称:apollo-client,代码行数:33,代码来源:general.ts

示例3: it

  it('should handle resolvers that work with booleans properly', done => {
    const query = gql`
      query CartDetails {
        isInCart @client
      }
    `;

    const cache = new InMemoryCache();
    cache.writeQuery({ query, data: { isInCart: true } });

    const client = new ApolloClient({
      cache,
      resolvers: {
        Query: {
          isInCart: () => false,
        },
      },
    });

    return client
      .query({ query, fetchPolicy: 'network-only' })
      .then(({ data }: any) => {
        expect({ ...data }).toMatchObject({
          isInCart: false,
        });
        done();
      });
  });
开发者ID:apollostack,项目名称:apollo-client,代码行数:28,代码来源:resolvers.ts

示例4: 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();
      });
    },
开发者ID:apollostack,项目名称:apollo-client,代码行数:58,代码来源:export.ts

示例5: 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();
      });
    },
开发者ID:apollostack,项目名称:apollo-client,代码行数:48,代码来源:export.ts

示例6: 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();
    });
  });
开发者ID:apollostack,项目名称:apollo-client,代码行数:48,代码来源:export.ts

示例7: 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();
      });
    },
开发者ID:apollostack,项目名称:apollo-client,代码行数:47,代码来源:export.ts

示例8: expect

    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);
    });
开发者ID:petermichuncc,项目名称:scanner,代码行数:48,代码来源:client.ts


注:本文中的apollo-cache-inmemory.InMemoryCache类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。