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
16 changes: 13 additions & 3 deletions src/routes/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { createGqlResponseSchema, gqlResponseSchema } from './schemas.js';
import { graphql } from 'graphql';
import { createGqlResponseSchema, gqlResponseSchema, schema } from './schemas.js';
import { graphql, parse, validate } from 'graphql';
import depthLimit from 'graphql-depth-limit';

const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
const { prisma } = fastify;
Expand All @@ -15,7 +16,16 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
},
},
async handler(req) {
// return graphql();
const { query, variables } = req.body;
const errors = validate(schema, parse(query), [depthLimit(5)]);
return errors.length
? { errors }
: await graphql({
schema,
source: query,
variableValues: variables,
contextValue: { prisma },
});
},
});
};
Expand Down
226 changes: 226 additions & 0 deletions src/routes/graphql/mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLInputObjectType,
GraphQLInt,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
} from 'graphql';

import { Post, User, Profile, GraphQLMemberTypeId } from './query.js';
import { UUIDType } from './types/uuid.js';
import {
PostDto,
PostModel,
ProfileDto,
ProfileModel,
UserDto,
UserModel,
Context,
} from './types/models.js';

// Input Types
const CreatePostInput = new GraphQLInputObjectType({
name: 'CreatePostInput',
fields: {
title: { type: new GraphQLNonNull(GraphQLString) },
content: { type: new GraphQLNonNull(GraphQLString) },
authorId: { type: new GraphQLNonNull(UUIDType) },
},
});

const ChangePostInput = new GraphQLInputObjectType({
name: 'ChangePostInput',
fields: {
title: { type: GraphQLString },
content: { type: GraphQLString },
},
});

const CreateUserInput = new GraphQLInputObjectType({
name: 'CreateUserInput',
fields: {
name: { type: new GraphQLNonNull(GraphQLString) },
balance: { type: new GraphQLNonNull(GraphQLFloat) },
},
});

const ChangeUserInput = new GraphQLInputObjectType({
name: 'ChangeUserInput',
fields: {
name: { type: GraphQLString },
balance: { type: GraphQLFloat },
},
});

const CreateProfileInput = new GraphQLInputObjectType({
name: 'CreateProfileInput',
fields: {
isMale: { type: new GraphQLNonNull(GraphQLBoolean) },
yearOfBirth: { type: new GraphQLNonNull(GraphQLInt) },
memberTypeId: { type: new GraphQLNonNull(GraphQLMemberTypeId) },
userId: { type: new GraphQLNonNull(UUIDType) },
},
});

const ChangeProfileInput = new GraphQLInputObjectType({
name: 'ChangeProfileInput',
fields: {
isMale: { type: GraphQLBoolean },
yearOfBirth: { type: GraphQLInt },
memberTypeId: { type: GraphQLMemberTypeId },
},
});

// Mutations
export const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
createPost: {
type: Post,
args: { dto: { type: new GraphQLNonNull(CreatePostInput) } },
resolve: async (
root,
args: { dto: PostDto },
context: Context,
): Promise<PostModel> => await context.prisma.post.create({ data: args.dto }),
},
deletePost: {
type: new GraphQLNonNull(UUIDType),
args: { id: { type: new GraphQLNonNull(UUIDType) } },
resolve: async (root, args: { id: string }, context: Context) => {
await context.prisma.post.delete({ where: { id: args.id } });
return args.id;
},
},
changePost: {
type: Post,
args: {
id: { type: new GraphQLNonNull(UUIDType) },
dto: { type: new GraphQLNonNull(ChangePostInput) },
},
resolve: async (
root,
args: { id: string; dto: PostDto },
context: Context,
): Promise<PostModel> =>
await context.prisma.post.update({
where: { id: args.id },
data: args.dto,
}),
},

createUser: {
type: User,
args: { dto: { type: new GraphQLNonNull(CreateUserInput) } },
resolve: async (
root,
args: { dto: UserDto },
context: Context,
): Promise<UserModel> => await context.prisma.user.create({ data: args.dto }),
},
deleteUser: {
type: new GraphQLNonNull(UUIDType),
args: { id: { type: new GraphQLNonNull(UUIDType) } },
resolve: async (root, args: { id: string }, context: Context) => {
await context.prisma.user.delete({ where: { id: args.id } });
return args.id;
},
},
changeUser: {
type: User,
args: {
id: { type: new GraphQLNonNull(UUIDType) },
dto: { type: new GraphQLNonNull(ChangeUserInput) },
},
resolve: async (
root,
args: { id: string; dto: UserDto },
context: Context,
): Promise<UserModel> =>
await context.prisma.user.update({
where: { id: args.id },
data: args.dto,
}),
},

createProfile: {
type: Profile,
args: { dto: { type: new GraphQLNonNull(CreateProfileInput) } },
resolve: async (
root,
args: { dto: ProfileDto },
context: Context,
): Promise<ProfileModel> => await context.prisma.profile.create({ data: args.dto }),
},
deleteProfile: {
type: new GraphQLNonNull(UUIDType),
args: { id: { type: new GraphQLNonNull(UUIDType) } },
resolve: async (root, args: { id: string }, context: Context) => {
await context.prisma.profile.delete({ where: { id: args.id } });
return args.id;
},
},
changeProfile: {
type: Profile,
args: {
id: { type: new GraphQLNonNull(UUIDType) },
dto: { type: new GraphQLNonNull(ChangeProfileInput) },
},
resolve: async (
root,
args: { id: string; dto: ProfileDto },
context: Context,
): Promise<ProfileModel> =>
await context.prisma.profile.update({
where: { id: args.id },
data: args.dto,
}),
},

subscribeTo: {
type: new GraphQLNonNull(GraphQLString),
args: {
userId: { type: new GraphQLNonNull(UUIDType) },
authorId: { type: new GraphQLNonNull(UUIDType) },
},
resolve: async (
root,
args: { userId: string; authorId: string },
context: Context,
): Promise<string> => {
await context.prisma.subscribersOnAuthors.create({
data: {
subscriberId: args.userId,
authorId: args.authorId,
},
});

return args.authorId;
}
},
unsubscribeFrom: {
type: new GraphQLNonNull(GraphQLString),
args: {
userId: { type: new GraphQLNonNull(UUIDType) },
authorId: { type: new GraphQLNonNull(UUIDType) },
},
resolve: async (
root,
args: { userId: string; authorId: string },
context: Context,
): Promise<string> => {
await context.prisma.subscribersOnAuthors.delete({
where: {
subscriberId_authorId: {
subscriberId: args.userId,
authorId: args.authorId,
},
},
});
return args.userId;
},
},
},
});
Loading