Giter Club home page Giter Club logo

graphql_swift_gen's 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.

graphql_swift_gen's People

Contributors

benemdon avatar bouk avatar dbart01 avatar dylanahsmith avatar ignacio-chiazzo avatar jduff avatar krbarnes avatar raulriera avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

graphql_swift_gen's Issues

Gen fails to support Union spec

The spec says that union types don't have any fields and only have possible object types.

It looks like the union implementation will require more thought. If you have any ideas talk here ๐Ÿ‘‡

Until then I will work up a fix so that gen doesn't crash in the meantime.

Replace `git ls-files` in gemspec with something that doesn't depend on git

This line: https://github.com/Shopify/graphql_swift_gen/blob/master/graphql_swift_gen.gemspec#L17 contains git ls-files which means using this gem requires git to be available (with a working .git directory). This is generally considered bad practice.

This repository is imported as a submodule of https://github.com/Shopify/ios.
For CI optimization purposes, we would like to not depend on .git being available inside test containers (Anka macOS containers). The monorepo's .git is about ~2.5 GB of data.

Support serialise nested object as nil

We have to support sending a nested object as nil so as to delete the object.

  objectUpdate(input: $input) {
        userErrors { field, message }
        object {
          id
          title
          suboject {
            id
            name
          }
        }
      }
    }

To do that we have to modify the serialise function.

Cannot convert return expression of type '[MyModelHere?]' to return type '[GraphQL.AbstractResponse]'

I'm using this schema.json file
https://gist.github.com/wwe-johndpope/eff04d5297024f8570174a52a25e7c4f

		override open func fetchChildObjectList(key: String) -> [GraphQL.AbstractResponse] {
			switch(key) {
				case "tags":
				return internalGetTags() ?? [] // failing here

				case "defaultAssetImage":
				return internalGetDefaultAssetImage() ?? []  // failing here

				case "logoImage":
				return internalGetLogoImage() ?? []  // failing here

				default:
				return []
			}

screen shot 2017-10-03 at 1 43 26 pm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.