|
| 1 | +# Migration |
| 2 | + |
| 3 | +## 2.0 to 3.0 |
| 4 | + |
| 5 | +### TypeReference removal |
| 6 | + |
| 7 | +The `GraphQLTypeReference` type was removed in v3.0.0, since it was made unnecessary by introducing closure-based `field` API that allows the package to better control the order of type resolution. |
| 8 | + |
| 9 | +To remove `GraphQLTypeReference`, you can typically just replace it with a reference to the `GraphQLObjectType` instance: |
| 10 | + |
| 11 | +```swift |
| 12 | +// Before |
| 13 | +let object1 = try GraphQLObjectType( |
| 14 | + name: "Object1" |
| 15 | +) |
| 16 | +let object2 = try GraphQLObjectType( |
| 17 | + name: "Object2" |
| 18 | + fields: ["object1": GraphQLField(type: GraphQLTypeReference("Object1"))] |
| 19 | +) |
| 20 | + |
| 21 | +// After |
| 22 | +let object1 = try GraphQLObjectType( |
| 23 | + name: "Object1" |
| 24 | +) |
| 25 | +let object2 = try GraphQLObjectType( |
| 26 | + name: "Object2" |
| 27 | + fields: ["object1": GraphQLField(type: object1)] |
| 28 | +) |
| 29 | +``` |
| 30 | + |
| 31 | +For more complex cyclic or recursive types, simply create the types first and assign the `fields` property afterward. Here's an example: |
| 32 | + |
| 33 | +```swift |
| 34 | +// Before |
| 35 | +let object1 = try GraphQLObjectType( |
| 36 | + name: "Object1" |
| 37 | + fields: ["object2": GraphQLField(type: GraphQLTypeReference("Object2"))] |
| 38 | +) |
| 39 | +let object2 = try GraphQLObjectType( |
| 40 | + name: "Object2" |
| 41 | + fields: ["object1": GraphQLField(type: GraphQLTypeReference("Object1"))] |
| 42 | +) |
| 43 | + |
| 44 | +// After |
| 45 | +let object1 = try GraphQLObjectType(name: "Object1") |
| 46 | +let object2 = try GraphQLObjectType(name: "Object2") |
| 47 | +object1.fields = { [weak object2] in |
| 48 | + guard let object2 = object2 else { return [:] } |
| 49 | + return ["object2": GraphQLField(type: object2)] |
| 50 | +} |
| 51 | +object2.fields = { [weak object1] in |
| 52 | + guard let object1 = object1 else { return [:] } |
| 53 | + return ["object1": GraphQLField(type: object1)] |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Note that this also gives you the chance to explicitly handle the memory cycle that cyclic types cause as well. |
| 58 | + |
| 59 | +### Type Definition Arrays |
| 60 | + |
| 61 | +The following type properties were changed from arrays to closures. To get the array version, in most cases you can just call the `get`-style function (i.e. for `GraphQLObject.fields`, use `GraphQLObject.getFields()`): |
| 62 | + |
| 63 | +- `GraphQLObjectType.fields` |
| 64 | +- `GraphQLObjectType.interfaces` |
| 65 | +- `GraphQLInterfaceType.fields` |
| 66 | +- `GraphQLInterfaceType.interfaces` |
| 67 | +- `GraphQLUnionType.types` |
| 68 | +- `GraphQLInputObjectType.fields` |
| 69 | + |
| 70 | +### Directive description is optional |
| 71 | + |
| 72 | +`GraphQLDirective` has changed from a struct to a class, and its `description` property is now optional. |
| 73 | + |
| 74 | +### GraphQL type codability |
| 75 | + |
| 76 | +With GraphQL type definitions now including closures, many of the objects in [Definition](https://github.com/GraphQLSwift/GraphQL/blob/main/Sources/GraphQL/Type/Definition.swift) are no longer codable. If you are depending on codability, you can conform the type appropriately in your downstream package. |
0 commit comments