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
docs: Document how to reuse Type of embedded object schema
Default behavior for embedded documents is to give the type a name derived from the tree-path. This
shows how to force a name for that generated embedded type.
Copy file name to clipboardExpand all lines: README.md
+65Lines changed: 65 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -157,7 +157,72 @@ UserTC.addRelation(
157
157
})
158
158
);
159
159
```
160
+
### Reusing the same mongoose Schame in embedded object fields
161
+
Suppose you have a common structure you use as embedded object in multiple Schemas.
162
+
Also suppose you want the strcutre to have the same GraphQL type across all parent types.
163
+
(For instance, to allow reuse of fragments for this type)
164
+
Here are Schemas to demonstrate:
165
+
```js
166
+
import { Schema } from'mongoose';
167
+
168
+
constImageDataStructure=Schema({
169
+
url:String,
170
+
dimensions : {
171
+
width:Number,
172
+
height:Number
173
+
}
174
+
}, { _id:false });
175
+
176
+
constUserProfile=Schema({
177
+
fullName:String,
178
+
personalImage: ImageDataStructure
179
+
});
180
+
181
+
constArticle=Schema({
182
+
title:String,
183
+
heroImage: ImageDataStructure
184
+
});
185
+
```
186
+
If you want the `ImageDataStructure` to use the same GraphQL type in both `Article` and `UserProfile` you will need to explicitly tell `graphql-compose-mongoose` that.
0 commit comments