-
Notifications
You must be signed in to change notification settings - Fork 11
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
Minify GraphQL query #1367
base: master
Are you sure you want to change the base?
Minify GraphQL query #1367
Conversation
有機會看看 PR 前後的 requests 資料量比較嗎? |
const minifyQuery = query => | ||
query | ||
.replace(/\s?([{}()])\s?/g, '$1') | ||
.replace(/\s+/g, ' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看起來大概是 20B,感覺差別不大
那這樣我可能會想,這幾段 regular expression 會不會有意外的 replace 狀況是我們沒想到的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
或者我們也可以用別人寫好的來做,只是怕會不會覺得不想要那麼多套件
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感覺他的套件要 server 端配合><" 不過看到他推薦的這個也蠻有趣的:https://www.apollographql.com/docs/apollo-server/performance/apq/
不只減少傳輸量,還允許 CDN 把結果 cache (這題再說XD)
想問 .replace(/\s?([{}()])\s?/g, '$1')
這段要怎麼理解?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感覺他的套件要 server 端配合><" 不過看到他推薦的這個也蠻有趣的:https://www.apollographql.com/docs/apollo-server/performance/apq/ 不只減少傳輸量,還允許 CDN 把結果 cache (這題再說XD)
我沒發現他這個還包含代換 token ORZ
那這裡有個應該是單純 minify 的:https://github.com/drwpow/gqlmin
想問
.replace(/\s?([{}()])\s?/g, '$1')
這段要怎麼理解?
只是把 query(name: $name) { name }
變成 query(name: $name){name}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我用了另一條路徑,https://github.com/goodjoblife/GoodJobShare/compare/gqlmin
如果 graphql 可以 minify --> 那是不是我們 bundle (compile) 的時候就可以 minify
嘗試了這樣:
- 將 graphql 放到
.gql
的文件 - 改用 import xxx from 'xxx.gql'
原理:
當 webpack 遇到 *.gql
(test: /\.(graphql|gql)$/,
) 的時候,
用 './graphql-loader.js' 去處理,
i.e. 把 *.gql
的內容,透過 gqlmin 處理,並回傳成 js code
基本上
mutation($input: FacebookLoginInput!) {
facebookLogin(input: $input) {
user {
_id
facebook_id
}
token
}
}
會變成
export default "mutation($input:FacebookLoginInput!){facebookLogin(input:$input){user{_id facebook_id}token}}";
所以 `import facebookLogin from './facebookLogin.gql',就會像是 import 一個純文字,後續操作都不變
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
或者這裡有個現成的 minify-graphql-loader 看可不可以直接套用
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這個 package 我發現太久以前了,他宣稱
It must to be used in chain with a GraphQL loader:
但現在的 graphql-tag/loader 在 apollo 會將 *.graphql
的文字解析成 AST
但 minify-graphql-loader 還在假設 graphql-tag/loader 只是幫忙處理 #import
的關鍵字
所以,這個 package 會產生不預期的 minify 結果
(我設定的結果:master...minify-graphql-loader)
Close #1353
這個 PR 是?
減少 graphQL 的空白字元
我應該如何手動測試?