You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
12
10
13
-
## Graphiti
11
+
Looking for help? Find resources [from the community](http://graphql.org/community/).
14
12
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 =tryGraphQLSchema(
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 =trygraphql(
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 =tryGraphQLSchema(
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 =trygraphqlSubscribe(
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
+
fortryawait 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
@@ -22,7 +124,9 @@ should be encoded using the `GraphQLJSONEncoder` provided by this package.
22
124
23
125
## Contributing
24
126
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:
26
130
27
131
### Swift
28
132
@@ -37,7 +141,7 @@ Most of this repo mirrors the structure of the canonical GraphQL implementation
37
141
38
142
This project is released under the MIT license. See [LICENSE](LICENSE) for details.
0 commit comments