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
Copy file name to clipboardExpand all lines: doc/migration.md
+116-2Lines changed: 116 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -27,8 +27,6 @@ Change your class declarations so that they no longer inherit from the generated
27
27
28
28
In cases where the return type has changed from `std::shared_ptr<service::Object>` to `std::shared_ptr<object::Interface>` or `std::shared_ptr<object::Union>`, wrap the concrete type in `std::make_shared<T>(...)` for the polymorphic type and return that.
29
29
30
-
If your implementation is tightly coupled with the object hierarchy from the schema, there are some strategies for separating them discussed in [#210](https://github.com/microsoft/cppgraphqlgen/issues/210).
31
-
32
30
## Simplify Field Accessor Signatures
33
31
34
32
Examine the generated object types and determine what the expected return type is from each field getter method. Replace the `service::FieldResult` wrapped type with the expected return type (possibly including the awaitable wrapper).
@@ -39,6 +37,122 @@ Parameters can be passed as a `const&` reference, a `&&` r-value reference, or b
39
37
40
38
Remove any unused `service::FieldParams` arguments. If your method does not take that as the first parameter, the generated template method will drop it and pass the rest of the expected arguments to your method.
41
39
40
+
## Decoupling Implementation Types from the Generated Types
41
+
42
+
If your implementation is tightly coupled with the object hierarchy from the schema, here's an example of how you might decouple them. Let's assume that you have a schema that looks something like this:
It's up to the you to make sure the `nodes` vector in this example only contains objects which actually implement the `Node` interface. If you want to do something more sophisticated like performing a lookup by `id`, you'd either need to request that before inserting an element and up-casting to `std::shared_ptr<service::Object>`, or you'd need to preserve the concrete type of each element, e.g. in a `std::variant` to be able to safely down-cast to the concrete type.
84
+
85
+
As of 4.x, the implementation might look more like this:
86
+
```c++
87
+
classNodeTypeImpl
88
+
{
89
+
public:
90
+
// Need to override this in the sub-classes to construct the correct sub-type wrappers.
This has several advantages over the previous version.
149
+
150
+
- You can declare your own inheritance heirarchy without any constraints inherited from `service::Object`, such as already inheriting from `std::enable_shared_from_this<service::Object>` and defininig `shared_from_this()` for that type.
151
+
- You can add your own common implementation for the interface methods you want, e.g. `NodeTypeImpl::getId`.
152
+
- Best of all, you no longer need to match an exact method signature to override the `object::NodeType*` accessors. For example, `NodeTypeImpl::getId` uses a more efficient return type, does not require a `service::FieldParams` argument (which is likely ignored anyway), and it can be `const` and `noexcept`. All of that together means you can use it as an internal accessor from any of these types as well as the field getter implementation.
153
+
154
+
The type erased implementation gives you a lot more control over your class hierarchy and makes it easier to use outside of the GraphQL service.
155
+
42
156
## CMake Changes
43
157
44
158
By default, earlier versions of `schemagen` would generate a single header and a single source file for the entire schema, including the declaration and definition of all of the object types. For any significantly complex schema, this source file could get very big. Even the `Today` sample schema was large enough to require a special `/bigobj` flag when compiling with `MSVC`. It also made incremental builds take much longer if you only added/removed/modified a few types, because the entire schema needed to be recompiled.
0 commit comments