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

@grapi/server

v3.0.4

Published

Grapi Schema Generator For GraphQL Server

Downloads

17

Readme

Businesses usually involving a broad spectrum of applications, that aren't integrated and requiring human intervention at almost every step of the process, this lack of integration and automation pretend to be solved trought an autogenerated GraphQL API Layer. Grapi make GraphQL API integration simple and easy.

Installation

yarn add @grapi/server

Features

Build GraphQL API with GraphQL SDL

SDL or Schema Definition Language is part of GraphQL Language, to define data and resolvers for the GraphQL API.

# File schema.graphql
enum Gender {
    NO_GENDER,
    FEMALE,
    MALE
}
type Actor @Model( dataSource: "datasource", key: "Actor" ) {
    id: ID ! @unique
    name: String !
    gender: Gender
}

Grapi read SDL types and autogen resolvers for query and mutations on every model defined in SDL schema.

Grapi for Typescript

yarn init
yarn add @grapi/server @grapi/mongodb
yarn add ts-node apollo-server 
yarn add -D typescript
// server.ts
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { MongodbDataSourceGroup } from '@grapi/mongodb'
import { Grapi } from '@grapi/server'
import { ApolloServer } from 'apollo-server'

const getDataSource = async () => {
    const datasource = new MongodbDataSourceGroup(
        process.env.MONGO_URI,
        process.env.MONGO_DATA_BASE_NAME
    )
    await datasource.initialize()
    return datasource
}

const startGraphQLServer = async () => {
    const datasource = await getDataSource()
    const sdl = readFileSync( resolve( __dirname, 'schema.graphql' ) ).toString()
    const grapi = new Grapi( {
        sdl,
        dataSources: {
            datasource: ( args ) => datasource.getDataSource( args.key ),
        }
    } )
    const server = new ApolloServer( grapi.createApolloConfig() )
    server.listen().then( ( { url } ) => {
        console.info( `GraphQL Server On: ${ url }` )
        console.info( `Go To Browser And See PlayGround` )
    } )
}

startGraphQLServer()

Run server

yarn ts-node server.ts 

Grapi for JavaScript

yarn init
yarn add @grapi/server @grapi/mongodb
yarn add apollo-server
// server.js
const { readFileSync } = require( 'fs' )
const { resolve } = require( 'path' )
const { MongodbDataSourceGroup } = require( '@grapi/mongodb' )
const { Grapi } = require( '@grapi/server' )
const { ApolloServer } = require( 'apollo-server' )

const getDataSource = async () => {
    const datasource = new MongodbDataSourceGroup(
        process.env.MONGO_URI,
        process.env.MONGO_DATA_BASE_NAME
    )
    await datasource.initialize()
    return datasource
}

const startGraphQLServer = async () => {
    const datasource = await getDataSource()
    const sdl = readFileSync( resolve( __dirname, 'schema.graphql' ) ).toString()
    const grapi = new Grapi( {
        sdl,
        dataSources: {
            datasource: ( args ) => datasource.getDataSource( args.key ),
        }
    } )
    const server = new ApolloServer( grapi.createApolloConfig() )
    server.listen().then( ( { url } ) => {
        console.info( `GraphQL Server On: ${ url }` )
        console.info( `Go To Browser And See PlayGround` )
    } )
}

startGraphQLServer()

Run server

node server.js

You can see the GraphQL server in action with

GraphQL PlayGround

Insomnia

Graphiql

Also Apollo Server offer a GraphQL Playground open your browser http://localhost:4000

Auto-Generated GraphQL Schema

Main characteristic of Grapi is autogen a GraphQL API with types defined in SDL, the previous schema create the next resolvers.

Singular and Plural

type Query {
    actor( where: ActorWhereUniqueInput ): Actor !
    actors( where: ActorWhereInput ): [ Actor ! ] !
}

Create - Update and Delete

type Mutation {
    createActor( data: ActorCreateInput ): Actor !
    updateActor( where: ActorWhereUniqueInput data: ActorUpdateInput ): Actor !
    deleteActor( where: ActorWhereUniqueInput ): Actor !
}

These resolvers serve a schema in a GraphQL Server. Admit retrieve and save data from datasource provided by Mongo DataSource or your custom data source.

RelationShip Made Easy

Database relationships are associations between tables. Grapi support autogen resolvers for queries and mutations in schema relations. It's easy create a complex server with data relations to retrieve and save data.

Next schema examples shows how to create different relationship types.

The key value in directive @Model is the collection name in mongodb

The datasource value in directive @Model is datasource alias defined in Grapi instance into dataSources object. That alias is arbitrary and is allow to named as you wish. The only condition is that in schema the alias has to be the same

One To One Unidirectional

# File schema.graphql
type ActorToAddress implements Relation @config( 
    name: "ActorToAddress"
    foreignKey: { key: "city_id", side: Actor } 
)

type Actor @Model( dataSource: "datasource", key: "Actor" ) {
    id: ID ! @unique
    name: String !
    address: Address @relation( with: ActorToAddress )
}

type Address @Model( dataSource: "datasource", key: "Address" ) {
    id: ID ! @unique
    street: String !
    location: Json
}

One To One Bidirectional

# File schema.graphql
type ActorToAddress implements Relation @config( 
    name: "ActorToAddress"
    foreignKey: { key: "city_id", side: Actor } 
)

type Actor @Model( dataSource: "datasource", key: "Actor" ) {
    id: ID ! @unique
    name: String !
    address: Address @relation( with: ActorToAddress )
}

type Address @Model( dataSource: "datasource", key: "Address" ) {
    id: ID ! @unique
    street: String !
    location: Json
    actor: Actor @relation( with: ActorToAddress )
}

One To Many Bidirectional

# File schema.graphql
type VehiclesFromActor implements Relation @config( 
    name: "VehiclesFromActor"
    foreignKey: { key: "owner_car_id" } 
)

type Actor @Model( dataSource: "datasource", key: "Actor" ) {
    id: ID ! @unique
    name: String !
    vehicles: [ Vehicle ! ] ! @relation( with: VehiclesFromActor )
}

type Vehicle @Model( dataSource: "datasource", key: "Vehicle" ) {
    id: ID ! @unique
    trademark: String !
    model: String
    name: String
    owner: Actor @relation( with: VehiclesFromActor )
}

Many To Many

# File schema.graphql
type MoviesFromActorManyToMany implements Relation @config( name: "MoviesFromActorManyToMany" )

type Actor @Model( dataSource: "datasource", key: "Actor" ) {
    id: ID ! @unique
    name: String !
    movies: [ Movie! ] ! @relation( with: MoviesFromActorManyToMany )
}

type Movie @Model( dataSource: "datasource", key: "Movie" ) {
    id: ID ! @unique
    title: String !
    actors: [ Actor ! ] ! @relation( with: MoviesFromActorManyToMany )
}

Supported data-sources

Inspired By

License

Apache-2.0

footer banner

Madrov Team