Skip to content

Commit c36f9f6

Browse files
committed
Update README.md
1 parent f14da96 commit c36f9f6

File tree

1 file changed

+21
-45
lines changed

1 file changed

+21
-45
lines changed

README.md

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import PackageDescription
3030

3131
let package = Package(
3232
dependencies: [
33-
.Package(url: "https://github.com/GraphQLSwift/Graphiti.git", .upToNextMinor(from: "0.14.0")),
33+
.Package(url: "https://github.com/GraphQLSwift/Graphiti.git", .upToNextMinor(from: "0.19.0")),
3434
]
3535
)
3636
```
@@ -52,15 +52,15 @@ One of the main design decisions behind Graphiti is **not** to polute your entit
5252

5353
#### Defining the context
5454

55-
Second 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. You will usually use the Context as the state holder of your API. Therefore, this will often be a `class`. We're calling it a store here, because that's the only thing that it does, but you should name it in a way that is appropriate to what your context does.
55+
Second 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. You will usually use the Context as the state holder of your API. Therefore, this will often be a `class`.
5656

5757
```swift
5858
/**
5959
* This data is hard coded for the sake of the demo, but you could imagine
6060
* fetching this data from a database or a backend service instead.
6161
*/
62-
final class MessageStore {
63-
func getMessage() -> Message {
62+
final class MessageContext {
63+
func message() -> Message {
6464
Message(content: "Hello, world!")
6565
}
6666
}
@@ -75,25 +75,9 @@ Now that we have our entities and context we can create the API itself.
7575
```swift
7676
import Graphiti
7777

78-
// We make Message adopt Keyable so we can
79-
// provide keys to be used in the schema.
80-
// This allows you to have different names between
81-
// your properties and the fields you expose in the schema.
82-
extension Message : Keyable {
83-
enum Keys : String {
84-
case content
85-
}
86-
}
87-
88-
struct MessageRoot : Keyable {
89-
enum Keys : String {
90-
case getMessage
91-
}
92-
93-
// The first parameter is the context, you can name it anything you want.
94-
// We're calling it `store` here because the context type is `MessageStore`.
95-
func getMessage(store: MessageStore, arguments: NoArguments) -> Message {
96-
store.getMessage()
78+
struct MessageRoot {
79+
func message(context: MessageContext, arguments: NoArguments) -> Message {
80+
context.message()
9781
}
9882
}
9983
```
@@ -105,22 +89,22 @@ Now we can finally define the Schema using the builder pattern.
10589
```swift
10690
struct MessageAPI : API {
10791
let root: MessageRoot
108-
let schema: Schema<MessageRoot, MessageStore>
92+
let schema: Schema<MessageRoot, MessageContext>
10993

11094
// Notice that `API` allows dependency injection.
11195
// You could pass mocked subtypes of `root` and `context` when testing, for example.
11296
init(root: MessageRoot) throws {
11397
self.root = root
11498

115-
self.schema = try Schema<MessageRoot, MessageStore> { schema in
116-
schema.type(Message.self) { type in
117-
type.field(.content, at: \.content)
99+
self.schema = try Schema<MessageRoot, MessageContext>(
100+
Type(Message.self,
101+
Field("content", at: \.content)
118102
}
119103

120-
schema.query { query in
121-
query.field(.getMessage, at: MessageRoot.getMessage)
122-
}
123-
}
104+
Query(
105+
Field("message", at: MessageRoot.message)
106+
)
107+
)
124108
}
125109
}
126110
```
@@ -133,7 +117,7 @@ To query the schema we need to instantiate the api and pass in an EventLoopGroup
133117
import NIO
134118

135119
let root = MessageRoot()
136-
let context = MessageStore()
120+
let context = MessageContext()
137121
let api = try MessageAPI(root: root)
138122
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
139123

@@ -142,7 +126,7 @@ defer {
142126
}
143127

144128
api.execute(
145-
request: "{ getMessage { content } }",
129+
request: "{ message { content } }",
146130
context: context,
147131
on: group
148132
).whenSuccess { result in
@@ -153,7 +137,7 @@ api.execute(
153137
The output will be:
154138

155139
```json
156-
{"data":{"getMessage":{"content":"Hello, world!"}}}
140+
{"data":{"message":{"content":"Hello, world!"}}}
157141
```
158142

159143
`API.execute` returns a `GraphQLResult` which adopts `Encodable`. You can use it with a `JSONEncoder` to send the response back to the client using JSON.
@@ -165,17 +149,9 @@ To use async resolvers, just add one more parameter with type `EventLoopGroup` t
165149
```swift
166150
import NIO
167151

168-
struct API : Keyable {
169-
enum Keys : String {
170-
case getMessage
171-
}
172-
173-
func getMessage(
174-
store: MessageStore,
175-
arguments: NoArguments,
176-
group: EventLoopGroup
177-
) -> EventLoopFuture<Message> {
178-
group.next().makeSucceededFuture(store.getMessage())
152+
struct MessageRoot {
153+
func message(context: MessageContext, arguments: NoArguments, group: EventLoopGroup) -> EventLoopFuture<Message> {
154+
group.next().makeSucceededFuture(store.message())
179155
}
180156
}
181157
```

0 commit comments

Comments
 (0)