1.8.3 • Published 3 months ago

@mavvy/minigql v1.8.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

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
keydescription
parentContextThe return value of the resolver for this field's parent
variablesAn object that contains all GraphQL arguments provided for this field
inputShortcut for the input property from the variables. Same as variables.input
contextAn object shared across all resolvers that are executing for a particular operation.
infoContains 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');
}
1.8.2

3 months ago

1.8.3

3 months ago

1.8.1

6 months ago

1.8.0

6 months ago

1.7.0

6 months ago

1.6.2

6 months ago

1.6.1

6 months ago

1.6.0

6 months ago

1.5.1

6 months ago

1.5.0

6 months ago

1.4.0

6 months ago

1.3.2

6 months ago

1.3.1

8 months ago

1.3.0

8 months ago

1.2.0

8 months ago

1.1.1

8 months ago

1.0.0

8 months ago