@n1ru4l/graphql-schema-generator-rest

A graphql schema generator for directive annotated type definitions

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@n1ru4l/graphql-schema-generator-rest
96642.0.05 years ago5 years agoMinified + gzip package size for @n1ru4l/graphql-schema-generator-rest in KB

Readme

Rest GraphQL Schema Generator
Commitizen friendly npm version CircleCI
This package provides the functionality of generating a GraphQL schema from type definitions annotated with @rest directives.

Install

yarn add @n1ru4l/graphql-schema-generator-rest

Usage

Check out the examples!

Type Definitions

type User {
  id: ID!
  login: String!
  friends: [User]!
    @rest(
      route: "/users/:userId/friends"
      provides: { userId: "id" } # map id from parent object to :userId route param
    )
}

type Query {
  user(id: ID!): User @rest(route: "/users/:id")
}

Creating a schema

import { generateRestSchema } from '@n1ru4l/graphql-schema-generator-rest'
import { graphql } from 'graphql'
import gql from 'graphql-tag'
import fetch from 'node-fetch'

const typeDefs = gql`
  type User {
    id: ID!
    login: String!
    friends: [User]!
      @rest(
        route: "/users/:userId/friends"
        provides: { userId: "id" } # map id from parent object to :userId route param
      )
  }

  type Query {
    user(id: ID!): User @rest(route: "/users/:id")
  }
`

const schema = generateRestSchema({
  typeDefs,
  fetcher: fetch,
})

const query = `
  query user {
    user(id: "2") {
      id
      login
      friends {
        id
        login
      }
    }
  }
`

graphql(schema, query)
  .then(console.log)
  .catch(console.log)

Recipies

apollo-link

import { generateRestSchema } from '@n1ru4l/graphql-schema-generator-rest'
import { Observable, ApolloLink } from 'apollo-link'
import { graphql, print } from 'graphql'
import gql from 'graphql-tag'
import fetch from 'node-fetch'

const typeDefs = gql`
  type User {
    id: ID!
    login: String!
    friends: [User]!
      @rest(
        route: "/users/:userId/friends"
        provides: { userId: "id" } # map id from parent object to :userId route param
      )
  }

  type Query {
    user(id: ID!): User @rest(route: "/users/:id")
  }
`

const schema = generateRestSchema({
  typeDefs,
  fetcher: fetch,
})

const link = new ApolloLink(operation =>
  new Observable(observer => {
    const { query, variables, operationName } = operation
    graphql(mergedSchema, print(query), {}, {}, variables, operationName)
      .then(result => {
          observer.next(result)
          observer.complete(result)
        })
        .catch(e => observer.error(e))
  })
)

const query = gql`
  query user {
    user(id: "2") {
      id
      login
      friends {
        id
        login
      }
    }
  }
`

makePromise(execute(link, { operationName: `userProfile`, query }))
  .then(console.log)
  .catch(console.log)

express

import express from 'express'
import bodyParser from 'body-parser'
import { generateRestSchema } from '@n1ru4l/graphql-schema-generator-rest'
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
import gql from 'graphql-tag'
import fetch from 'node-fetch'

const typeDefs = gql`
  type User {
    id: ID!
    login: String!
    friends: [User]!
      @rest(
        route: "/users/:userId/friends"
        provides: { userId: "id" } # map id from parent object to :userId route param
      )
  }

  type Query {
    user(id: ID!): User @rest(route: "/users/:id")
  }
`

const schema = generateRestSchema({
  typeDefs,
  fetcher: fetch,
})

const PORT = 3000

const app = express()

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }))
app.listen(PORT)

Tests

yarn test

Contribute

Checkout project

For contributions please fork this repository.
git clone https://github.com/<your-login>/graphql-schema-generator-rest.git
cd graphql-schema-generator-rest
yarn install

Commiting Changes

Please use yarn cm for commiting changes to git.