snowballtools-base/packages/backend/src/resolvers.ts
prathamesh0 1f192444c4 Implement GQL resolver to get organizations data (#20)
* Add resolver method for getting organizations

* Fetch project data along with organizations from db

* Refactor code to map db project entity to graphql type

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
2024-02-01 11:37:57 +05:30

36 lines
1020 B
TypeScript

import { Database } from './database';
import { projectToGqlType } from './utils';
export const createResolvers = async (db: Database): Promise<any> => {
return {
Query: {
// TODO: add custom type for context
user: (_: any, __: any, context: any) => {
return db.getUser(context.userId);
},
organizations: async (_:any, __: any, context: any) => {
const organizations = await db.getOrganizationsbyUserId(context.userId);
const orgsWithProjectsPromises = organizations.map(async (org) => {
const dbProjects = await db.getProjectsbyOrganizationId(org.id);
const projects = dbProjects.map(dbProject => {
return projectToGqlType(dbProject);
});
return {
...org,
projects
};
});
const orgsWithProjects = await Promise.all(orgsWithProjectsPromises);
// TODO: Populate members field when / if required
return orgsWithProjects;
}
}
};
};