开源软件名称(OpenSource Name):GraphQLSwift/GraphQL开源软件地址(OpenSource Url):https://github.com/GraphQLSwift/GraphQL开源编程语言(OpenSource Language):Swift 100.0%开源软件介绍(OpenSource Introduction):GraphQLThe Swift implementation for GraphQL, a query language for APIs created by Facebook. Looking for help? Find resources from the community. UsageSchema DefinitionThe let schema = try GraphQLSchema(
query: GraphQLObjectType( // Defines the special "query" type
name: "Query",
fields: [
"hello": GraphQLField( // Users may query 'hello'
type: GraphQLString, // The result is a string type
resolve: { _, _, _, _ in
"world" // The result of querying 'hello' is "world"
}
)
]
)
) For more complex schema examples see the test files. This repo only contains the core GraphQL implementation and does not focus on the ease of schema creation. For a better experience when creating your GraphQL schema use Graphiti. ExecutionOnce a schema has been defined queries may be executed against it using the global let result = try graphql(
schema: schema,
request: "{ hello }",
eventLoopGroup: eventLoopGroup
).wait() The result of this query is a { "hello": "world" } SubscriptionThis package supports GraphQL subscription, but until the integration of To create a subscription field in a GraphQL schema, use the let schema = try GraphQLSchema(
subscribe: GraphQLObjectType(
name: "Subscribe",
fields: [
"hello": GraphQLField(
type: GraphQLString,
resolve: { eventResult, _, _, _, _ in // Defines how to transform each event when it occurs
return eventResult
},
subscribe: { _, _, _, _, _ in // Defines how to construct the event stream
let asyncStream = AsyncThrowingStream<String, Error> { continuation in
let timer = Timer.scheduledTimer(
withTimeInterval: 3,
repeats: true,
) {
continuation.yield("world") // Emits "world" every 3 seconds
}
}
return ConcurrentEventStream<String>(asyncStream)
}
)
]
)
) To execute a subscription use the let subscriptionResult = try graphqlSubscribe(
schema: schema,
request: "{ hello }",
eventLoopGroup: eventLoopGroup
).wait()
// Must downcast from EventStream to concrete type to use in 'for await' loop below
let concurrentStream = subscriptionResult.stream! as! ConcurrentEventStream
for try await result in concurrentStream.stream {
print(result)
} The code above will print the following JSON every 3 seconds: { "hello": "world" } The example above assumes that your environment has access to Swift Concurrency. If that is not the case, try using GraphQLRxSwift Encoding ResultsIf you encode a ContributingIf you think you have found a security vulnerability, please follow the Security guidelines. Those contributing to this package are expected to follow the Swift Code of Conduct, the Swift API Design Guidelines, and the SSWG Technical Best Practices. Most of this repo mirrors the structure of (the canonical GraphQL implementation written in Javascript/Typescript)[https://github.com/graphql/graphql-js]. If there is any feature missing, looking at the original code and "translating" it to Swift works, most of the time. For example: Swift/Sources/GraphQL/Language/AST.swift Javascript/TypescriptLicenseThis 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
请发表评论