|
| 1 | +# GraphQLKit |
| 2 | +[](http://swift.org) |
| 3 | +[](http://vapor.codes) |
| 4 | +[](https://circleci.com/gh/alexsteinerde/graphql-kit) |
| 5 | + |
| 6 | + |
| 7 | +Easy setup of a GraphQL server with Vapor. It uses the GraphQL implementation of [Graphiti](https://github.com/alexsteinerde/Graphiti). |
| 8 | + |
| 9 | +## Features |
| 10 | +- [x] Arguments, operation name and query support |
| 11 | +- [x] Normal access to the `Request` object as in normal Vapor request handlers |
| 12 | +- [x] Accept JSON in the body of a POST request as the GraphQL query |
| 13 | +- [x] POST and GET support |
| 14 | +- [ ] Accept `application/graphql` content type requests |
| 15 | +- [ ] Downloadable schema file |
| 16 | +- [ ] Multi-Resolver support |
| 17 | + |
| 18 | +## Installation |
| 19 | +```Swift |
| 20 | +import PackageDescription |
| 21 | + |
| 22 | +let package = Package( |
| 23 | + dependencies: [ |
| 24 | + .package(url: "https://github.com/alexsteinerde/graphql-kit.git", from: "1.0.0"), |
| 25 | + ], |
| 26 | + targets: [ |
| 27 | + .target(name: "App", dependencies: [<#T##Other Dependencies#>, "GraphQLKit"]), |
| 28 | + ... |
| 29 | + ] |
| 30 | +) |
| 31 | +``` |
| 32 | + |
| 33 | +## Getting Started |
| 34 | +### Define your schema |
| 35 | +This package is setup to accept only `Request` objects as the context object for the schema. This gives the opportunity to access all functionality that Vapor provides, for example authentication, service management and database access. To see an example implementation please have a look at the [`vapor-graphql-template`](https://github.com/alexsteinerde/vapor-graphql-template) repository. |
| 36 | +This package only provides the needed functions to register an existing GraphQL schema on a Vapor application. To define your schema please refer to the [Graphiti](https://github.com/alexsteinerde/Graphiti) documentations. |
| 37 | +But by including this package some other helper functions are exposed: |
| 38 | + |
| 39 | +#### Async Resolver |
| 40 | +An `EventLoopGroup` parameter is no longer required for async resolvers as the `Request` context object already provides access to it's `EventLoopGroup` attribute `eventLoop`. |
| 41 | + |
| 42 | +```Swift |
| 43 | +// Instead of adding an unnecessary parameter |
| 44 | +func getAllTodos(store: Request, arguments: NoArguments, _: EventLoopGroup) throws -> EventLoopFuture<[Todo]> { |
| 45 | + Todo.query(on: store).all() |
| 46 | +} |
| 47 | + |
| 48 | +// You don't need to provide the eventLoopGroup parameter even when resolving a future. |
| 49 | +func getAllTodos(store: Request, arguments: NoArguments) throws -> EventLoopFuture<[Todo]> { |
| 50 | + Todo.query(on: store).all() |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### Register the schema on the router |
| 55 | +In your configure.swift file call the `register(graphQLSchema: <#T##Schema<FieldKeyProvider, Request>#>, withResolver: <#T##FieldKeyProvider#>)`. By default this registers the GET and POST endpoints at `/graphql`. But you can also pass the optional parameter `at:` and override the default value. |
| 56 | + |
| 57 | +```Swift |
| 58 | +let router = EngineRouter.default() |
| 59 | + |
| 60 | +// Register the schema and it's resolver. |
| 61 | +router.register(graphQLSchema: todoSchema, withResolver: TodoAPI()) |
| 62 | + |
| 63 | +services.register(router, as: Router.self) |
| 64 | +``` |
| 65 | + |
| 66 | +## License |
| 67 | +This project is released under the MIT license. See [LICENSE](LICENSE) for details. |
| 68 | + |
| 69 | +## Contribution |
| 70 | +You can contribute to this project by submitting a detailed issue or by forking this project and sending a pull request. Contributions of any kind are very welcome :) |
0 commit comments