Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(GCOM-1476): Optimize fragments to type data more easily in TypeScript #2357

Open
wants to merge 13 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/kind-chicken-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphcommerce/magento-product-grouped': patch
'@graphcommerce/magento-cart-pickup': patch
'@graphcommerce/magento-cart-items': patch
'@graphcommerce/magento-graphcms': patch
'@graphcommerce/magento-product': patch
'@graphcommerce/magento-cart': patch
---

Create individual fragments for values with children to enhance readability and simplify type checking.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MenuQueryFragment } from '@graphcommerce/magento-category'
import { MenuQueryFragment, CategoryItemFragment } from '@graphcommerce/magento-category'
import {
CategoryDefaultFragment,
FilterTypes,
Expand All @@ -7,7 +7,6 @@ import {
ProductListQuery,
useProductList,
} from '@graphcommerce/magento-product'
import { CategoryPageQuery } from '../../graphql/CategoryPage.gql'

type BaseProps = MenuQueryFragment &
ProductListQuery &
Expand All @@ -25,8 +24,7 @@ type SearchLayoutProps = {
type CategoryLayoutProps = {
id: string
title: string
category: CategoryDefaultFragment &
NonNullable<NonNullable<CategoryPageQuery['categories']>['items']>[number]
category: CategoryDefaultFragment & CategoryItemFragment
}

type ProductListProps = ReturnType<typeof useProductList<BaseProps>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { DownloadableProductOptions } from '@graphcommerce/magento-product-downl
import { GroupedProducts } from '@graphcommerce/magento-product-grouped'
import { isTypename } from '@graphcommerce/next-ui'
import { Divider, Typography } from '@mui/material'
import { ProductPage2Query } from '../../graphql/ProductPage2.gql'
import { ProductPage2ItemFragment } from '../../graphql/ProductPage2Item.gql'

export type AddProductsToCartViewProps = {
product: NonNullable<NonNullable<NonNullable<ProductPage2Query['products']>['items']>[number]>
product: ProductPage2ItemFragment
}

export function AddProductsToCartView(props: AddProductsToCartViewProps) {
Expand Down
10 changes: 1 addition & 9 deletions examples/magento-graphcms/graphql/ProductPage2.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ query ProductPage2(
products(filter: { url_key: { eq: $urlKey } }) @inContext(context: $context) {
...ProductSpecs
items {
__typename
uid
...ProductCustomizable
...ProductWeight
...ProductPageItem
...ConfigurableOptions
...DownloadableProductOptions
...BundleProductOptions
...GroupedProduct
...ProductPage2Item
}
}
# Workaround for https://github.com/magento/magento2/issues/32427
Expand Down
11 changes: 11 additions & 0 deletions examples/magento-graphcms/graphql/ProductPage2Item.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fragment ProductPage2Item on ProductInterface {
__typename
uid
...ProductPageItem
...ProductCustomizable
...ProductWeight
...ConfigurableOptions
...DownloadableProductOptions
...BundleProductOptions
...GroupedProduct
}
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/checkout/terms/[url].tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PageOptions } from '@graphcommerce/framer-next-pages'
import { CartAgreementsDocument, CartAgreementsQuery } from '@graphcommerce/magento-cart'
import { CartAgreementFragment, CartAgreementsDocument } from '@graphcommerce/magento-cart'
import { StoreConfigDocument } from '@graphcommerce/magento-store'
import { GetStaticProps, PageMeta, LayoutOverlayHeader, LayoutTitle } from '@graphcommerce/next-ui'
import { Container, Typography } from '@mui/material'
import { GetStaticPaths } from 'next'
import { LayoutOverlay, LayoutOverlayProps } from '../../../components'
import { graphqlSsrClient, graphqlSharedClient } from '../../../lib/graphql/graphqlSsrClient'

type Props = { agreement: NonNullable<NonNullable<CartAgreementsQuery['checkoutAgreements']>[0]> }
type Props = { agreement: CartAgreementFragment }
type RouteProps = { url: string }
type GetPageStaticPaths = GetStaticPaths<RouteProps>
type GetPageStaticProps = GetStaticProps<LayoutOverlayProps, Props>
Expand Down
2 changes: 0 additions & 2 deletions packages/magento-cart-items/Api/CartItems.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
fragment CartItems on Cart {
id
items {
uid
__typename
...CartItem
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RenderType, TypeRenderer } from '@graphcommerce/next-ui'
import { Box } from '@mui/material'
import { CartItemFragment } from '../../Api/CartItem.gql'
import { CartItemsFragment } from '../../Api/CartItems.gql'

export type CartItemRenderer = TypeRenderer<NonNullable<NonNullable<CartItemsFragment['items']>[0]>>
export type CartItemRenderer = TypeRenderer<CartItemFragment>

export type CartProps = { renderer: CartItemRenderer } & CartItemsFragment

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ActionCard, ActionCardItemRenderProps } from '@graphcommerce/next-ui'
import { Trans } from '@lingui/react'
import { Box, Button } from '@mui/material'
import { GetPickupLocationsForProductsQuery } from '../graphql/GetPickupLocationsForProducts.gql'
import { PickupLocationItemFragment } from '../graphql/PickupLocationItem.gql'

export type Location = NonNullable<
NonNullable<NonNullable<GetPickupLocationsForProductsQuery['pickupLocations']>['items']>[number]
>
export type Location = PickupLocationItemFragment

export function PickupLocationActionCard(props: ActionCardItemRenderProps<Location>) {
const { onReset, name, contact_name, street, postcode, city, description, ...cardProps } = props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,7 @@ query GetPickupLocationsForProducts(
currentPage: $currentPage
) {
items {
city
pickup_location_code
contact_name
country_id
description
email
fax
latitude
longitude
name
phone
postcode
region
region_id
street
...PickupLocationItem
}
}
}
17 changes: 17 additions & 0 deletions packages/magento-cart-pickup/graphql/PickupLocationItem.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fragment PickupLocationItem on PickupLocation {
city
pickup_location_code
contact_name
country_id
description
email
fax
latitude
longitude
name
phone
postcode
region
region_id
street
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fragment CartAgreement on CheckoutAgreement {
agreement_id
checkbox_text
content
content_height
is_html
mode
name
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
query CartAgreements {
checkoutAgreements {
agreement_id
checkbox_text
content
content_height
is_html
mode
name
...CartAgreement
}
}
1 change: 1 addition & 0 deletions packages/magento-cart/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './ApolloCartError'
export * from './CartAdded'
export * from './CartAddressMultiLine/CartAddressMultiLine'
export * from './CartAddressSingleLine/CartAddressSingleLine'
export * from './CartAgreementsForm/CartAgreement.gql'
export * from './CartAgreementsForm/CartAgreements.gql'
export * from './CartAgreementsForm/CartAgreementsForm'
export * from './CartFab/CartFab'
Expand Down
2 changes: 2 additions & 0 deletions packages/magento-category/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export * from './utils/magentoMenuToNavigation'
export * from './utils/findParentBreadcrumbItem'
export * from './queries/getCategoryStaticPaths'
export * from './components/CategoryBreadcrumb'
export * from './queries/CategoryItem.gql'
export * from './queries/CategoryQueryFragment.gql'
export * from './queries/MenuQueryFragment.gql'
export * from './queries/MenuQueryCategoryItem.gql'
export * from './queries/NavigationItem.gql'
export * from './utils/useCategoryTree'
9 changes: 9 additions & 0 deletions packages/magento-category/queries/CategoryItem.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fragment CategoryItem on CategoryTree {
uid
...CategoryDefault
...CategoryBreadcrumb
...CategoryDescription
...CategoryChildren
...CategoryMeta
...CategoryHeroNav
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
fragment CategoryQueryFragment on Query {
categories(filters: { url_path: { eq: $url } }) {
items {
uid
...CategoryDefault
...CategoryBreadcrumb
...CategoryDescription
...CategoryChildren
...CategoryMeta
...CategoryHeroNav
...CategoryItem
}
}
}
16 changes: 16 additions & 0 deletions packages/magento-category/queries/MenuQueryCategoryItem.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fragment MenuQueryCategoryItem on CategoryTree {
uid
...NavigationItem
children {
uid
...NavigationItem
children {
uid
...NavigationItem
children {
uid
...NavigationItem
}
}
}
}
15 changes: 1 addition & 14 deletions packages/magento-category/queries/MenuQueryFragment.graphql
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
fragment MenuQueryFragment on Query {
menu: categories {
items {
uid
...NavigationItem
children {
uid
...NavigationItem
children {
uid
...NavigationItem
children {
uid
...NavigationItem
}
}
}
...MenuQueryCategoryItem
}
}
}
5 changes: 2 additions & 3 deletions packages/magento-category/utils/magentoMenuToNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import {
nonNullable,
} from '@graphcommerce/next-ui'
import { i18n } from '@lingui/core'
import { MenuQueryCategoryItemFragment } from '../queries/MenuQueryCategoryItem.gql'
import { MenuQueryFragment } from '../queries/MenuQueryFragment.gql'
import { NavigationItemFragment } from '../queries/NavigationItem.gql'

type Item = NonNullable<NonNullable<NonNullable<MenuQueryFragment['menu']>['items']>[0]>

export type MagentoNavigationItemProps = NavigationItemFragment

function categoryToNav(
props: Item | null | undefined,
props: MenuQueryCategoryItemFragment | null | undefined,
Component?: React.ComponentType<MagentoNavigationItemProps>,
): NavigationNode | undefined {
if (!props) return undefined
Expand Down
2 changes: 2 additions & 0 deletions packages/magento-product-grouped/GroupedProduct.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
fragment GroupedProduct on GroupedProduct {
__typename
uid
items {
position
qty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ActionCardLayout, filterNonNullableKeys } from '@graphcommerce/next-ui'
import { useMemo } from 'react'
import { ProductPageGroupedQueryFragment } from '../ProductPageGroupedQueryFragment.gql'
import { GroupedProductFragment } from '../GroupedProduct.gql'
import { GroupedProductActionCard } from './GroupedProductActionCard'

type GroupedProductsProps = {
product: NonNullable<NonNullable<ProductPageGroupedQueryFragment['typeProducts']>['items']>[0]
product: GroupedProductFragment
}

export function GroupedProducts(props: GroupedProductsProps) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
fragment ProductPageGroupedQueryFragment on Query {
typeProducts: products(filter: { url_key: { eq: $urlKey } }) {
items {
__typename
uid
...GroupedProduct
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { i18n } from '@lingui/core'
import { Box } from '@mui/material'
import React from 'react'
import { useFormAddProductsToCart } from '../AddProductsToCart'
import { ProductCustomizableFragment } from './ProductCustomizable.gql'
import { CustomizableOptionFragment } from './CustomizableOption.gql'

export type OptionTypeRenderer = TypeRenderer<
NonNullable<NonNullable<ProductCustomizableFragment['options']>[number]> & {
CustomizableOptionFragment & {
optionIndex: number
index: number
currency: CurrencyEnum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
fragment CustomizableOption on CustomizableOptionInterface {
__typename
uid
required
sort_order
title
...CustomizableAreaOption
...CustomizableCheckboxOption
...CustomizableDateOption
...CustomizableDropDownOption
...CustomizableMultipleOption
...CustomizableFieldOption
...CustomizableFileOption
...CustomizableRadioOption
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
fragment ProductCustomizable on CustomizableProductInterface {
options {
uid
__typename
...CustomizableOption
...CustomizableAreaOption
...CustomizableCheckboxOption
...CustomizableDateOption
...CustomizableDropDownOption
...CustomizableMultipleOption
...CustomizableFieldOption
...CustomizableFileOption
...CustomizableRadioOption
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AttributeFrontendInputEnum } from '@graphcommerce/graphql-mesh'
import { ProductListFiltersFragment } from '../ProductListFilters/ProductListFilters.gql'
import { ProductListAggregationFragment } from '../ProductListFilters'
import { ProductFilterEqualChip } from './ProductFilterEqualChip'
import { ProductFilterEqualSection } from './ProductFilterEqualSection'
import { ProductFilterRangeChip } from './ProductFilterRangeChip'
Expand All @@ -9,7 +9,7 @@ import { excludeCategory } from './activeAggregations'
import { applyAggregationCount } from './applyAggregationCount'

export type FilterProps = {
aggregation: NonNullable<NonNullable<ProductListFiltersFragment['aggregations']>[number]>
aggregation: ProductListAggregationFragment
}

export type FilterRenderer = Record<AttributeFrontendInputEnum, React.FC<FilterProps>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fragment ProductListAggregation on Aggregation {
__typename
label
attribute_code
count
options {
__typename
label
value
count
}
}
Loading
Loading