Skip to content

Commit 2a3df43

Browse files
authored
[MFSNW26] Add boilerplate GraphQL structure (#20)
* added boilerplate graphql-related files * lint fix * downgrade express
1 parent 2ecfcd6 commit 2a3df43

File tree

9 files changed

+1552
-262
lines changed

9 files changed

+1552
-262
lines changed

backend/graphql/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { gql } from 'apollo-server-express';
2+
import { makeExecutableSchema } from '@graphql-tools/schema';
3+
import { merge } from 'lodash';
4+
5+
import sampleResolvers from './resolvers/sampleResolvers';
6+
import sampleType from './types/sampleType';
7+
8+
const query = gql`
9+
type Query {
10+
_empty: String
11+
}
12+
`;
13+
14+
const mutation = gql`
15+
type Mutation {
16+
_empty: String
17+
}
18+
`;
19+
20+
const executableSchema = makeExecutableSchema({
21+
typeDefs: [query, mutation, sampleType],
22+
resolvers: merge(sampleResolvers),
23+
});
24+
25+
export default executableSchema;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import SampleService from '../../services/implementations/sampleService';
2+
import ISampleService from '../../services/interfaces/sampleService';
3+
4+
const sampleService: ISampleService = new SampleService();
5+
6+
const sampleResolvers = {
7+
Query: {
8+
sampleById: async (_: unknown, { id }: { id: string }) => {
9+
return sampleService.getSampleById(id);
10+
},
11+
samples: async () => {
12+
return sampleService.getAllSamples();
13+
},
14+
},
15+
Mutation: {
16+
createSample: async (
17+
_: unknown,
18+
{ sample }: { sample: { name: string; description: string } }
19+
) => {
20+
return sampleService.createSample(sample);
21+
},
22+
updateSample: async (
23+
_: unknown,
24+
{ id, sample }: { id: string; sample: { name: string; description: string } }
25+
) => {
26+
return sampleService.updateSample(id, sample);
27+
},
28+
deleteSampleById: async (_: unknown, { id }: { id: string }) => {
29+
return sampleService.deleteSampleById(id);
30+
},
31+
},
32+
};
33+
34+
export default sampleResolvers;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { gql } from 'apollo-server-express';
2+
3+
const sampleType = gql`
4+
type SampleDTO {
5+
id: String!
6+
name: String!
7+
description: String!
8+
createdAt: String!
9+
updatedAt: String!
10+
}
11+
12+
input CreateSampleDTO {
13+
name: String!
14+
description: String!
15+
}
16+
17+
extend type Query {
18+
sampleById(id: String!): SampleDTO!
19+
samples: [SampleDTO!]!
20+
}
21+
22+
extend type Mutation {
23+
createSample(sample: CreateSampleDTO!): SampleDTO!
24+
updateSample(id: String!, sample: CreateSampleDTO!): SampleDTO!
25+
deleteSampleById(id: String!): SampleDTO!
26+
}
27+
`;
28+
29+
export default sampleType;

backend/models/sample.model.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Column, DataType, Model, Table } from 'sequelize-typescript';
2+
3+
@Table({ tableName: 'samples' })
4+
export default class Sample extends Model {
5+
@Column({ type: DataType.STRING, primaryKey: true })
6+
id!: string;
7+
8+
@Column({ type: DataType.STRING })
9+
name!: string;
10+
11+
@Column({ type: DataType.STRING })
12+
description!: string;
13+
14+
@Column({ type: DataType.DATE })
15+
createdAt!: Date;
16+
17+
@Column({ type: DataType.DATE })
18+
updatedAt!: Date;
19+
}

0 commit comments

Comments
 (0)