Setup Apollo

Setup Apollo

安装

安装官网
通过cocoapods添加

  1. 在podfile中添加pod 'Apollo'
  2. pod install

添加自动生成API文件脚本

在编译之前,需将已下载的schema文件转为iOS可运行的代码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文件

从服务器下载已定义的schema文件
apollo schema:download $filePath --endpoint=$endpointULR
如:apollo schema:download --endpoint=http://localhost:8080/graphql schema.json
其他参数

build 工程

setup apollo成功后,创建对应的.graphql文件,build工程后将在API文件中生成对应的代码。

在xcode中安装graphql插件

安装xcode-graphql插件,语法高亮.graphql文件

  1. 下载xcode-graphql
  2. 在此工程文件中执行./setup.sh
  3. 重启xcode
image.png

image.png

graphql-playground

建议安装graphql-playground
支持历史查询、配置header,自动reload schema等
方便与后端联调接口,查看类型,参数说明等。
brew cask install graphql-playground

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。