Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.example

This file was deleted.

65 changes: 65 additions & 0 deletions src/routes/graphql/dataloaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import DataLoader from 'dataloader';
import { PrismaClient } from '@prisma/client';

export function createContext(prisma: PrismaClient) {
return {
postLoader: new DataLoader(async (userIds) => {
const posts = await prisma.post.findMany({
where: {
authorId: { in: userIds as string[] },
},
});
return userIds.map((id) => posts.filter((post) => post.authorId === id));
}),

profileLoader: new DataLoader(async (userIds) => {
const profiles = await prisma.profile.findMany({
where: {
userId: { in: userIds as string[] },
},
});
return userIds.map((id) => profiles.find((profile) => profile.userId === id));
}),

memberTypeLoader: new DataLoader(async (memberTypeIds) => {
const memberTypes = await prisma.memberType.findMany({
where: {
id: { in: memberTypeIds as string[] },
},
});
return memberTypeIds.map((id) =>
memberTypes.find((memberType) => memberType.id === id),
);
}),

userSubscribedToLoader: new DataLoader(async (userIds) => {
const userSubscribed = await prisma.subscribersOnAuthors.findMany({
where: {
subscriberId: { in: userIds as string[] },
},
include: {
author: true,
},
});
return userIds.map((id) =>
userSubscribed
.filter((user) => user.subscriberId === id)
.map((user) => user.author),
);
}),

subscribedToUserLoader: new DataLoader(async (userIds) => {
const subscribers = await prisma.subscribersOnAuthors.findMany({
where: {
authorId: { in: userIds as string[] },
},
include: {
subscriber: true,
},
});
return userIds.map((id) =>
subscribers.filter((user) => user.authorId === id).map((user) => user.subscriber),
);
}),
};
}
19 changes: 16 additions & 3 deletions src/routes/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { createGqlResponseSchema, gqlResponseSchema } from './schemas.js';
import { graphql } from 'graphql';
import { createGqlResponseSchema, gqlResponseSchema, gqlSchema } from './schemas.js';
import { graphql, parse, validate } from 'graphql';
import depthLimit from 'graphql-depth-limit';
import { createContext } from './dataloaders.js';

const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
const { prisma } = fastify;
Expand All @@ -15,7 +17,18 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
},
},
async handler(req) {
// return graphql();
const errors = validate(gqlSchema, parse(req.body.query), [depthLimit(5)]);
if (errors.length) return { errors };

return graphql({
schema: gqlSchema,
source: req.body.query,
variableValues: req.body.variables,
contextValue: {
prisma,
loaders: createContext(prisma),
},
});
},
});
};
Expand Down
186 changes: 186 additions & 0 deletions src/routes/graphql/mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { Prisma } from '@prisma/client';
import { GraphQLNonNull, GraphQLObjectType, GraphQLString } from 'graphql';
import { postType, createPostInput, changePostInput } from './types/postType.js';
import {
profileType,
createProfileInput,
changeProfileInput,
} from './types/profileType.js';
import { userType, createUserInput, changeUserInput } from './types/userType.js';
import { UUIDType } from './types/uuid.js';
import { Context } from './types/model.js';

export const mutationType = new GraphQLObjectType({
name: 'Mutations',
fields: {
createUser: {
type: new GraphQLNonNull(userType),
args: {
dto: { type: new GraphQLNonNull(createUserInput) },
},
resolve: async (_, { dto }, context: Context) => {
return context.prisma.user.create({
data: dto as Prisma.UserCreateInput,
});
},
},
createProfile: {
type: new GraphQLNonNull(profileType),
args: {
dto: { type: new GraphQLNonNull(createProfileInput) },
},
resolve: async (_, { dto }, context: Context) => {
return context.prisma.profile.create({
data: dto as Prisma.ProfileCreateInput,
});
},
},
createPost: {
type: new GraphQLNonNull(postType),
args: {
dto: { type: new GraphQLNonNull(createPostInput) },
},
resolve: async (_, { dto }, context: Context) => {
return context.prisma.post.create({
data: dto as Prisma.PostCreateInput,
});
},
},
changePost: {
type: new GraphQLNonNull(postType),
args: {
id: { type: new GraphQLNonNull(UUIDType) },
dto: { type: new GraphQLNonNull(changePostInput) },
},
resolve: (
_,
{ id, dto }: { id: string; dto: Prisma.PostUpdateInput },
context: Context,
) => {
return context.prisma.post.update({
where: { id },
data: dto,
});
},
},
changeProfile: {
type: new GraphQLNonNull(profileType),
args: {
id: { type: new GraphQLNonNull(UUIDType) },
dto: { type: new GraphQLNonNull(changeProfileInput) },
},
resolve: async (
_,
{ id, dto }: { id: string; dto: Prisma.ProfileUpdateInput },
context: Context,
) => {
return context.prisma.profile.update({
where: { id },
data: dto,
});
},
},
changeUser: {
type: new GraphQLNonNull(userType),
args: {
id: { type: new GraphQLNonNull(UUIDType) },
dto: { type: new GraphQLNonNull(changeUserInput) },
},
resolve: async (
_,
{ id, dto }: { id: string; dto: Prisma.UserUpdateInput },
context: Context,
) => {
return context.prisma.user.update({
where: { id },
data: dto,
});
},
},
deleteUser: {
type: new GraphQLNonNull(GraphQLString),
args: {
id: { type: new GraphQLNonNull(UUIDType) },
},
resolve: async (_, { id }: { id: string }, context: Context) => {
return (
await context.prisma.user.delete({
where: { id },
})
).id;
},
},
deletePost: {
type: new GraphQLNonNull(GraphQLString),
args: {
id: { type: new GraphQLNonNull(UUIDType) },
},
resolve: async (_, { id }: { id: string }, context: Context) => {
return (
await context.prisma.post.delete({
where: { id },
})
).id;
},
},
deleteProfile: {
type: new GraphQLNonNull(GraphQLString),
args: {
id: { type: new GraphQLNonNull(UUIDType) },
},
resolve: async (_, { id }: { id: string }, context: Context) => {
return (
await context.prisma.profile.delete({
where: { id },
})
).id;
},
},
subscribeTo: {
type: new GraphQLNonNull(GraphQLString),
args: {
userId: { type: new GraphQLNonNull(UUIDType) },
authorId: { type: new GraphQLNonNull(UUIDType) },
},
resolve: async (
_,
{ userId, authorId }: { userId: string; authorId: string },
context: Context,
) => {
return (
await context.prisma.subscribersOnAuthors.create({
data: {
subscriberId: userId,
authorId,
},
})
).subscriberId;
},
},
unsubscribeFrom: {
type: new GraphQLNonNull(GraphQLString),
args: {
userId: { type: new GraphQLNonNull(UUIDType) },
authorId: { type: new GraphQLNonNull(UUIDType) },
},
resolve: async (
_,
{ userId, authorId }: { userId: string; authorId: string },
context: Context,
) => {
return (
await context.prisma.subscribersOnAuthors.delete({
where: {
subscriberId: userId,
authorId: authorId,
subscriberId_authorId: {
subscriberId: userId,
authorId,
},
},
})
).authorId;
},
},
},
});
Loading