1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import * as express from 'express';
import * as bodyParser from 'body-parser';

import { Field, ObjectDefinition, ObjectImplementation, Resolver, Arg, Schema, BaseSchema } from 'aerographql';

/**
* Fake Database objects
*/
let users: User[] = [
{ admin: false, age: 25, description: 'Description of Bob', name: 'Bob', id: '0' },
{ admin: true, age: 36, description: 'Description of Alice', name: 'Alice', id: '1' },
{ admin: false, age: 28, description: 'Decription of Steeve', name: 'Steeve', id: '3' }
];

let todos: { [ key: string ]: Todo[] } =
{
Bob: [
{ id: '0', title: 'Todo1', content: 'Bob Todo1 content' },
{ id: '1', title: 'Todo2', content: 'Bob Todo2 content' },
{ id: '2', title: 'Todo3', content: 'Bob Todo3 content' }
],
Alice: [
{ id: '3', title: 'Todo1', content: 'Alice Todo1 content' },
{ id: '4', title: 'Todo2', content: 'Alice Todo2 content' },
{ id: '5', title: 'Todo3', content: 'Alice Todo3 content' } ],
Steeve: [
{ id: '6', title: 'Todo1', content: 'Steeve Todo1 content' },
{ id: '7', title: 'Todo2', content: 'Steeve Todo2 content' },
{ id: '8', title: 'Todo3', content: 'Steeve Todo3 content' } ]
};

/**
* Schema definitions
*/
@ObjectDefinition( {
name: 'User'
} )
export class User {
@Field( { type: 'ID' } ) id: string;
@Field() name: string = "";
@Field() description: string = "Empty description";
@Field() age: number = 0;
@Field() admin: boolean = false;
}

@ObjectDefinition( {
name: 'Todo'
} )
export class Todo {
@Field( { type: 'ID' } ) id: string;
@Field() title: string = "";
@Field() content: string = "Empty todo";
}

@ObjectImplementation( {
name: 'User'
} )
export class UserImpl {

@Resolver( { type: Todo, list: true} )
todos( user: User, @Arg( { nullable: true } ) search: string ) {
return todos[user.name];
}
}

@ObjectImplementation( {
name: 'RootQuery'
} )
export class RootQuery {

@Resolver( { type: User } )
user( @Arg() name: string ): User | Promise<User> {
return users.find( u => u.name === name );
}
}

@Schema( {
rootQuery: 'RootQuery',
components: [ RootQuery, User, UserImpl, Todo ]
} )
export class MySchema extends BaseSchema {
}

/**
* Actual server code
*/
let mySchema = new MySchema();
this.app = express();
this.app.use( '/graphql', bodyParser.json(), graphqlExpress( { schema: mySchema.graphQLSchema } ) );
this.app.use( '/graphiql', graphiqlExpress( { endpointURL: '/graphql' } ) );
this.app.listen( 3000, () => {
console.log( 'Up and running !' );
} );