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

WIP: Syncing cli with server #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion backend/codegen.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
schema: '**.gql'
hooks:
afterAllFileWrite:
- prettier --write
- npx prettier --write
generates:
./src/graphql/resolvers-types.ts:
config:
Expand Down
15 changes: 15 additions & 0 deletions backend/prisma/migrations/20201231122158_init/migration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ CREATE TABLE "Project" (
PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "TimeRecord" (
"id" SERIAL,
"date" TEXT NOT NULL,
"start" TEXT NOT NULL,
"end" TEXT NOT NULL,
"breakDuration" INTEGER NOT NULL,
"userId" INTEGER NOT NULL,

PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL,
Expand All @@ -25,3 +37,6 @@ CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");

-- AddForeignKey
ALTER TABLE "Project" ADD FOREIGN KEY("userId")REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TimeRecord" ADD FOREIGN KEY("userId")REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
21 changes: 16 additions & 5 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ model Project {
userId Int @unique
}

model TimeRecord {
id Int @id @default(autoincrement())
date String
start String
end String
breakDuration Int
user User @relation(fields: [userId], references: [id])
userId Int
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
password String?
projects Project?
id Int @id @default(autoincrement())
email String @unique
name String?
password String?
projects Project?
timeRecords TimeRecord[]
}
88 changes: 71 additions & 17 deletions backend/src/graphql/resolvers-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,56 @@ export type Project = {
export type Query = {
__typename?: 'Query';
projects?: Maybe<Array<Maybe<Project>>>;
timeRecords?: Maybe<Array<Maybe<TimeRecord>>>;
user?: Maybe<User>;
users?: Maybe<Array<Maybe<User>>>;
};

export type UserInput = {
email: Scalars['String'];
name?: Maybe<Scalars['String']>;
password?: Maybe<Scalars['String']>;
export type TimeRecordInput = {
date: Scalars['String'];
start: Scalars['String'];
end: Scalars['String'];
breakDuration: Scalars['Int'];
};

export type User = {
__typename?: 'User';
export type TimeRecord = {
__typename?: 'TimeRecord';
id?: Maybe<Scalars['Int']>;
email?: Maybe<Scalars['String']>;
name?: Maybe<Scalars['String']>;
date?: Maybe<Scalars['String']>;
start?: Maybe<Scalars['String']>;
end?: Maybe<Scalars['String']>;
breakDuration?: Maybe<Scalars['Int']>;
user?: Maybe<User>;
userId?: Maybe<Scalars['Int']>;
};

export type Mutation = {
__typename?: 'Mutation';
addTimeRecords?: Maybe<Scalars['Boolean']>;
createUser?: Maybe<User>;
};

export type MutationAddTimeRecordsArgs = {
timeRecords: Array<TimeRecordInput>;
};

export type MutationCreateUserArgs = {
user: UserInput;
};

export type UserInput = {
email: Scalars['String'];
name?: Maybe<Scalars['String']>;
password?: Maybe<Scalars['String']>;
};

export type User = {
__typename?: 'User';
id?: Maybe<Scalars['Int']>;
email?: Maybe<Scalars['String']>;
name?: Maybe<Scalars['String']>;
};

export type WithIndex<TObject> = TObject & Record<string, any>;
export type ResolversObject<TObject> = WithIndex<TObject>;

Expand Down Expand Up @@ -158,10 +182,12 @@ export type ResolversTypes = ResolversObject<{
Int: ResolverTypeWrapper<Scalars['Int']>;
String: ResolverTypeWrapper<Scalars['String']>;
Query: ResolverTypeWrapper<{}>;
UserInput: UserInput;
User: ResolverTypeWrapper<User>;
TimeRecordInput: TimeRecordInput;
TimeRecord: ResolverTypeWrapper<TimeRecord>;
Mutation: ResolverTypeWrapper<{}>;
Boolean: ResolverTypeWrapper<Scalars['Boolean']>;
UserInput: UserInput;
User: ResolverTypeWrapper<User>;
}>;

/** Mapping between all available schema types and the resolvers parents */
Expand All @@ -170,10 +196,12 @@ export type ResolversParentTypes = ResolversObject<{
Int: Scalars['Int'];
String: Scalars['String'];
Query: {};
UserInput: UserInput;
User: User;
TimeRecordInput: TimeRecordInput;
TimeRecord: TimeRecord;
Mutation: {};
Boolean: Scalars['Boolean'];
UserInput: UserInput;
User: User;
}>;

export type IsAuthenticatedDirectiveArgs = {};
Expand Down Expand Up @@ -213,24 +241,39 @@ export type QueryResolvers<
ParentType,
ContextType
>;
timeRecords?: Resolver<
Maybe<Array<Maybe<ResolversTypes['TimeRecord']>>>,
ParentType,
ContextType
>;
user?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>;
users?: Resolver<Maybe<Array<Maybe<ResolversTypes['User']>>>, ParentType, ContextType>;
}>;

export type UserResolvers<
export type TimeRecordResolvers<
ContextType = ApolloContext,
ParentType extends ResolversParentTypes['User'] = ResolversParentTypes['User']
ParentType extends ResolversParentTypes['TimeRecord'] = ResolversParentTypes['TimeRecord']
> = ResolversObject<{
id?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
email?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
name?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
date?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
start?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
end?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
breakDuration?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
user?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>;
userId?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type MutationResolvers<
ContextType = ApolloContext,
ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']
> = ResolversObject<{
addTimeRecords?: Resolver<
Maybe<ResolversTypes['Boolean']>,
ParentType,
ContextType,
RequireFields<MutationAddTimeRecordsArgs, 'timeRecords'>
>;
createUser?: Resolver<
Maybe<ResolversTypes['User']>,
ParentType,
Expand All @@ -239,11 +282,22 @@ export type MutationResolvers<
>;
}>;

export type UserResolvers<
ContextType = ApolloContext,
ParentType extends ResolversParentTypes['User'] = ResolversParentTypes['User']
> = ResolversObject<{
id?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
email?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
name?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type Resolvers<ContextType = ApolloContext> = ResolversObject<{
Project?: ProjectResolvers<ContextType>;
Query?: QueryResolvers<ContextType>;
User?: UserResolvers<ContextType>;
TimeRecord?: TimeRecordResolvers<ContextType>;
Mutation?: MutationResolvers<ContextType>;
User?: UserResolvers<ContextType>;
}>;

/**
Expand Down
4 changes: 2 additions & 2 deletions backend/src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mergeResolvers } from '@graphql-tools/merge';
import userResolver from '../user/user.resolver';
import projectResolver from '../project/project.resolver';
import timeRecordResolver from '../timeRecord/timeRecord.resolver';

export default mergeResolvers([userResolver, projectResolver]);
export default mergeResolvers([userResolver, timeRecordResolver]);
3 changes: 3 additions & 0 deletions backend/src/server/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const startExpress = (): void => {
}),
);

// server health check
app.get('/server-health', (req, res) => res.sendStatus(200));

// attach apollo server to express
getApolloServer(apolloContext).applyMiddleware({ app });

Expand Down
30 changes: 30 additions & 0 deletions backend/src/timeRecord/addTimeRecords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { MutationResolvers } from 'src/graphql/resolvers-types';

export const addTimeRecords: MutationResolvers['addTimeRecords'] = async (
parent,
args,
{ prisma },
) => {
const inserts = args.timeRecords.map((record) => {
const data = {
date: record.date,
start: record.start,
end: record.end,
breakDuration: record.breakDuration,
user: {
connect: {
id: 1,
},
},
};

return prisma.timeRecord.create({ data });
});
try {
await Promise.all(inserts);
return true;
} catch (error) {
console.log('There was an error while adding time records to db', error);
return false;
}
};
9 changes: 9 additions & 0 deletions backend/src/timeRecord/getTimeRecords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { QueryResolvers } from 'src/graphql/resolvers-types';

export const getTimeRecords: QueryResolvers['timeRecords'] = (
parent,
args,
{ prisma },
) => {
return prisma.timeRecord.findMany({ where: { userId: 1 } });
};
24 changes: 24 additions & 0 deletions backend/src/timeRecord/timeRecord.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
input TimeRecordInput {
date: String!
start: String!
end: String!
breakDuration: Int!
}

type TimeRecord {
id: Int
date: String
start: String
end: String
breakDuration: Int
user: User
userId: Int
}

type Query {
timeRecords: [TimeRecord]
}

type Mutation {
addTimeRecords(timeRecords: [TimeRecordInput!]!): Boolean
}
14 changes: 14 additions & 0 deletions backend/src/timeRecord/timeRecord.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Resolvers } from '../graphql/resolvers-types';
import { getTimeRecords } from './getTimeRecords';
import { addTimeRecords } from './addTimeRecords';

const resolver: Resolvers = {
Query: {
timeRecords: getTimeRecords,
},
Mutation: {
addTimeRecords,
},
};

export default resolver;
Loading