@@ -85,44 +85,59 @@ Now we can finally define the GraphQL API with its schema.
8585struct MessageAPI : API {
8686 let resolver: Resolver
8787 let schema: Schema<Resolver, Context>
88-
89- init (resolver : Resolver) throws {
90- self .resolver = resolver
91-
92- self .schema = try Schema< Resolver, Context> {
93- Type (Message.self ) {
94- Field (" content" , at : \.content )
95- }
96-
97- Query {
98- Field (" message" , at : Resolver.message )
99- }
88+ }
89+
90+ let api = MessageAPI (
91+ resolver : Resolver ()
92+ schema: try ! Schema< Resolver, Context> {
93+ Type (Message.self ) {
94+ Field (" content" , at : \.content )
95+ }
96+
97+ Query {
98+ Field (" message" , at : Resolver.message )
10099 }
101100 }
102- }
101+ )
103102```
104103
104+ Schemas may also be created in a modular way using ` SchemaBuilder ` :
105+
106+ ```
107+ let builder = SchemaBuilder(Resolver.self, Context.self)
108+ builder.add(
109+ Type(Message.self) {
110+ Field("content", at: \.content)
111+ }
112+ )
113+ builder.query.add(
114+ Field("message", at: Resolver.message)
115+ )
116+ let schema = try builder.build()
117+
118+ let api = MessageAPI(
119+ resolver: Resolver()
120+ schema: schema
121+ )
122+ ```
123+
105124⭐️ Notice that ` API ` allows dependency injection. You could pass mocks of ` resolver ` and ` context ` when testing, for example.
106125
107126#### Querying
108127
109- To query the schema we need to instantiate the api and pass in an EventLoopGroup to feed the execute function alongside the query itself.
128+ To query the schema we need to pass in a NIO EventLoopGroup to feed the execute function alongside the query itself.
110129
111130``` swift
112131import NIO
113132
114- let resolver = Resolver ()
115- let context = Context ()
116- let api = try MessageAPI (resolver : resolver)
117133let group = MultiThreadedEventLoopGroup (numberOfThreads : System.coreCount )
118-
119134defer {
120135 try ? group.syncShutdownGracefully ()
121136}
122137
123138api.execute (
124139 request : " { message { content } }" ,
125- context : context ,
140+ context : Context () ,
126141 on : group
127142).whenSuccess { result in
128143 print (result)
0 commit comments