coding/react
[Apollo/React] Fragments
코딩희송
2021. 3. 25. 01:55
fragments
는 여러 쿼리를 공유 할 수 있다.
Customer
객체에서 사용할 수 있는 CustomerFeild
:
export const CUSTOMER_FEILD = gql`
fragment CustomerFeild on Customer {
_id
name
birthdate
gender
job
}
`;
스프레드 구문을 통해 참조하는 쿼리에 fragments
를 포함 할 수 있다.
query GetPerson {
people(id: "7") {
...NameParts
avatar(size: LARGE)
}
}
...NameParts
가 갖고있는 정보는 firstName
과 lastName
이다.
각 스프레드구문 덩어리들은 임포트로도 불러서 사용할 수 있다.
import { gql } from '@apollo/client';
import { CORE_COMMENT_FIELDS } from './fragments';
export const GET_POST_DETAILS = gql`
${CORE_COMMENT_FIELDS}
query CommentsForPost($postId: ID!) {
post(postId: $postId) {
title
body
author
comments {
...CoreCommentFields
}
}
}
`;