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
Vue 数据流最佳实践:nuxt vue graphql apollo (maybe no vuex) #356
Comments
query 部分完全可以由各依赖的子组件们来提供对应数据片段,即 fragments。 之前的整个 query 内容实在太大了!一个页面根本不需要这么多冗余数据,只取UI需要的数据,按需查询。 fragment PreSale on BffSpu {
preSaleAttribute {
startTime
endTime
scheduledDeliveryTime
}
spu {
isPreSale
}
}
fragment SEO on Product {
seo {
seoTitle
seoKeywords
seoDescription
}
}
fragment Promotion on PromotionActivity {
name
label
description
ruleDesc
id
beginTime
endTime
gifts {
name
number
}
}
fragment Brand on Product {
brand {
imageUrl
code
name
description
}
}
fragment Category on ProductCategory {
name
code
path
} query productDetail($codes: [String!]!) {
shop {
bffSpusByCodes(codes: $codes) {
type
...PreSale
spu {
id
code
title
onShelves
productType
...SEO
promotions {
...Promotion
}
...Brand
categories {
...Category
}
images {
url
}
subTitle
description
inventory
sales
skus {}
bffSkus {}
}
}
} |
ref: Using fragments - ApolloQuery | Vue Apollo <!-- MessageList.vue -->
export default {
fragments: {
message: gql`
fragment message on Message {
id
user {
id
name
}
text
created
}
`
}
} apollo: {
messages: gql`
query GetMessages {
messages {
...message
}
}
${this.$options.fragments.message}
`
} |
但其实,为了不必要的 fragment 层级考虑,还有两种额外的处理方式,方便管理&解除耦合关系,同时也符合 vuex 中央集权式的理念。
当然,这也需要后端的支持,因为此时请求的是 import { BatchHttpLink } from 'apollo-link-batch-http'
const httpLinkOptions = {
uri,
fetch,
credentials: 'same-origin',
headers: {},
}
const httpLink = createHttpLink(httpLinkOptions)
const batchLink = new BatchHttpLink({
...httpLinkOptions,
uri: `${uri}s`,
})
query fetchDynamicOrganismData($codes: ProductCodeInput) {
... on Query {
... on Query {
Product: shop {
currency
decimalPlaces
productByCode(codes: $codes) {
id
code
title
onShelves
images {
url
__typename
}
salePrice {
amount
__typename
}
skus {
id
code
isEnabled
__typename
}
__typename
}
__typename
}
__typename
}
... on Query {
ProductCarousel: shop {
currency
decimalPlaces
productByCode(codes: $codes) {
id
code
title
onShelves
images {
url
__typename
}
salePrice {
amount
__typename
}
listPrice {
amount
__typename
}
skus {
id
code
isEnabled
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
} 用于合并时的代码(有待改进,现在过多 import gql from 'fraql'
const getDynamicFragments = organismsList =>
Object.values(organisms)
.filter(comp =>
comp.fragment &&
!!comp.fragment.dynamic &&
organismsList.find(type => type === comp.name))
.map(comp => comp.fragment.dynamic)
.reduce((a, b) => gql`
fragment _ on Query {
${a}
${b}
}
`) 而且需要通过 |
两者对比可以看出,option 1 明显要简单直接一些: 潜在的优化包括:option 1 能够做到 query 的分时,从而让组件自由选择在 SSR 端或 client 端进行数据请求,从而更精确控制请求性能或是 SEO 数据。 |
关于 GraphQL 作为后端 BFF 的定位和实践,可能面临的问题:
|
graphql mutation 要以input为入参
pages/xxx apollo.js components.js
query就该离得最近, 子组件也由自己去取
或者就使用fragememts吧,如果想合并请求,或者是考虑page route 入参
SSR渲染,只可能发生在fetch或者async data,但是这样的话 apollo怎么到data呢?
::: tip You have access to this in options like variables, even on the server! :::
The text was updated successfully, but these errors were encountered: