A module that attaches an ApolloServer to your Easy-Express Server!
π Homepage
To start using ApolloServer's awesome capabilities, simply attach a GraphQLModule
to your EasyExpressServer
and you're good to go!
npm install @easy-express/graphql
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);
},
},
};
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.
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;
});
});
π€ Leonard Parisi
- Website: leonardparisi.com
- Github: @leonardparisi
- LinkedIn: @https://www.linkedin.com/in/lenny-parisi/
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Give a βοΈ if this project helped you!
Bitcoin Wallet Address: 3PjDkxNznkx7HVmVdfSMYgGJeeJzoDH7e6