Skip to content

Commit

Permalink
Merge pull request #2289 from graphcommerce-org/feature/injectable-fr…
Browse files Browse the repository at this point in the history
…agments2

Feature/injectable fragments2
  • Loading branch information
paales authored Jun 5, 2024
2 parents 3eca112 + cd97bc7 commit 0f44bfe
Show file tree
Hide file tree
Showing 45 changed files with 55 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-seahorses-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphcommerce/graphql-codegen-near-operation-file": patch
---

Do not require fragments to be marked @injectable but always accept @inject directives.
6 changes: 3 additions & 3 deletions docs/framework/plugins-graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ query ProductList(
}

# Fragment used for a ProductListItem used when rendering any listing
fragment ProductListItem on ProductInterface @injectable {
fragment ProductListItem on ProductInterface {
uid
...ProductLink
sku
Expand All @@ -59,8 +59,8 @@ fragment ProductListItem on ProductInterface @injectable {

## How to modify an existing query?

Some Fragments inside GraphCommerce have been marked as `@injectable` which
allows you to inject our own fields in the fragments.
Fragments inside GraphCommerce allows you to inject our own fields in the
fragments.

```graphql
fragment MyCustomFragment on ProductInterface
Expand Down
2 changes: 1 addition & 1 deletion packages/hygraph-dynamic-rows/graphql/DynamicRow.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment DynamicRow on DynamicRow @injectable {
fragment DynamicRow on DynamicRow {
placement
target {
... on Node {
Expand Down
2 changes: 1 addition & 1 deletion packages/hygraph-ui/components/Asset/Asset.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment Asset on Asset @injectable {
fragment Asset on Asset {
url
width
height
Expand Down
2 changes: 1 addition & 1 deletion packages/hygraph-ui/graphql/HygraphPage.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment HygraphPage on Page @injectable {
fragment HygraphPage on Page {
title
metaTitle
metaDescription
Expand Down
2 changes: 1 addition & 1 deletion packages/hygraph-ui/graphql/PageLink.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment PageLink on PageLink @injectable {
fragment PageLink on PageLink {
title
url
description {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CartBillingAddress on Cart @injectable {
fragment CartBillingAddress on Cart {
id
__typename
billing_address {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CartPageFragment on Cart @injectable {
fragment CartPageFragment on Cart {
id
__typename
email
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ShippingPageFragment on Cart @injectable {
fragment ShippingPageFragment on Cart {
...BillingAddress
...ShippingAddress
...CartSummary
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-cart-coupon/Api/Coupon.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment Coupon on Cart @injectable {
fragment Coupon on Cart {
id
__typename
applied_coupons {
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-cart-email/Api/GuestEmailChanged.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fragment GuestEmailChanged on Cart @injectable {
fragment GuestEmailChanged on Cart {
id
}
2 changes: 1 addition & 1 deletion packages/magento-cart-items/Api/CartItem.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CartItem on CartItemInterface @injectable {
fragment CartItem on CartItemInterface {
uid
quantity
__typename
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-cart-items/Api/CartItems.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CartItems on Cart @injectable {
fragment CartItems on Cart {
id
items {
uid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment AvailablePaymentMethod on AvailablePaymentMethod @injectable {
fragment AvailablePaymentMethod on AvailablePaymentMethod {
code
title
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment PaymentMethodContext on Cart @injectable {
fragment PaymentMethodContext on Cart {
id
available_payment_methods {
...AvailablePaymentMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment PaymentMethodUpdated on Cart @injectable {
fragment PaymentMethodUpdated on Cart {
id
selected_payment_method {
...SelectedPaymentMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment SelectedPaymentMethod on SelectedPaymentMethod @injectable {
fragment SelectedPaymentMethod on SelectedPaymentMethod {
code
title
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment BillingAddress on Cart @injectable {
fragment BillingAddress on Cart {
id
billing_address {
...CartAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ShippingAddress on Cart @injectable {
fragment ShippingAddress on Cart {
id
is_virtual
shipping_addresses {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment AvailableShippingMethods on Cart @injectable @inject(into: ["ShippingAddress"]) {
fragment AvailableShippingMethods on Cart @inject(into: ["ShippingAddress"]) {
shipping_addresses {
available_shipping_methods {
...AvailableShippingMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ShippingMethodSelected on Cart @injectable {
fragment ShippingMethodSelected on Cart {
id
shipping_addresses {
selected_shipping_method {
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-cart/Api/CartItemCountChanged.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CartItemCountChanged on Cart @injectable {
fragment CartItemCountChanged on Cart {
__typename
id
total_quantity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CartStartCheckout on Cart @injectable {
fragment CartStartCheckout on Cart {
prices {
grand_total {
...Money
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
fragment CartTotals on Cart
@injectable
@inject(into: ["CartItemCountChanged", "PaymentMethodUpdated"]) {
fragment CartTotals on Cart @inject(into: ["CartItemCountChanged", "PaymentMethodUpdated"]) {
shipping_addresses {
selected_shipping_method {
carrier_code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment OrderSuccesPage on Cart @injectable {
fragment OrderSuccesPage on Cart {
...CartItems
...CartTotals
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CategoryDescription on CategoryTree @injectable {
fragment CategoryDescription on CategoryTree {
uid
description
name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CategoryHeroNav on CategoryTree @injectable {
fragment CategoryHeroNav on CategoryTree {
uid
level
is_anchor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CategoryPathFragment on CategoryTree @injectable {
fragment CategoryPathFragment on CategoryTree {
uid
url_path
children {
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-category/queries/NavigationItem.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment NavigationItem on CategoryTree @injectable {
fragment NavigationItem on CategoryTree {
uid
include_in_menu
name
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-cms/CmsPageContent.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CmsPageContent on CmsPage @injectable {
fragment CmsPageContent on CmsPage {
content_heading
content
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fragment AccountDashboardCustomer on Customer @injectable {
fragment AccountDashboardCustomer on Customer {
email
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment CustomerTokenFragment on CustomerToken @injectable {
fragment CustomerTokenFragment on CustomerToken {
__typename
token
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ConfigurableOptions on ConfigurableProduct @injectable {
fragment ConfigurableOptions on ConfigurableProduct {
__typename
uid
sku
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ConfigurableOptionsSelection on ConfigurableProduct @injectable {
fragment ConfigurableOptionsSelection on ConfigurableProduct {
configurable_product_options_selection(configurableOptionValueUids: $selectedOptions) {
__typename
configurable_options {
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-product/Api/ProductListItem.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ProductListItem on ProductInterface @injectable {
fragment ProductListItem on ProductInterface {
uid
...ProductLink
sku
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-product/Api/ProductPageItem.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ProductPageItem on ProductInterface @injectable {
fragment ProductPageItem on ProductInterface {
__typename
uid
...ProductListItem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment UseAddProductsToCartAction on ProductInterface @injectable {
fragment UseAddProductsToCartAction on ProductInterface {
uid
sku
only_x_left_in_stock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment JsonLdProduct on ProductInterface @inject(into: ["ProductPageItem"]) @injectable {
fragment JsonLdProduct on ProductInterface @inject(into: ["ProductPageItem"]) {
uid
name
sku
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ProductCustomizable on CustomizableProductInterface @injectable {
fragment ProductCustomizable on CustomizableProductInterface {
options {
uid
__typename
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ComplexTextValue on ComplexTextValue @injectable {
fragment ComplexTextValue on ComplexTextValue {
html
__typename
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment ProductPageMeta on ProductInterface @injectable {
fragment ProductPageMeta on ProductInterface {
...ProductLink
sku
name
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-store/StoreConfigFragment.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fragment StoreConfigFragment on StoreConfig @injectable {
fragment StoreConfigFragment on StoreConfig {
website_name
store_code
store_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ function injectInjectable(injectables, injector) {
});
});
if (!found)
throwInjectError(injectVal, `fragment ${target} @injectable { ... } can not be found or isn't injectable`);
throwInjectError(injectVal, `fragment ${target} { ... } can not be found`);
});
}
function injectableDirective(documentFiles) {
const documents = documentFiles
.map(({ document }) => document)
.filter((doc) => doc);
const injectables = documents.filter((d) => isFragment(d) && hasInjectableDirective(d));
const injectables = documents.filter((d) => isFragment(d));
const injectors = documents.filter((d) => isFragment(d) && hasInjectDirective(d));
injectors.forEach((d) => injectInjectable(injectables, d));
return documentFiles;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
DEPRECATED, CAN BE COMPLETELY OMITTED
Defines wheter a Fragment can be injected
```graphql
Expand All @@ -10,7 +12,7 @@ fragment MyInjectableFragment on Model @injectable {
directive @injectable on FRAGMENT_DEFINITION

"""
Defines whether a Fragment injects into an @injectable
Defines whether a Fragment injects into another Fragment
```graphql
fragment MyFragment on Model @inject(into ["MyInjectableFragment"]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ function injectInjectable(injectables: DocumentNode[], injector: DocumentNode) {
},
})
})
if (!found)
throwInjectError(
injectVal,
`fragment ${target} @injectable { ... } can not be found or isn't injectable`,
)
if (!found) throwInjectError(injectVal, `fragment ${target} { ... } can not be found`)
})
}

Expand All @@ -117,7 +113,7 @@ export function injectableDirective(documentFiles: Types.DocumentFile[]) {
.map(({ document }) => document)
.filter((doc) => doc) as DocumentNode[]

const injectables = documents.filter((d) => isFragment(d) && hasInjectableDirective(d))
const injectables = documents.filter((d) => isFragment(d))

const injectors = documents.filter((d) => isFragment(d) && hasInjectDirective(d))

Expand Down

0 comments on commit 0f44bfe

Please sign in to comment.