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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'; import * as express from 'express'; import * as bodyParser from 'body-parser';
import { Injectable, Field, ObjectDefinition, ObjectImplementation, Resolver, Arg, Schema, BaseSchema, Interface } from 'aerographql';
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 ]: (PonctualTodo | RecurentTodo) [] } = { Bob: [ { id: '0', title: 'Todo1', content: 'Bob Todo1 content', occurence: 'Every Week' }, { id: '1', title: 'Todo2', content: 'Bob Todo2 content', date: 'Friday' }, { id: '2', title: 'Todo3', content: 'Bob Todo3 content', occurence: 'Every Day' } ], Alice: [ { id: '3', title: 'Todo1', content: 'Alice Todo1 content', date: 'Mondy' }, { id: '4', title: 'Todo2', content: 'Alice Todo2 content', date: 'Saturday' }, { id: '5', title: 'Todo3', content: 'Alice Todo3 content', occurence: 'Every Week' } ], Steeve: [ { id: '6', title: 'Todo1', content: 'Steeve Todo1 content', occurence: 'Every Month' }, { id: '7', title: 'Todo2', content: 'Steeve Todo2 content', date: 'Tuesday' }, { id: '8', title: 'Todo3', content: 'Steeve Todo3 content', occurence: 'Every Day' } ] };
@Injectable() class UserService { find( name: string ) { return users.find( u => u.name === name ); } }
@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; }
@Interface( { name: 'Todo' } ) export class Todo { @Field( { type: 'ID' } ) id: string; @Field() title: string = ""; @Field() content: string = "Empty todo"; }
@ObjectDefinition( { implements: [Todo] } ) export class PonctualTodo { @Field( { type: 'ID' } ) id: string; @Field() title: string = ""; @Field() content: string = "Empty todo"; @Field() date: string = "Date"; }
@ObjectDefinition( { implements: [Todo] } ) export class RecurentTodo { @Field( { type: 'ID' } ) id: string; @Field() title: string = ""; @Field() content: string = "Empty todo"; @Field() occurence: string = "Date"; }
@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 { constructor( private userService: UserService ) {}
@Resolver( { type: User } ) user( @Arg() name: string ): User | Promise<User> { return this.userService.find( name ); } }
@Schema( { rootQuery: 'RootQuery', components: [ RootQuery, User, UserImpl, Todo, PonctualTodo, RecurentTodo ], providers: [ UserService ] } ) export class MySchema extends BaseSchema { }
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 !' ); } );
|