# @mavvy/minigql

> Minimalist Apollo Graphql Server

Latest version **1.8.3** (published 2024-03-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mavvy/minigql
pnpm add @mavvy/minigql
yarn add @mavvy/minigql
bun add @mavvy/minigql
```

Provides the command `minigql`.

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.8.3 |
| Published | 2024-03-06 |
| First published | 2023-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 7 |
| Unpacked size | 18.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Marc Jeric Espiritu |
| Maintainers | mavvystudio |

## Links

- npm: https://www.npmjs.com/package/@mavvy/minigql
- Repository: https://github.com/mavvy22/minigql
- Homepage: https://github.com/mavvy22/minigql#readme
- Issues: https://github.com/mavvy22/minigql/issues
- npm.io page: https://npm.io/package/@mavvy/minigql

## Dependencies (7)

- [dotenv](https://npm.io/package/dotenv.md) ^16.3.1
- [graphql](https://npm.io/package/graphql.md) ^16.6.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^11.1.0
- [typescript](https://npm.io/package/typescript.md) ^5.2.2
- [@apollo/server](https://npm.io/package/@apollo/server.md) ^4.9.3
- [semantic-release](https://npm.io/package/semantic-release.md) ^21.1.1
- [@semantic-release/git](https://npm.io/package/@semantic-release/git.md) ^10.0.1

## Recent versions

- 1.8.3 (latest) — 2024-03-06
- 1.8.2 — 2024-03-06
- 1.8.1 — 2023-12-08
- 1.8.0 — 2023-12-07
- 1.7.0 — 2023-12-06
- 1.6.2 — 2023-11-22
- 1.6.1 — 2023-11-22
- 1.6.0 — 2023-11-22
- 1.5.1 — 2023-11-22
- 1.5.0 — 2023-11-22
- 1.4.0 — 2023-11-21
- 1.3.2 — 2023-11-20
- 1.3.1 — 2023-10-06
- 1.3.0 — 2023-10-06
- 1.2.0 — 2023-10-02
- … 2 more at https://npm.io/package/@mavvy/minigql/versions

## README

# MiniGQL - A Minimalist Nodejs Graphql Server

Setting up a nodejs graphql server should be simple, right?

## Setup

***IMPORTANT***
Before you get started, just remember that this framework requires at least 1 Query and 1 Mutation to get it running.

### Install

```bash
npm install @mavvy/minigql
```

install typescript
```bash
npm install typescript @types/node --save-dev
```

### package.json

Set type to module
```json
{
  "type": "module"
}
```

### Add script to package.json
```javascript
  {
    "scripts": {
      "start": "minigql start"
    }
  }
```

### sample tsconfig.json file
```json
{
  "compilerOptions": {
    "lib": ["es2020"],
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "types": ["node"]
  }
}
```

### Add .env

```bash
export PORT = 3000
```

### Add schema

create a schema.ts file under src directory

```typescript
// src/schema.ts

export default `
  type Todo {
    name: String
  }

  input AddTodoInput {
    name: String!
  }
`

```

### Add resolvers

create the files under src/resolvers directory

```typescript
// src/resolvers/todos.ts
export const resolverType = 'Query';

export const returnType = '[Todo]';

export const handler = async () => {
  return [{
    name: 'My Todo One'
  }];
}
```

#### Resolver options

##### resolverType
Optional. Resolver type: currently supported types are `Query` and `Mutation`. `Subscription` soon. Default is Query.

```javascript
export const resolverType = 'Query';
```

##### returnType

Optional. The return type of the gql resolver that is defined on your `schema.ts` file.

```javascript
export const returnType = '[Product]';
```

##### inputVariable
Optional. input type name for the resolver argument named input

```javascript
export const inputVariable = 'NameInput!';
```
Note: Make sure you define the NameInput on your schema.ts file like so:
```javascript
//src/schema.ts
export default `
  input NameInput {
    name: String
  }
`
```
On your resolver, you can access it via params
```javascript
export const resolver = async ({input}) => {
  console.log(input); // {name: 'foo'}
}
```

##### handler

Required. The main resolver function to execute
```javascript
export const handler = async () => {
  return {name: 'foo'}
}
```
###### handler params
|key|description|
|---|-----------|
|parentContext|The return value of the resolver for this field's parent
|variables|An object that contains all GraphQL arguments provided for this field|
|input|Shortcut for the input property from the variables. Same as `variables.input`|
|context|An object shared across all resolvers that are executing for a particular operation. |
|info|Contains information about the operation's execution state, including the field name, the path to the field from the root, and more.|

```javascript
export const handler = async (handlerParams) => {
  console.log(handlerParams.input);
}
```
## Advanced Configuration

### Apollo Config

Create a server.ts file under src directory

```javascript
// src/server.ts
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache';

export const apolloConfig = {
  cache: new InMemoryLRUCache(),
};
```

### serverConfig

Apollo standAloneServer config

```javascript
// src/server.ts

const getToken = (req) => req.headers.authentication;

export const serverConfig = {
  context: async ({ req }) => ({
    token: getToken(req),
  }),
}
```

### preStart function
Good location for running a database connection. etc.

```javascript
// src/server.ts
import mongoose from 'mongoose';

export async function preStart() {
  await mongoose.connect(process.env.MONGO_URI);
  console.log('connected to db');
}
```

---
_Source: https://npm.io/package/@mavvy/minigql · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
