[๐Ÿš€Apollo] Cache - Reading and writing

Jake_Youngยท2021๋…„ 9์›” 17์ผ
0

apollo ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ฒˆ์—ญํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Using GraphQL queries

  • readQuery
  • ์ง์ ‘ ๋ฐ˜ํ™˜๋œ object๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
  • writeQuery๋ฅผ ์จ๋ผ.
  • ๋งŒ์•ฝ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ local์— ์—†๋‹ค๋ฉด null์„ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  server๋กœ ๋”ฐ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋‹ค.
const READ_TODO = gql`
  query ReadTodo($id: ID!) {
    todo(id: $id) {
      id
      text
      completed
    }
  }
`;

// Fetch the cached to-do item with ID 5
const { todo } = client.readQuery({
  query: READ_TODO,
  variables: { // Provide any required variables here
    id: 5,
  },
});
  • writeQuery
  • readQuery์™€ ๊ฑฐ์˜ ํก์‚ฌํ•˜์ง€๋งŒ, data option์ด ์žˆ๋‹ค๋Š” ๊ฒŒ ๋‹ค๋ฅด๋‹ค.
client.writeQuery({
  query: gql`
    query WriteTodo($id: Int!) {
      todo(id: $id) {
        id
        text
        completed
      }
    }`,
  data: { // Contains the data to write
    todo: {
      __typename: 'Todo',
      id: 5,
      text: 'Buy grapes ๐Ÿ‡',
      completed: false
    },
  },
  variables: {
    id: 5
  }
});

Using GraphQL fragments

  • complete valid query๋ฅผ ์š”๊ตฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
  • graphQL queris์™€ ๋‹ค๋ฅด๊ฒŒ id option์„ ์š”๊ตฌํ•œ๋‹ค.
  • ์ด๋•Œ์˜ id๋Š” cache id ์ด๋‹ค.
  • readFragment
Copy
const todo = client.readFragment({
  id: 'Todo:5', // The value of the to-do item's cache ID
  fragment: gql`
    fragment MyTodo on Todo {
      id
      text
      completed
    }
  `,
});
  • writeFragment
  • ํ•ด๋‹น id์˜ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋งŒ๋„ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • subscribe ํ•˜๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด rerendering ๋œ๋‹ค.
client.writeFragment({
  id: 'Todo:5',
  fragment: gql`
    fragment MyTodo on Todo {
      completed
    }
  `,
  data: {
    completed: true,
  },
});

Using cache.modify

  • writeQuery ๋‚˜ writeFragment์ฒ˜๋Ÿผ modify๋„ subscription๋“ค์„ trigger ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋„ค๊ฐ€ broadcast:false๋ฅผ ์•ˆ ๋ถ™์ธ๋‹ค๋ฉด..
  • ์•ž์˜ ๋‘ ์ฟผ๋ฆฌ์™€ ๋‹ค๋ฅด๊ฒŒ ์ด๋ฏธ ์กฐ๋‚ดํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ ์“ธ ์ˆ˜๋Š” ์—†๋‹ค.
  • ID๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด cache.identify๋ฅผ ์“ธ ๊ฒƒ์„ ์ถ”์ฒœ.
cache.modify({
  id: cache.identify(myObject),
  fields: {
   	/*name ํ•„๋“œ์˜ ๊ฐ’์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ*/ 
    name(cachedName) {
      return cachedName.toUpperCase();
    },
  },
  /* broadcast: false // Include this to prevent automatic query refresh */
});
// readField๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
const idToRemove = 'abc123';

cache.modify({
  id: cache.identify(myPost),
  fields: {
    comments(existingCommentRefs, { readField }) {
      return existingCommentRefs.filter(
        commentRef => idToRemove !== readField('id', commentRef)
      );
    },
  },
});
const newComment = {
  __typename: 'Comment',
  id: 'abc123',
  text: 'Great blog post!',
};

cache.modify({
  id: cache.identify(myPost),
  fields: {
    comments(existingCommentRefs = [], { readField }) {
      const newCommentRef = cache.writeFragment({
        data: newComment,
        fragment: gql`
          fragment NewComment on Comment {
            id
            text
          }
        `
      });

      // Quick safety check - if the new comment is already
      // present in the cache, we don't need to add it again.
      if (existingCommentRefs.some(
        ref => readField('id', ref) === newComment.id
      )) {
        return existingCommentRefs;
      }

      return [...existingCommentRefs, newCommentRef];
    }
  }
});
// useMutation ํ•  ๋•Œ cache ์–ด๋–ป๊ฒŒ ์—…๋ฐ์ดํŠธ ํ•˜๋Š”์ง€!!
const [addComment] = useMutation(ADD_COMMENT, {
  update(cache, { data: { addComment } }) {
    cache.modify({
      id: cache.identify(myPost),
      fields: {
        comments(existingCommentRefs = [], { readField }) {
          const newCommentRef = cache.writeFragment({
            data: addComment,
            fragment: gql`
              fragment NewComment on Comment {
                id
                text
              }
            `
          });
          return [...existingCommentRefs, newCommentRef];
        }
      }
    });
  }
});
cache.modify({
  id: cache.identify(myPost),
  fields: {
    comments(existingCommentRefs, { DELETE }) {
      return DELETE;
    },
  },
});
cache.modify({
  id: cache.identify(myPost),
  fields: {
    comments(existingCommentRefs, { INVALIDATE }) {
      return INVALIDATE;
    },
  },
});
profile
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ํŒŒ์ด์ฌ ๊ทธ๋ฆฌ๊ณ  ์ปดํ“จํ„ฐ์™€ ๋„คํŠธ์›Œํฌ

0๊ฐœ์˜ ๋Œ“๊ธ€