From 4c374caeb7b9e35fbb9aee57400891637d8401d4 Mon Sep 17 00:00:00 2001 From: Yoad Snapir Date: Sun, 26 Mar 2017 16:58:46 +0300 Subject: [PATCH 1/2] 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. --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/README.md b/README.md index 9b188361..21b47f68 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,72 @@ UserTC.addRelation( }) ); ``` +### Reusing the same mongoose Schame in embedded object fields +Suppose you have a common structure you use as embedded object in multiple Schemas. +Also suppose you want the strcutre to have the same GraphQL type across all parent types. +(For instance, to allow reuse of fragments for this type) +Here are Schemas to demonstrate: +```js +import { Schema } from 'mongoose'; + +const ImageDataStructure = Schema({ + url: String, + dimensions : { + width: Number, + height: Number + } +}, { _id: false }); + +const UserProfile = Schema({ + fullName: String, + personalImage: ImageDataStructure +}); + +const Article = Schema({ + title: String, + heroImage: ImageDataStructure +}); +``` +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. +Do the following: +```js +import { convertSchemaToGraphQL } from 'graphql-compose-mongoose'; +convertSchemaToGraphQL(ImageDataStructure, 'EmbeddedImage'); // Force this type on this mongoose schema +``` +Before continuing to convert your models to TypeComposers: +```js +import mongoose from 'mongoose'; +import { composeWithMongoose } from 'graphql-compose-mongoose'; +const UserProfileModel = mongoose.model('UserProfile', UserProfile); +const ArticleModel = mongoose.model('Article', Article); + +const UserProfileTC = composeWithMongoose(UserProfileModel); +const ArticleTC = composeWithMongoose(ArticleModel); +``` +Then, you can use queries like this: +```graphql +query { + topUser { + fullName + personalImage { + ...fullImageData + } + } + topArticle { + title + heroImage { + ...fullImageData + } + } +} +fragment fullImageData on EmbeddedImage { + url + dimensions { + width height + } +} +``` Customization options ===================== From c632d57c680da44833946c2e8e71cb8cb1014b61 Mon Sep 17 00:00:00 2001 From: Yoad Snapir Date: Sun, 26 Mar 2017 17:03:02 +0300 Subject: [PATCH 2/2] docs: Improve clarity of docs about embedded type names Small fix to the doc clarity --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 21b47f68..77ccd3b8 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,8 @@ const Article = Schema({ heroImage: ImageDataStructure }); ``` -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. +If you want the `ImageDataStructure` to use the same GraphQL type in both `Article` and `UserProfile` you will need create it as a mongoose schema (not a standard javascript object) and to explicitly tell `graphql-compose-mongoose` the name you want it to have. Otherwise, without the name, it would generate the name according to the first parent this type was embedded in. + Do the following: ```js import { convertSchemaToGraphQL } from 'graphql-compose-mongoose';