npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mavvy/minigql

v1.8.3

Published

Minimalist Apollo Graphql Server

Downloads

26

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

npm install @mavvy/minigql

install typescript

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

package.json

Set type to module

{
  "type": "module"
}

Add script to package.json

  {
    "scripts": {
      "start": "minigql start"
    }
  }

sample tsconfig.json file

{
  "compilerOptions": {
    "lib": ["es2020"],
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "types": ["node"]
  }
}

Add .env

export PORT = 3000

Add schema

create a schema.ts file under src directory

// src/schema.ts

export default `
  type Todo {
    name: String
  }

  input AddTodoInput {
    name: String!
  }
`

Add resolvers

create the files under src/resolvers directory

// 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.

export const resolverType = 'Query';
returnType

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

export const returnType = '[Product]';
inputVariable

Optional. input type name for the resolver argument named input

export const inputVariable = 'NameInput!';

Note: Make sure you define the NameInput on your schema.ts file like so:

//src/schema.ts
export default `
  input NameInput {
    name: String
  }
`

On your resolver, you can access it via params

export const resolver = async ({input}) => {
  console.log(input); // {name: 'foo'}
}
handler

Required. The main resolver function to execute

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.|

export const handler = async (handlerParams) => {
  console.log(handlerParams.input);
}

Advanced Configuration

Apollo Config

Create a server.ts file under src directory

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

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

serverConfig

Apollo standAloneServer config

// 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.

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

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