relay cache - ConnectionHandler createEdge vs buildConnectionEdge

Written by Paul

relay cache - ConnectionHandler createEdge vs buildConnectionEdge

createEdge

์„œ๋ฒ„์—์„œ ์ค€ Payload๊ฐ€ node ์ธ ๊ฒฝ์šฐ์— ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ํŠน์ • ์ปค๋„ฅ์…˜์— ์—ฃ์ง€๋ฅผ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.
notion image
์„ธ ๋ฒˆ์งธ ์ธ์ž์ธ node ์ž„์„ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
type CreateTodoPayload { todo: TodoNode! } type Mutation { createTodo(input: CreateTodoInput!): CreateTodoPayload! }
ย 
relay cache updater ์˜ˆ์ œ
const serverTodo = store.getRootField('createTodo').getLinkedRecord('todo') const todoEdges = todosConnection?.getLinkedRecords('edges') const newTodoEdge = ConnectionHandler.createEdge( store, todosConnection, serverTodo, // node ํƒ€์ž… 'TodoEdge', )
ย 

buildConnectionEdge

์„œ๋ฒ„์—์„œ ์ค€ Payload๊ฐ€ Connection ์ธ ๊ฒฝ์šฐ์— ํ•ด๋‹น ์—ฃ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ํŠน์ • ์ปค๋„ฅ์…˜์— ์—ฃ์ง€๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค
์„ธ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ edge ์ž„์„ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
notion image
type TodoConnection implements Connection { edges: [TodoEdge!]! pageInfo: PageInfo! count: Int } type Mutation { createTodo(input: CreateTodoInput!): TodoConnection! }
ย 
relay cache updater ์˜ˆ์ œ
const serverEdges = store.getRootField('createTodo').getLinkedRecords('edges') const newEdge = ConnectionHandler.buildConnectionEdge(store, connection, serverEdge) if (!newEdge) { return } ConnectionHandler.insertEdgeAfter(connection, newEdge)
ย 
๊ฒฐ๋ก ์ ์œผ๋กœ
  • node ๋ฅผ ํ†ตํ•ด์„œ connection edge๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด createEdge
  • connection ์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ edge๋ฅผ ํ†ตํ•ด์„œ edge ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด buildConnectionEdge
๋ฅผ ์šฉ๋„์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
โ† Go home