Skip to content

Commit 3a6fb0f

Browse files
migrate from graphql-codegen to genql (#43)
* genql is awesome * changeset * genql prerelease * append ts-nocheck on top of generated files * version
1 parent 450a930 commit 3a6fb0f

38 files changed

+29283
-40210
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# nextjs-localstorage
2+
3+
## 0.1.1
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [7be03f0]
8+
- @bsmnt/storefront-hooks@2.0.2
9+
10+
## 0.1.1-next.0
11+
12+
### Patch Changes
13+
14+
- Updated dependencies [7be03f0]
15+
- @bsmnt/storefront-hooks@1.3.1-next.0

examples/nextjs-localstorage/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nextjs-localstorage",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
@@ -17,6 +17,6 @@
1717
},
1818
"devDependencies": {
1919
"tsconfig": "*",
20-
"typescript": "^4.8.4"
20+
"typescript": "^4.9.5"
2121
}
2222
}

examples/nextjs-shopify/CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# nextjs-shopify
2+
3+
## 0.1.1
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [7be03f0]
8+
- @bsmnt/drop@1.2.1
9+
- @bsmnt/storefront-hooks@2.0.2
10+
11+
## 0.1.1-next.0
12+
13+
### Patch Changes
14+
15+
- Updated dependencies [7be03f0]
16+
- @bsmnt/drop@1.2.1-next.0
17+
- @bsmnt/storefront-hooks@1.3.1-next.0

