Skip to content

rbonillajr/ngPanamaDemoRestGraphQLApollo

Repository files navigation

graphql-server-typed

MIT licensed code style: prettier renovate-app badge

Create a fully configured, production ready graphql server, using

  • typescript
  • graphql-code-generator
  • graphql-subscriptions
  • merge-graphql-schemas
  • Dependency injection with injection-js

This project demonstrates how to generate typescript types from graphql schema, using Graphql code generetor library.

Installation

run npm install

Build and start server instance

Build command

Running the command

npm run build

Or on windows machine, run

npm run build:win

will generate typescript types, and transpile code to dist folder

How to start server instance after building

npm start

Run server directly with ts-node

npm start:dev

Watch for code changes

npm run start:watch

This will monitor your changes and will automatically restart the server.

The server will run on port 8080. You can change this by editing the config file.

Code Formatting

We use Prettier and Tslint to format and enforce standards on our code.
Both will run on the project automatically before each commit.

Prettier rewrites code according to the .prettierrc.json configuration file.
If you want to activate prettier manually (on all .ts files inside src folder) without committing, run:

npm run prettier

Tslint will check rules found in the tslint.json configuration file.
If you want to check tslint manually (on all .ts files inside src folder) without committing, run:

npm run tslint

Type generation using gql codegen

npm run generate

This will automatically generate types in types.d.ts file! generate command is executed in every build

Project structure

We use the function makeExecutableSchema() from graphql-tools to to combine our types and resolvers. Instead of passing one large string for our schema, we split our types and resolvers to multiple files, located in graphql directory in types and resolvers directories. This way, we avoid schema complexity by using merge-graphql-schemas:

  import * as path from "path";
  import {makeExecutableSchema} from "graphql-tools";
  import {fileLoader, mergeTypes, mergeResolvers} from "merge-graphql-schemas";
  import {GraphQLSchema} from "graphql";

  const typesArray = fileLoader(path.join(__dirname, '../types'), { recursive: true });
  const resolversArray = fileLoader(path.join(__dirname, '../resolvers'));
  const allTypes = mergeTypes(typesArray);
  const allResolvers = mergeResolvers(resolversArray);
  let schema: GraphQLSchema;
  schema= makeExecutableSchema({
      typeDefs: allTypes,
      resolvers: allResolvers
  });

  export default schema;

So as your project grows - you can extend the schema by adding new type in types directory, and adding matching resolver file in resolvers directory. The schema is updated automatically.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published