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

graphql-example

v1.6.1

Published

Intuitive GraphQL Resolver Example - Application example using contextable.js as GraphQL rootValue on steroids.

Downloads

40

Readme

graphql-example

Intuitive GraphQL Resolver Example - Application example using RawModel.js as GraphQL rootValue on steroids.

GraphQL is a modern replacement for the well known REST API server. This is a pure GraphQL server application - an example API server.

Features

This example uses Node.js v7 and MongoDB.

  • GraphQL rootValue using RawModel.js.
  • Nested schema.
  • Print GraphQL schema from command-line.
  • Execute GraphQL schema from command-line.
  • Input data validation.
  • Context-aware models.
  • Graphql HTTP server.
  • MongoDB connector (an example how to use a database connector).

Pre-requisites

  • Make sure you are using Node.js v7+.
  • Install and start MongoDB server.

Build Setup

# install dependencies
npm install

# start the server (GraphiQL is started at http://127.0.0.1:3000)
npm start

# use nodemon in development to automatically reload the server on changes
npm install -g nodemon
nodemon --exec npm start

# run GraphQL query from command-line
npm run exec '{getUsers {id name}}'

# run tests
npm test

Run

npm start

Starts GraphiQL server at http://127.0.0.1:3000/

Query Examples

mutation { # create new user
  createUser(name: "") {
    id
    name
    errors {
      path
      errors {
      	validator
      	message
      	code
      }
    }
  }
}
query { # get users
  getUsers(skip: 0, limit: 5) {
    id
    name
  }
}

Architecture

|- /config    - config files
|- /scripts   - scripts that can be executed from CLI (used by `package.json`)
|- /src
  |- /graph`    - GraphQL application
  |- /http      - HTTP server
  |- /lib       - general helpers (e.g. Mongo DB connector)
  |- index.js   - application main file
- /tests        - tests written in [ava](https://github.com/avajs/ava) framework

The application exports two main classes - Graph (GraphQL application - src/http) and HTTP (HTTP server - src/graph). Each class represents a stand-alone application. You could create two separated npm packages from this to further split your code to responsibilities.

The scripts in the ./src/scripts folder use these classes to print GraphQL schema, execute GraphQL query and start the HTTP server from the command-line. These scripts are used by the package.json file thus you can use the npm run {script-name} commands.

Graph application describes your data model and provides a communication layer. HTTP application exposes GraphQL application over HTTP thus users can use the GraphQL application as your API endpoint.

The HTTP server is based on express-graphql which is a bridge to communicate with a GraphQL application via Express HTTP server. You could substitute this with koa-graphql or koa-graphql-next. The express-graphql middleware includes a GraphiQL user interface which is a generic interface for running GraphQL queries and mutations (for use in development).

Graph application exposes the API over the GraphQL schema defined in ./src/graph/schema/index.graphql. It uses the RawModel.js for describing and validating input data. To keep the example simple, we only have two models here where the Root model represents a GraphQL resolver - the rootValue for GraphQL.

GraphQL Clients

Your front-end application will need a GraphQL client to communicate with a GraphQL server. You can also use a raw browser's fetch to post data to the GraphQL server.

Popular GraphQL clients (you can add your own):

Tutorials

Node.js tutorials: Node.js Cheatsheet