examples/nextjs-shopify/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nextjs-shopify",
33
"private": true,
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"scripts": {
66
"dev": "next dev",
77
"build": "next build",

examples/nextjs-shopify/src/pages/index.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import styles from '../styles/Home.module.css'
55
import { useCartQuery } from '../storefront/hooks'
66
import { GetStaticProps } from 'next'
77
import { storefront } from '../storefront/sdk-gen/sdk'
8-
import { CollectionFragment } from '../storefront/sdk-gen/generated'
8+
import {
9+
CollectionFragment,
10+
collectionFragment
11+
} from '../storefront/sdk-gen/fragments'
912

1013
type PageProps = {
1114
collections: Array<CollectionFragment>
@@ -75,7 +78,13 @@ export default function Home({ collections }: PageProps) {
7578
}
7679

7780
export const getStaticProps: GetStaticProps<PageProps> = async (context) => {
78-
const { collections } = await storefront.FetchCollections()
81+
const { collections } = await storefront.query({
82+
collections: {
83+
__args: { first: 250 },
84+
nodes: collectionFragment
85+
}
86+
})
87+
7988
return {
8089
props: {
8190
collections: collections.nodes
Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
import { createStorefrontHooks } from '@bsmnt/storefront-hooks'
22
import { storefront } from '../sdk-gen/sdk'
3+
import type {
4+
CartGenqlSelection,
5+
CartUserErrorGenqlSelection,
6+
FieldsSelection,
7+
Cart as GenqlCart
8+
} from '../sdk-gen/generated'
9+
10+
const cartFragment = {
11+
id: true,
12+
checkoutUrl: true,
13+
createdAt: true,
14+
cost: { subtotalAmount: { amount: true, currencyCode: true } }
15+
} satisfies CartGenqlSelection
16+
17+
export type Cart = FieldsSelection<GenqlCart, typeof cartFragment>
18+
19+
const userErrorFragment = {
20+
message: true,
21+
code: true,
22+
field: true
23+
} satisfies CartUserErrorGenqlSelection
324

425
export const {
526
QueryClientProvider,
@@ -12,54 +33,88 @@ export const {
1233
cartCookieKey: 'example-nextjs-shopify',
1334
fetchers: {
1435
fetchCart: async (cartId) => {
15-
const { cart } = await storefront.FetchCart({ id: cartId })
36+
const { cart } = await storefront.query({
37+
cart: {
38+
__args: { id: cartId },
39+
...cartFragment
40+
}
41+
})
42+
1643
if (cart === undefined) throw new Error('Request failed')
1744
return cart
1845
}
1946
},
2047
mutators: {
2148
addLineItemsToCart: async (cartId, lines) => {
22-
const { cartLinesAdd } = await storefront.AddLineItem({
23-
cartId,
24-
lines
49+
const { cartLinesAdd } = await storefront.mutation({
50+
cartLinesAdd: {
51+
__args: {
52+
cartId,
53+
lines
54+
},
55+
cart: cartFragment,
56+
userErrors: userErrorFragment
57+
}
2558
})
59+
2660
return {
2761
data: cartLinesAdd?.cart,
2862
userErrors: cartLinesAdd?.userErrors
2963
}
3064
},
3165
createCart: async () => {
32-
const { cartCreate } = await storefront.CreateCart()
66+
const { cartCreate } = await storefront.mutation({
67+
cartCreate: {
68+
cart: cartFragment,
69+
userErrors: userErrorFragment
70+
}
71+
})
3372
return {
3473
data: cartCreate?.cart,
3574
userErrors: cartCreate?.userErrors
3675
}
3776
},
77+
// TODO we could use the same mutation as createCart?
3878
createCartWithLines: async (lines) => {
39-
const { cartCreate } = await storefront.CreateCartWithLines({ lines })
79+
const { cartCreate } = await storefront.mutation({
80+
cartCreate: {
81+
__args: { input: { lines } },
82+
cart: cartFragment,
83+
userErrors: userErrorFragment
84+
}
85+
})
4086
return {
4187
data: cartCreate?.cart,
4288
userErrors: cartCreate?.userErrors
4389
}
4490
},
4591
removeLineItemsFromCart: async (cartId, lineIds) => {
46-
const { cartLinesRemove } = await storefront.RemoveLineItem({
47-
cartId,
48-
lineIds
92+
const { cartLinesRemove } = await storefront.mutation({
93+
cartLinesRemove: {
94+
__args: { cartId, lineIds },
95+
cart: cartFragment,
96+
userErrors: userErrorFragment
97+
}
4998
})
5099
return {
51100
data: cartLinesRemove?.cart,
52101
userErrors: cartLinesRemove?.userErrors
53102
}
54103
},
55104
updateLineItemsInCart: async (cartId, lines) => {
56-
const { cartLinesUpdate } = await storefront.UpdateLineItem({
57-
cartId,
58-
lines: lines.map((l) => ({
59-
id: l.merchandiseId,
60-
quantity: l.quantity,
61-
attributes: l.attributes
62-
}))
105+
const { cartLinesUpdate } = await storefront.mutation({
106+
cartLinesUpdate: {
107+
__args: {
108+
cartId,
109+
lines: lines.map((l) => ({
110+
id: l.merchandiseId,
111+
quantity: l.quantity,
112+
attributes: l.attributes
113+
}))
114+
},
115+
cart: cartFragment,
116+
userErrors: userErrorFragment
117+
}
63118
})
64119
return {
65120
data: cartLinesUpdate?.cart,
@@ -70,4 +125,4 @@ export const {
70125
createCartIfNotFound: true
71126
})
72127

73-
export { useProductFormHelper } from './use-product-form-helper'
128+
// export { useProductFormHelper } from './use-product-form-helper'

examples/nextjs-shopify/src/storefront/hooks/use-product-form-helper.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import { useCallback, useMemo, useState } from 'react'
2-
3-
import type { ProductFragment, ProductVariant } from '../sdk-gen/generated'
2+
import { Product, ProductVariant } from '../sdk-gen/generated'
43

54
type BareBonesVariant = Pick<
65
ProductVariant,
76
'availableForSale' | 'compareAtPriceV2' | 'priceV2' | 'selectedOptions' | 'id'
87
>
98

10-
type BareBonesProduct = Pick<
11-
ProductFragment,
12-
'options' | 'availableForSale'
13-
> & {
9+
type BareBonesProduct = Pick<Product, 'options' | 'availableForSale'> & {
1410
variants: { nodes: Array<BareBonesVariant> }
1511
} & { [key: string]: unknown }
1612

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import type {
2+
Collection,
3+
Product,
4+
CollectionGenqlSelection,
5+
FieldsSelection,
6+
ProductGenqlSelection,
7+
ImageGenqlSelection,
8+
ProductVariantGenqlSelection,
9+
ProductVariant,
10+
Image,
11+
} from './generated'
12+
13+
const imageFragment = {
14+
url: true,
15+
width: true,
16+
height: true,
17+
altText: true,
18+
} satisfies ImageGenqlSelection
19+
20+
export type ImageFragment = FieldsSelection<Image, typeof imageFragment>
21+
22+
export const productVariantFragment = {
23+
id: true,
24+
title: true,
25+
availableForSale: true,
26+
quantityAvailable: true,
27+
compareAtPriceV2: {
28+
amount: true,
29+
currencyCode: true,
30+
},
31+
priceV2: {
32+
amount: true,
33+
currencyCode: true,
34+
},
35+
image: imageFragment,
36+
selectedOptions: {
37+
name: true,
38+
value: true,
39+
},
40+
} satisfies ProductVariantGenqlSelection
41+
42+
export type ProductVariantFragment = FieldsSelection<
43+
ProductVariant,
44+
typeof productVariantFragment
45+
>
46+
47+
export const productFragment = {
48+
id: true,
49+
title: true,
50+
options: {
51+
__args: { first: 25 },
52+
id: true,
53+
name: true,
54+
values: true,
55+
},
56+
availableForSale: true,
57+
variants: {
58+
__args: { first: 100 },
59+
nodes: productVariantFragment
60+
},
61+
} satisfies ProductGenqlSelection
62+
63+
export type ProductFragment = FieldsSelection<Product, typeof productFragment>
64+
65+
export const collectionFragment = {
66+
id: true,
67+
title: true,
68+
description: true,
69+
products: {
70+
__args: { first: 100 },
71+
nodes: productFragment,
72+
},
73+
} satisfies CollectionGenqlSelection
74+
75+
export type CollectionFragment = FieldsSelection<
76+
Collection,
77+
typeof collectionFragment
78+
>
79+

0 commit comments

Comments
 (0)