Created
October 1, 2018 19:03
-
-
Save cuchi/cbe3f7ab2abb2c5d667f20a154876309 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { buildSchemaSync, Resolver, Query, ObjectType, Mutation, Field } from 'type-graphql' | |
import { ApolloServer } from 'apollo-server' | |
import 'reflect-metadata' | |
const plainUsersArray = [{ | |
id: 1, | |
name: 'John Doe' | |
}, { | |
id: 2, | |
name: 'Foo' | |
}, { | |
id: 3, | |
name: 'Bar' | |
}] | |
@ObjectType() | |
class SimpleUser { | |
@Field() id: number | |
@Field() name: string | |
} | |
@ObjectType() | |
class Entity { | |
@Field() id: number | |
} | |
@ObjectType() | |
class InheritedUser extends Entity { | |
@Field() name: string | |
} | |
@Resolver() | |
class UserResolver { | |
@Query(_ => [SimpleUser]) | |
users(): SimpleUser[] { | |
return plainUsersArray | |
} | |
@Query(_ => [InheritedUser]) | |
inheritedUsers(): InheritedUser[] { | |
return plainUsersArray | |
} | |
@Mutation(_ => String) | |
someMutation() { | |
return "" | |
} | |
} | |
const schema = buildSchemaSync({ resolvers: [UserResolver] }) | |
const apolloServer = new ApolloServer({ schema }) | |
apolloServer.listen(3000).then(() => { | |
console.log('The server is listening...') | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "type-graphql-test", | |
"dependencies": { | |
"apollo-server": "^2.1.0", | |
"reflect-metadata": "^0.1.12", | |
"ts-node": "^7.0.1", | |
"type-graphql": "^0.14.0", | |
"typescript": "^3.1.1" | |
}, | |
"scripts": { | |
"start": "ts-node index.ts" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "es2018", | |
"module": "commonjs", | |
"experimentalDecorators": true, | |
"emitDecoratorMetadata": true, | |
"lib": ["esnext"] | |
}, | |
"include": ["index.ts"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment