@occupop/lib-graphql
v3.0.1
Published
library used by microservices
Downloads
99
Maintainers
Keywords
Readme
Claro! Aqui está o README.md traduzido para o inglês:
# GraphQL Server Library
A library to set up a GraphQL server with Apollo Server, Express, and various useful configurations.
## Installation
```bash
npm install @kaueoccupop/lib-graphqlUsage
Basic Configuration
Create and configure a GraphQL server with type definitions, resolvers, and context.
import express from 'express';
import { makeGraphqlServer, GraphqlServerInputParams } from '@kaueoccupop/lib-graphql';
import { typeDefs, resolvers } from './schema';
import { contextFunction } from './context';
const app = express();
const params: GraphqlServerInputParams = {
port: 4000,
sensitiveRoutesConfig: {
'/auth/login': ['password'],
'/auth/register': ['password', 'ssn'],
},
routeConfig: {
router: express.Router(),
prefix: '/api',
},
routes: [
{
method: 'post',
path: '/custom-endpoint',
handlers: [
(req, res) => {
res.send('Hello from custom endpoint!');
},
],
},
],
};
makeGraphqlServer({
contextFunction,
app,
typeDefs,
resolvers,
})(params).then(() => {
console.log('Server is running...');
});Folder Structure
src/index.ts(main file)schema.ts(type definitions and resolvers)context.ts(context function)middleware/logRouteMiddleware.ts(middleware for route logging)
Configurations
GraphqlServerInputParams
| Property | Type | Description |
|------------------------|-------------------------------|-------------------------------------------------------------------------------------------------|
| port | number | Port where the server will run |
| routeConfig | RoutingConfig | Express router configuration |
| middlewares | ApplicationRequestHandler[] | List of express middlewares |
| routes | HttpRoute[] | Custom route definitions |
| errorRequestHandler | ErrorRequestHandler | Custom error handler |
| sensitiveRoutesConfig| SensitiveRouteConfig | Configuration for routes and sensitive fields to be masked in logs |
SensitiveRouteConfig
An object mapping routes to a list of sensitive fields that should be masked in the logs.
const sensitiveRoutesConfig: SensitiveRouteConfig = {
'/auth/login': ['password'],
'/auth/register': ['password', 'ssn'],
};Publishing to npm
To publish the library to npm, follow these steps:
- Configure
package.json
Ensure your package.json is correctly configured with the name, version, and other necessary information.
{
"name": "@kaueoccupop/lib-graphql",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "jest",
"cover": "jest --coverage"
},
"files": [
"dist"
],
"devDependencies": {
"typescript": "^5.3.3",
"ts-jest": "^29.1.2",
"jest": "^29.7.0",
"@types/jest": "^29.5.12"
},
"dependencies": {
"@apollo/server": "^4.10.1",
"@apollo/subgraph": "^2.7.1",
"express": "^4.18.3",
"cors": "^2.8.5",
"helmet": "^7.1.0",
"graphql": "^16.8.1",
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"express-rate-limit": "^7.2.0",
"awilix": "^10.0.2"
}
}- Compile the Code
Compile your TypeScript code to JavaScript.
npm run build- Publish to npm
Log in to your npm account and publish the library.
npm login
npm run publish:npmNotes
- Helmet: Used to set security-related HTTP headers.
- Rate Limit: Limits the number of requests per time window.
- CORS: Enables CORS for all routes.
- Health Check: Health check endpoint configured at
/health. - Route Logging: Middleware for logging request and response details.
Configuration Examples
Type Definitions and Resolvers
// schema.ts
import { gql } from '@apollo/server';
export const typeDefs = gql`
type Query {
hello: String
}
`;
export const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};Context Function
// context.ts
import { ExpressContextFunctionArgument } from '@apollo/server/express4';
export const contextFunction = ({ req, res }: ExpressContextFunctionArgument) => {
return {
authUser: req.user,
container: req.container,
};
};License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
If you would like to contribute to the project, follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b my-feature. - Make your changes and commit them:
git commit -m 'My new feature'. - Push to the branch:
git push origin my-feature. - Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
I hope this `README.md` helps to document your project clearly and completely.