Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cuchi
Created October 1, 2018 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuchi/cbe3f7ab2abb2c5d667f20a154876309 to your computer and use it in GitHub Desktop.
Save cuchi/cbe3f7ab2abb2c5d667f20a154876309 to your computer and use it in GitHub Desktop.
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...')
})
{
"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"
}
}
{
"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