Development Environment
#
TypeScriptGraphQL Modules always supports the latest TypeScript version. Lower versions are not supported!
#
TS-Node (recommended way)To set up your development environment easily, we recommend to use TypeScript.
You don't have to use TypeScript, but it makes it much easier to use GraphQL Modules.
To get started with your development environment, install the following tools in your project:
Next, create/update tsconfig.json
in your root directory:
These configurations facilitate development, but of course you can modify them as you wish. Keep particularly in mind to keep
experimentalDecorators: true
because that's important for GraphQL Modules.
Next, add the following scripts to your package.json
:
dev
starts your server in the development mode.debug
starts the server in the debug mode.build
uses thetsc
compiler to compile your code to JavaScript.start
runs the compiled server using pure Node.
paths
#
TypeScript has an aliasing mechanism for working with modules directories.
To set it up quickly with GraphQL Modules, first add the following package to your server:
Then update your scripts to load the require extension for TypeScript paths
:
And you can add custom mapping to your tsconfig.json
file:
You can now import files between modules like below:
.graphql
files#
Import from You can also treat .graphql
files as text files and import from them easily.
It's useful because many IDEs detect .graphql
files and do syntax highlighting for them.
You can use graphql-import-node to enable NodeJS to import .graphql
files:
#
WebpackIf you are using Webpack, we recommend to use ts-loader or awesome-typescript-loader to load your TypeScript files.
To load .graphql
files, you can use graphql-tag/loader.
Here is a simple webpack.config.js
that should do the job:
#
TypeScript-Babel-StarterYou can use Babel for TypeScript with GraphQL Modules by using TypeScript-Babel-Starter.
Still, if you use dependency injection, you have to decorate each property and argument in the providers manually even for the classes like below;
#
JavaScript UsageIf you are using JavaScript (not TypeScript) in your project, you can either add support for TypeScript or use GraphQL Modules with the JavaScript API.
#
With BabelIf you are using Babel to transpile your JavaScript files, you can use babel-plugin-transform-decorators to get decorators support, which enables using decorators such as @Inject
in a regular way.
#
Without decorators#
Dependency InjectionYou can use Inject
and Injectable
as regular functions to wrap your arguments and classes from tslib
.
Take care to add the polyfill reflect-metadata
and require it (once).
#
Testing EnvironmentWe recommend Jest as your test runner: it has a simple API, it's super fast and you can integrate it with any CI tools.
#
JestTo test your GraphQL Modules server with Jest, first add support for TypeScript to your Jest instance:
Then, add the following section to your package.json
:
And add a script to your package.json
:
Also, make sure that each one of your spec files starts with:
#
Other Test RunnersYou can use any other test runner you prefer. Just figure out how to use it with TypeScript and make sure you can import CommonJS easily.