Setup Apollo
安裝
安裝官網(wǎng)
通過(guò)cocoapods添加
- 在podfile中添加
pod 'Apollo'
pod install
添加自動(dòng)生成API文件腳本
在編譯之前,需將已下載的schema文件轉(zhuǎn)為iOS可運(yùn)行的代碼API.swift
target -> Build Phases -> click the "+" -> "New Run Script Phase"
添加如下代碼
APOLLO_FRAMEWORK_PATH="$(eval find $FRAMEWORK_SEARCH_PATHS -name "Apollo.framework" -maxdepth 1)"
if [ -z "$APOLLO_FRAMEWORK_PATH" ]; then
echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project."
exit 1
fi
cd "${SRCROOT}/${TARGET_NAME}"
$APOLLO_FRAMEWORK_PATH/check-and-run-apollo-cli.sh codegen:generate --queries="$(find . -name '*.graphql')" --schema=schema.json API.swift
image.png
添加schema文件
從服務(wù)器下載已定義的schema文件
apollo schema:download $filePath --endpoint=$endpointULR
如:apollo schema:download --endpoint=http://localhost:8080/graphql schema.json
其他參數(shù)
build 工程
setup apollo成功后耸别,創(chuàng)建對(duì)應(yīng)的.graphql
文件悯嗓,build工程后將在API文件中生成對(duì)應(yīng)的代碼。
在xcode中安裝graphql插件
安裝xcode-graphql插件颤练,語(yǔ)法高亮.graphql
文件
- 下載xcode-graphql
- 在此工程文件中執(zhí)行
./setup.sh
- 重啟xcode
image.png
image.png
graphql-playground
建議安裝graphql-playground
支持歷史查詢(xún)、配置header驱负,自動(dòng)reload schema等
方便與后端聯(lián)調(diào)接口嗦玖,查看類(lèi)型,參數(shù)說(shuō)明等跃脊。
brew cask install graphql-playground