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


TypeScript InMemoryCache.readQuery方法代码示例

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

示例2: expect

 .then(() => {
   expect(cache.readQuery({ query: counterQuery })).toMatchObject({
     counter: 12,
   });
   expect(client.query({ query: counterQuery })).resolves.toMatchObject({
     data: { counter: 12 },
   });
 })
开发者ID:petermichuncc,项目名称:scanner,代码行数:8,代码来源:client.ts

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

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


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