Skip to content

Commit c279a4a

Browse files
committed
feat: add type-graphql for declaring GraphQL schema
1 parent 9a4c3d1 commit c279a4a

File tree

8 files changed

+305
-81
lines changed

8 files changed

+305
-81
lines changed

package-lock.json

Lines changed: 228 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
"start": "node dist/index.js",
99
"start:dev": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules --respawn src/index.ts",
1010
"test": "jest --runInBand",
11-
"test:dev": "node --inspect ./node_modules/jest/bin/jest.js --runInBand"
11+
"test:dev": "node --inspect ./node_modules/jest/bin/jest.js --runInBand"
1212
},
1313
"author": "Samuel Caçador",
1414
"license": "MIT",
1515
"dependencies": {
1616
"apollo-server": "^3.6.3",
1717
"apollo-server-express": "^3.6.3",
18+
"class-validator": "^0.13.2",
1819
"dotenv": "^16.0.0",
1920
"express": "^4.17.3",
20-
"graphql": "^16.3.0",
21-
"graphql-playground-middleware-express": "^1.7.23"
21+
"graphql": "^15.8.0",
22+
"graphql-playground-middleware-express": "^1.7.23",
23+
"reflect-metadata": "^0.1.13",
24+
"type-graphql": "^1.1.1"
2225
},
2326
"devDependencies": {
2427
"@types/express": "^4.17.13",

src/entities/Recipe.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ObjectType, Field, ID } from 'type-graphql';
2+
3+
@ObjectType()
4+
export class Recipe {
5+
@Field(type => ID)
6+
id: string;
7+
8+
@Field()
9+
title: string;
10+
11+
@Field({ nullable: true })
12+
description?: string;
13+
}

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import 'reflect-metadata';
12
import 'dotenv/config';
23
import express from 'express';
34
import router from './router';
4-
import { apolloServer } from './services/ApolloServices/ApolloServerService';
5+
import { createApolloServer } from './services/ApolloServices/ApolloServerService';
56

67
const app = express();
78

8-
apolloServer
9-
.start()
10-
.then(() => {
9+
createApolloServer()
10+
.then(async (apolloServer: { start: () => void; getMiddleware: () => any }) => {
11+
await apolloServer.start();
1112
app.use(express.json());
1213
app.use(apolloServer.getMiddleware());
1314
app.use(router);

src/resolvers/RecipeResolver.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Resolver, Query } from 'type-graphql';
2+
import { Recipe } from '../entities/Recipe';
3+
4+
@Resolver(Recipe)
5+
export class RecipeResolver {
6+
@Query(() => [Recipe])
7+
recipes() {
8+
return [
9+
{
10+
id: '1',
11+
title: '123',
12+
description: ' lol',
13+
},
14+
];
15+
}
16+
}
Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
import { ApolloServer, gql } from 'apollo-server-express';
2-
3-
// The GraphQL schema
4-
const typeDefs = gql`
5-
type Query {
6-
"A simple type for getting started!"
7-
hello: String
8-
}
9-
`;
10-
11-
// A map of functions which return data for the schema.
12-
const resolvers = {
13-
Query: {
14-
hello: () => 'world',
15-
},
16-
};
17-
18-
const apolloServer = new ApolloServer({
19-
typeDefs,
20-
resolvers,
21-
});
22-
23-
export { apolloServer };
1+
import { ApolloServer } from 'apollo-server-express';
2+
import { buildSchema } from 'type-graphql';
3+
import { RecipeResolver } from '../../resolvers/RecipeResolver';
4+
5+
async function createApolloServer(): Promise<ApolloServer> {
6+
const schema = await buildSchema({
7+
resolvers: [RecipeResolver],
8+
});
9+
return new ApolloServer({
10+
schema: schema,
11+
});
12+
}
13+
14+
export { createApolloServer };
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
import { apolloServer } from '../../../src/services/ApolloServices/ApolloServerService';
1+
import 'reflect-metadata';
2+
import { createApolloServer } from '../../../src/services/ApolloServices/ApolloServerService';
23

34
describe('ApolloServiceService', () => {
4-
it('hello resolver', async () => {
5-
const query = `{ hello }`;
5+
it('recipe resolver', async () => {
6+
const apolloServer = await createApolloServer();
7+
const query = `{
8+
recipes {
9+
id
10+
title
11+
description
12+
}
13+
}`;
614

715
const result = await apolloServer.executeOperation({
816
query,
917
});
1018

1119
expect(result.data).toMatchObject({
12-
hello: 'world',
20+
recipes: [
21+
{
22+
id: '1',
23+
title: '123',
24+
description: ' lol',
25+
},
26+
],
1327
});
1428
});
1529
});

tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
"rootDir": "./src",
66
"allowJs": false,
77
"outDir": "./dist",
8+
"lib": ["ES2020", "esnext.asynciterable"],
89
"esModuleInterop": true,
910
"forceConsistentCasingInFileNames": true,
1011
"strict": true,
11-
"skipLibCheck": true
12+
"skipLibCheck": true,
13+
"emitDecoratorMetadata": true,
14+
"experimentalDecorators": true,
15+
"strictPropertyInitialization": false
1216
},
1317
"include": [
1418
"./src/**/*"

0 commit comments

Comments
 (0)