Skip to content

Commit 96374f6

Browse files
doc: Adds usage guide in readme
1 parent edd1d67 commit 96374f6

File tree

1 file changed

+111
-7
lines changed

1 file changed

+111
-7
lines changed

README.md

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,120 @@
11
# GraphQL
22

3-
The Swift implementation for GraphQL, a query language for APIs created by Facebook.
4-
53
[![Swift][swift-badge]][swift-url]
64
[![License][mit-badge]][mit-url]
75
[![Slack][slack-badge]][slack-url]
86
[![GitHub Actions][gh-actions-badge]][gh-actions-url]
97
[![Codebeat][codebeat-badge]][codebeat-url]
108

11-
Looking for help? Find resources [from the community](http://graphql.org/community/).
9+
The Swift implementation for GraphQL, a query language for APIs created by Facebook.
1210

13-
## Graphiti
11+
Looking for help? Find resources [from the community](http://graphql.org/community/).
1412

15-
This repo only contains the core GraphQL implementation. For a better experience when creating your GraphQL schema use [Graphiti](https://github.com/GraphQLSwift/Graphiti).
13+
## Usage
14+
15+
### Schema Definition
16+
17+
The `GraphQLSchema` object can be used to define [GraphQL Schemas and Types](https://graphql.org/learn/schema/).
18+
These schemas are made up of types, fields, arguments, and resolver functions. Below is an example:
19+
20+
```swift
21+
let schema = try GraphQLSchema(
22+
query: GraphQLObjectType( // Defines the special "query" type
23+
name: "Query",
24+
fields: [
25+
"hello": GraphQLField( // Users may query 'hello'
26+
type: GraphQLString, // The result is a string type
27+
resolve: { _, _, _, _ in
28+
"world" // The result of querying 'hello' is "world"
29+
}
30+
)
31+
]
32+
)
33+
)
34+
```
35+
36+
For more complex schema examples see the test files.
37+
38+
This repo only contains the core GraphQL implementation and does not focus on the ease of schema creation. For a better experience
39+
when creating your GraphQL schema use [Graphiti](https://github.com/GraphQLSwift/Graphiti).
40+
41+
### Execution
42+
43+
Once a schema has been defined queries may be executed against it using the global `graphql` function:
44+
45+
```swift
46+
let result = try graphql(
47+
schema: schema,
48+
request: "{ hello }",
49+
eventLoopGroup: eventLoopGroup
50+
).wait()
51+
```
52+
53+
The result of this query is a `GraphQLResult` that encodes to the following JSON:
54+
55+
```json
56+
{ "hello": "world" }
57+
```
58+
59+
### Subscription
60+
61+
This package supports GraphQL subscription, but until the integration of `AsyncSequence` in Swift 5.5 the standard Swift library did not
62+
provide an event-stream construct. For historical reasons and backwards compatibility, this library implements subscriptions using an
63+
`EventStream` protocol that nearly every asynchronous stream implementation can conform to.
64+
65+
To create a subscription field in a GraphQL schema, use the `subscribe` resolver that returns an `EventStream`. You must also provide a
66+
`resolver`, which defines how to process each event as it occurs and must return the field result type. Here is an example:
67+
68+
```swift
69+
let schema = try GraphQLSchema(
70+
subscribe: GraphQLObjectType(
71+
name: "Subscribe",
72+
fields: [
73+
"hello": GraphQLField(
74+
type: GraphQLString,
75+
resolve: { eventResult, _, _, _, _ in // Defines how to transform each event when it occurs
76+
return eventResult
77+
},
78+
subscribe: { _, _, _, _, _ in // Defines how to construct the event stream
79+
let asyncStream = AsyncThrowingStream<String, Error> { continuation in
80+
let timer = Timer.scheduledTimer(
81+
withTimeInterval: 3,
82+
repeats: true,
83+
) {
84+
continuation.yield("world") // Emits "world" every 3 seconds
85+
}
86+
}
87+
return ConcurrentEventStream<String>(asyncStream)
88+
}
89+
)
90+
]
91+
)
92+
)
93+
```
94+
95+
To execute a subscription use the `graphqlSubscribe` function:
96+
97+
```swift
98+
let subscriptionResult = try graphqlSubscribe(
99+
schema: schema,
100+
request: "{ hello }",
101+
eventLoopGroup: eventLoopGroup
102+
).wait()
103+
// Must downcast from EventStream to concrete type to use in 'for await' loop below
104+
let concurrentStream = subscriptionResult.stream! as! ConcurrentEventStream
105+
for try await result in concurrentStream.stream {
106+
print(result)
107+
}
108+
```
109+
110+
The code above will print the following JSON every 3 seconds:
111+
112+
```json
113+
{ "hello": "world" }
114+
```
115+
116+
The example above assumes that your environment has access to Swift Concurrency. If that is not the case, try using
117+
[GraphQLRxSwift](https://github.com/GraphQLSwift/GraphQLRxSwift)
16118

17119
## Encoding Results
18120

@@ -22,7 +124,9 @@ should be encoded using the `GraphQLJSONEncoder` provided by this package.
22124

23125
## Contributing
24126

25-
Most of this repo mirrors the structure of the canonical GraphQL implementation written in Javascript/Typescript. If there is any feature missing, looking at the original code and "translating" it to Swift works, most of the time. For example:
127+
Most of this repo mirrors the structure of
128+
(the canonical GraphQL implementation written in Javascript/Typescript)[https://github.com/graphql/graphql-js]. If there is any feature
129+
missing, looking at the original code and "translating" it to Swift works, most of the time. For example:
26130

27131
### Swift
28132

@@ -37,7 +141,7 @@ Most of this repo mirrors the structure of the canonical GraphQL implementation
37141

38142
This project is released under the MIT license. See [LICENSE](LICENSE) for details.
39143

40-
[swift-badge]: https://img.shields.io/badge/Swift-5.2-orange.svg?style=flat
144+
[swift-badge]: https://img.shields.io/badge/Swift-5.5-orange.svg?style=flat
41145
[swift-url]: https://swift.org
42146

43147
[mit-badge]: https://img.shields.io/badge/License-MIT-blue.svg?style=flat

0 commit comments

Comments
 (0)