Coder Social home page Coder Social logo

esakkik / graphql-server-typed Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tomyitav/graphql-server-typed

0.0 1.0 0.0 109 KB

Using typescript and gql-code-gen for easy graphql setup

Home Page: https://medium.com/@tomyitav/graphql-server-for-enterprise-development-675d2ff2d0ed

Dockerfile 1.84% TypeScript 98.16%

graphql-server-typed's Introduction

graphql-server-typed

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

Clone the repository and run npm install

git clone https://github.com/tomyitav/graphql-server-typed.git
npm install

Starting the server

npm run build:dev
npm start

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

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.

Run server in production with Docker

npm run build

After npm building the project, go to project root directory, open shell and run:

docker build -t graphql-server .

Instructions about running the container are available here

Connect to the server from client app

See the following example on how to connect to the server using apollo-angular.

graphql-server-typed's People

Contributors

tomyitav avatar

Watchers

Esakki Krishnan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.