在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):GraphQLSwift/Graphiti开源软件地址(OpenSource Url):https://github.com/GraphQLSwift/Graphiti开源编程语言(OpenSource Language):Swift 100.0%开源软件介绍(OpenSource Introduction):GraphitiGraphiti is a Swift library for building GraphQL schemas fast, safely and easily. Looking for help? Find resources from the community. Getting StartedAn overview of GraphQL in general is available in the README for the Specification for GraphQL. That overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started with this repository is to walk through that README and the corresponding tests in parallel. Using GraphitiAdd Graphiti to your import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/GraphQLSwift/Graphiti.git", .upToNextMinor(from: "0.20.1")),
]
) Graphiti provides two important capabilities: building a type schema, and serving queries against that type schema. Defining entitiesFirst, we declare our regular Swift entities. struct Message : Codable {
let content: String
} Defining the contextSecond step is to create your application's context. The context will be passed to all of your field resolver functions. This allows you to apply dependency injection to your API. This is the place where you can put code that talks to a database or another service. struct Context {
func message() -> Message {
Message(content: "Hello, world!")
}
} Defining the GraphQL API resolverNow that we have our entities and context we can create the GraphQL API resolver. import Graphiti
struct Resolver {
func message(context: Context, arguments: NoArguments) -> Message {
context.message()
}
} Defining the GraphQL API schemaNow we can finally define the GraphQL API with its schema. struct MessageAPI : API {
let resolver: Resolver
let schema: Schema<Resolver, Context>
init(resolver: Resolver) throws {
self.resolver = resolver
self.schema = try Schema<Resolver, Context> {
Type(Message.self) {
Field("content", at: \.content)
}
Query {
Field("message", at: Resolver.message)
}
}
}
}
QueryingTo query the schema we need to instantiate the api and pass in an EventLoopGroup to feed the execute function alongside the query itself. import NIO
let resolver = Resolver()
let context = Context()
let api = try MessageAPI(resolver: resolver)
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
defer {
try? group.syncShutdownGracefully()
}
api.execute(
request: "{ message { content } }",
context: context,
on: group
).whenSuccess { result in
print(result)
} The output will be: {"data":{"message":{"content":"Hello, world!"}}}
Async resolversTo use async resolvers, just add one more parameter with type import NIO
struct Resolver {
func message(context: Context, arguments: NoArguments, group: EventLoopGroup) -> EventLoopFuture<Message> {
group.next().makeSucceededFuture(context.message())
}
} SubscriptionThis library supports GraphQL subscriptions. To use them, you must create a concrete subclass of the If you don't feel like creating a subclass yourself, you can use the GraphQLRxSwift repository to integrate RxSwift observables out-of-the-box. Or you can use that repository as a reference to connect a different stream library like ReactiveSwift, OpenCombine, or one that you've created yourself. Star Wars API exampleCheck the Star Wars API for a more complete example. LicenseThis project is released under the MIT license. See LICENSE for details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论