Tommsy64 / graphql

An Easy-Express module that attaches an ApolloServer to your Easy-Express Server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Welcome to Easy-Express's GraphQL Module 👋

Version License: MIT

A module that attaches an ApolloServer to your Easy-Express Server!

🏠 Homepage

Introduction

To start using ApolloServer's awesome capabilities, simply attach a GraphQLModule to your EasyExpressServer and you're good to go!

Install

npm install @easy-express/graphql

Setup

Create a new directory to hold all of your GraphQL modules. Each file in this directory should export typeDefs and resolvers like the example below:

import { gql } from 'apollo-server-express';
import { Balances } from '../entities/Balances';
import { getRepository } from 'typeorm';

export const typeDefs = gql`
  type Balance {
    id: ID!
    date: String!
    balance: Float!
  }

  extend type Query {
    get_all_balances: [Balance]
  }

  extend type Mutation {
    insert_balance(id: ID!, date: String!, balance: Float!): Balance
  }
`;
export const resolvers = {
  Query: {
    get_all_balances: async () => getRepository(Balances).find(),
  },
  Mutation: {
    insert_balance: async (obj: any, args: any) => {
      const balance = new Balances();
      balance.date = args.date;
      balance.balance = args.balance;
      return getRepository(Balances).save(balance);
    },
  },
};

Usage

Create a new GraphQLModule and pass the path to the directory holding all your graphql modules. Then, simply attach the module to your EasyExpressServer. This will add the '/graphql' endpoint to your app.

Example

import { EasyExpressServer } from '@easy-express/server';
import { DatabaseModule } from '@easy-express/typeorm';
import { GraphQLModule } from '@easy-express/graphql';
import * as dotenv from 'dotenv';

// load env vars from .env file
dotenv.config();

// create a new server
let server = new EasyExpressServer();

// define routes for your server...
server.instance.get('/', (req, res) => {
  res.send('Hello World!');
});

// attach the modules
server.attachModule(new GraphQLModule(__dirname + '/graphql-modules/')).then(() => {
  server
    .attachModule(new DatabaseModule(__dirname + '/entities/'))
    .then(() => {
      // Start the server after you've attached the two module
      server.start();
    })
    .catch((e) => {
      console.error(e);
      return e;
    });
});

Author

👤 Leonard Parisi

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

Bitcoin Wallet Address: 3PjDkxNznkx7HVmVdfSMYgGJeeJzoDH7e6

About

An Easy-Express module that attaches an ApolloServer to your Easy-Express Server.


Languages

Language:TypeScript 100.0%