With our database code in place, we are now ready to turn our attention to writing our GraphQL server. One of the earliest decisions I took when writing the sample code for this chapter was that we would simplify the process of writing our code as much as possible. If we look at a reference sample that Facebook posted, we will find that the code can be tediously long-winded:
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString
} from 'graphql';
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve() {
return 'world';
}
}
}
})
});
This example is from https://github.com/graphql/graphql-js. We can see that we have a lot of reliance on special types that don't map one to one onto TypeScript types.
Since we want to make our code that bit more TypeScript-friendly, we are going to use type-graphql. We will install it via npm, along with the graphql type definitions and reflect-metadata:
npm install type-graphql @types/graphql reflect-metadata --save
At this stage, we should also set our tsconfig file up to look like this:
{
"compileOnSave": false,
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"lib": ["es2016", "esnext.asynciterable", "dom"],
"outDir": "./dist",
"noImplicitAny": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
}
}