graphql-python / graphql-python/graphene-mongo

What is intended way to implement Union?

Offen
#117 3 Kommentare 0 Reaktionen 1 zugewiesene Person Beansprucht von @abawchen Auf GitHub ansehen
good first issue wait for response
Vorherrschende Sprache
Python
Sterne
287
Forks
111
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Here is the schema that i am trying to implement:
```gql
type Text {
id: ID
content: String
}
type Image {
id: ID
image_url: String
thumb_url: String
}
union Post = Text | Image
type User {
id: ID
posts: [Post]
}
type Query {
users: [User]
}
```
and execute this query:
```gql
{
users {
posts {
... on Image {
image_url
}
... on Text {
content
}
}
}
}
```

What I have tried:
1. _Mongoengine models_: make PostModel as separate Document, inherit TextModel and ImageModel from it and reference PostModel from UserModel.
_Graphene objects_: make types for Text, Image, User and Post as MongoengineObjectType.
(full code [here](https://github.com/graphql-python/graphene-mongo/files/3951781/option1.py.txt))
This returns errors `Unknown type "Image".` and `Unknown type "Text".`
In this case Post type has only fields `Cls: String, id: ID`.

2. _Mongoengine models_: same.
_Graphene objects_: inherit Post from graphene.Union and set Image and Text as types of that union.
(full code [here](https://github.com/graphql-python/graphene-mongo/files/3951811/option2.py.txt))
This returns `Cannot query field "posts" on type "User".`.
I suppose this happens because now graphene_mongo does not state that `User.posts` reference `Post`

So how should such schema be implemented?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.