當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。