• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Shopify/graphql_swift_gen: GraphQL swift client code generator

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Shopify/graphql_swift_gen

开源软件地址(OpenSource Url):

https://github.com/Shopify/graphql_swift_gen

开源编程语言(OpenSource Language):

Swift 66.2%

开源软件介绍(OpenSource Introduction):

GraphQLSwiftGen

Build Status

Generate swift code for any specific GraphQL schema that provides query builders and response classes.

Installation

The code generator requires ruby version 2.1 or later.

Until this project is released, it is recommended to include it into a project as a git submodule.

$ git submodule https://github.com/Shopify/graphql_swift_gen.git

It is recommended to use bundler to install the code generators ruby package.

Add this line to your application's Gemfile:

gem 'graphql_swift_gen', path: 'graphql_swift_gen'

And then execute:

$ bundle

The generated code depends on support/Sources/GraphQL.swift from this repo which should be added to the project along with the generated code.

Usage

Create a script that generates the code for a GraphQL API

require 'graphql_swift_gen'
require 'graphql_schema'
require 'json'

introspection_result = File.read("graphql_schema.json")
schema = GraphQLSchema.new(JSON.parse(introspection_result))

GraphQLSwiftGen.new(schema,
  nest_under: 'ExampleSchema',
  custom_scalars: [
    GraphQLSwiftGen::Scalar.new(
      type_name: 'Money',
      swift_type: 'NSDecimalNumber',
      deserialize_expr: ->(expr) { "NSDecimalNumber(string: #{expr}, locale: GraphQL.posixLocale)" },
      serialize_expr: ->(expr) { "#{expr}.description(withLocale: GraphQL.posixLocale)" },
    ),
  ]
).save("#{Dir.pwd}/../MyApp/Source")

That generated code depends on the GraphQLSupport package.

The generated code includes a query builder that can be used to create a GraphQL query in a type-safe mannar.

let queryString = ExampleSchema.buildQuery { $0
    .user { $0
        .firstName()
        .lastName()
    }
}

The generated code also includes response classes that will deserialize the response and provide methods for accessing the field data with it already coerced to the correct type.

guard let response = try? GraphQLResponse<ExampleSchema.QueryRoot>(jsonData: response) else {
    print("Invalid GraphQL response")
    return
}
if let errors = response.errors {
    for error in errors {
        print("GraphQL error: \(error.message)")
    }
}
if let data = response.data {
    let user = data.user
    print("\(user.firstName) \(user.lastName)")
}

Development

After checking out the repo, run bundle to install ruby dependencies. Then, run rake test to run the tests.

To install this gem onto your local machine, run bundle exec rake install or reference it from a Gemfile using the path option (e.g. gem 'graphql_swift_gen', path: '~/src/graphql_swift_gen').

Contributing

See our contributing guidelines for more information.

License

The gem is available as open source under the terms of the MIT License.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap