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

@vobi/api-composer

v0.0.48

Published

API composer to generate GraphQL and REST from single configuration

Downloads

3

Readme

API Composer

api-composer is a tool to describe node.js APIs with simple and elegant syntax. If you like simplicity and need to build complex GraphQL and/or REST APIs from single API description, you will love api-composer.

Table of Contents

Why?

Simply speaking api-composer is a kind of configuration layer. It's just a way to describe API with the concepts coming from graphql ecosystem. You can generate anything you want from this description. You need just write your own plugin for this purpose or use already built plugins. By default api-composer comes with graphql schema generator that is based on excellent graphql-composer library.

With adopting api-composer you can graphqlize even your REST API.

Here are main benefits:

  • Simple and elegant syntax to describe API.
  • Single source from which you can generate graphql schema, routes for express/koa and so on.
  • Middleware system which you can use to run functions before and after resolve function. You can run some middlewares synchronously and others - asynchronously.

Install

npm i @vobi/api-composer

Quick Demo

Create simple project and initialize package.json

mkdir coolapi && cd coolapi
npm init -y

Install some npm packages to actually build and serve graphql api

npm i @vobi/api-composer graphql graphql-compose express express-graphql body-parser

Create index.js file

touch index.js

Alongside of other necessary modules import and initialize api-composer

const express = require('express')
const bodyParser = require('body-parser')
const graphqlHTTP = require('express-graphql')
const { ApiComposer } = require('@vobi/api-composer')

const app = express()

app.use(bodyParser.json())

const api = new ApiComposer()

Add simple query and simple mutation.

api.query('hello', () => 'Hello, World!')
api.mutation('simpleMutation', () => 'I am a simple mutation')

Note: first argument is a name of query/mutation and second - resolve function.

Finally, you can generate and pass schema to express' graphqlHTTP middleware, and run express app

app.use(
  '/graphql',
  graphqlHTTP({
    schema: api.getGraphqlSchema(),
    graphiql: true
  })
)

app.listen(8000, function () {
  console.log('app launch on 8000')
  console.log('Go to http://localhost:8000/graphql')
})

That's it. Complete index.js file will look like this

const express = require('express')
const bodyParser = require('body-parser')
const graphqlHTTP = require('express-graphql')
const { ApiComposer } = require('@vobi/api-composer')

const app = express()

app.use(bodyParser.json())

const api = new ApiComposer()

api.query('hello', () => 'Hello, World!')
api.mutation('simpleMutation', () => 'I am a simple mutation')

const graphqlSchema = api.getGraphqlSchema()

app.use(
  '/graphql',
  graphqlHTTP({
    schema: graphqlSchema,
    graphiql: true
  })
)

app.listen(8001, function () {
  console.log('app launch on 8001')
  console.log('Go to http://localhost:8001/graphql')
})

When you run the program (node index.js) and open http://localhost:8001/graphql you can request to our query and mutation:

query hello {
  hello
}

and

mutation simpleMutation {
  simpleMutation
}

More Examples

You can see more examples in ./examples folder of this repository.

License

API Composer is MIT licensed.