MIT License

项目介绍

alexsteinerde/graphql-kit 是一个托管在 GitHub 上的开源项目,主要使用 Swift 编写,采用 MIT License 许可证,目前获得 104 个 Star、18 次 Fork。

仓库全名
alexsteinerde/graphql-kit
开源许可证
MIT License
Star 数
104
Fork 数
18
未关闭 Issue
3

README 摘要

GraphQLKit Easy setup of a GraphQL server with Vapor. It uses the GraphQL implementation of Graphiti. Features [x] Arguments, operation name and query support [x] Normal access to the Request object as in normal Vapor request handlers [x] Accept JSON in the body of a POST request as the GraphQL query [x] POST and GET support [ ] Accept application/graphql content type requests [ ] Downloadable schema file [ ]…

完整内容见 GitHub 原始 README 或本页 。

常见问题

graphql-kit 用什么语言开发?

主要使用 Swift 编写,采用 MIT License 许可证,目前有 104 个 Star。

graphql-kit 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 graphql-kit 的源码?

可以直接克隆仓库:git clone https://github.com/alexsteinerde/graphql-kit.git,默认分支为 master;安装与运行步骤见仓库 README。

graphql-kit 最近还在维护吗?

仓库最近一次更新为 2024年7月24日,当前有 3 个未关闭 Issue;Star 数为 104,可作为社区活跃度的参考。

关于

Easy setup of a GraphQL server with Vapor. It uses the GraphQL implementation of Graphiti.

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
146
价值分
68
活跃状态
活跃

语言占比

Swift

Release

README

GraphQLKit

Language Vapor Version build

Easy setup of a GraphQL server with Vapor. It uses the GraphQL implementation of Graphiti.

Features

  • Arguments, operation name and query support
  • Normal access to the Request object as in normal Vapor request handlers
  • Accept JSON in the body of a POST request as the GraphQL query
  • POST and GET support
  • [ ] Accept application/graphql content type requests
  • Downloadable schema file
  • Multi-Resolver support

Installation

import PackageDescription

let package = Package(
    dependencies: [
    .package(url: "https://github.com/alexsteinerde/graphql-kit.git", from: "2.0.0"),
    ],
    targets: [
    .target(name: "App", dependencies: ["GraphQLKit"]),
    ...
    ]
)

Getting Started

Define your schema

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 repository. 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 documentations. But by including this package some other helper functions are exposed:

Async Resolver

An EventLoopGroup parameter is no longer required for async resolvers as the Request context object already provides access to it’s EventLoopGroup attribute eventLoop.

// Instead of adding an unnecessary parameter
func getAllTodos(store: Request, arguments: NoArguments, _: EventLoopGroup) throws -> EventLoopFuture<[Todo]> {
    Todo.query(on: store).all()
}

// You don't need to provide the eventLoopGroup parameter even when resolving a future.
func getAllTodos(store: Request, arguments: NoArguments) throws -> EventLoopFuture<[Todo]> {
    Todo.query(on: store).all()
}
Enums

It automatically resolves all cases of an enum if the type conforms to CaseIterable.

enum TodoState: String, Codable, CaseIterable {
    case open
    case done
    case forLater
}

Enum(TodoState.self),
Parent, Children and Siblings

Vapor has the functionality to fetch an objects parent, children or siblings automatically with @Parent, @Children and @Siblings types. To integrate this into GraphQL, GraphQLKit provides extensions to the Field type that lets you use the parent, children or siblings property as a keypath. Fetching of those related objects is then done automatically.

:warning: Loading related objects in GraphQL has the N+1 problem. A solution would be to build a DataLoader package for Swift. But this hasn’t been done yet.

final class User: Model {
    ...
    
    @Children(for: \.$user)
    var todos: [Todo]
    
    ...
}

final class Todo: Model {
    ...
    
    @Parent(key: "user_id")
    var user: User
    
    @Siblings(through: TodoTag.self, from: \.$todo, to: \.$tag)
    public var tags: [Tag]
    
    ...
}
// Schema types
Type(User.self) {
    Field("todos", with: \.$todos)
}

Type(Todo.self) {
    Field("user", with: \.$user)
    Field("tags", with: \.$tags)
}

Register the schema on the application

In your configure.swift file call the register(graphQLSchema: Schema<YourResolver, Request>, withResolver: YourResolver) on your Application instance. 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.

// Register the schema and it's resolver.
app.register(graphQLSchema: todoSchema, withResolver: TodoAPI())

License

This project is released under the MIT license. See LICENSE for details.

Contribution

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 :)

评论

暂无评论