Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does graphql-to-mongodb handle relationships? #44

Closed
thardy opened this issue Mar 31, 2019 · 1 comment
Closed

Does graphql-to-mongodb handle relationships? #44

thardy opened this issue Mar 31, 2019 · 1 comment

Comments

@thardy
Copy link

thardy commented Mar 31, 2019

Before I dig too deep, I was wondering if this library supports simple relationships, like a book with an authorId - can I pull an author object back on a book query?

Also, is there a better place to ask questions like this that aren't issues? I couldn't find anything on Stackoverflow. Do you guys have a gitter room, slack room, or forum?

@yoavkarako
Copy link
Collaborator

The library does not provide any way to describe relationships, nor will it make any guesswork to try and retrieve the related objects for you. However, it does not impede you from extending your type with a field to perform such a query.

A naive implementation of a book type that could be added to the express example to play with:

const BookType = new GraphQLObjectType({
    name: 'BookType',
    fields: () => ({
        name: { type: GraphQLString },
        pages: { type: GraphQLInt },
        authorId: { type: GraphQLID },
        author: { 
            type: PersonType,
            resolve: async (obj, args, { db }: { db: Db }, info) => {
                if (!obj.authorId) return null;
                const projection = getMongoDbProjection(info, PersonType);
                return await db.collection("people").findOne({ _id: new ObjectId(obj.authorId) }, { projection })
            },
            dependencies: ['authorId']
        }
    })
});

Note that you can still use the library to get an exact projection. I imagine a more mature solution will use a batching solution to ensure minimal DB queries.

There isn't a place to ask these kinds of questions at present besides here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants