@n1ru4l/apollo-link-rest

0.1.9 • Public • Published

Experimental Apollo Rest Link Implementation

CircleCI

Demo: https://codesandbox.io/s/5yknn3jjzp

If you have ideas or want to contribute feel free to open issues or pull requests 😊

Install

yarn add @n1ru4l/apollo-link-rest

Rest Schema Generator

Generate an executable Schema from typeDefs annotated with @rest directives This can be used by apollo-server or a schema link.

Example Schema

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")
}

Usage

import { generateRestSchema } from 'apollo-rest-link'
import { graphql } from 'graphql'
import fetch from 'node-fetch'

const typeDefs = `
  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)

Schema Link

Query a generated schema.

Usage

import { generateRestSchema, createSchemaLink } from 'apollo-rest-link'
import { execute, makePromise } from 'apollo-link'
import gql from 'graphql-tag'
import fetch from 'node-fetch'

const typeDefs = `
  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 = createSchemaLink({ schema })

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)

Rest Link

This implementation uses query directives and maps them to rest endpoints. At the moment there is only support for queries which are mapped to GET requests

Example Query

query userProfile($id: ID!) {
  userProfile(id: $id) @rest(type: "User", route: "/users/:id", params: { id: $id }) {
    id
    login
    friends @rest(
      type: "User"
      route: "/users/:userId/friends"
      provides: { userId: "id" } # map id from parent object to :userId route param
    ) {
      id
      login
    }
  }
}

Usage

import gql from 'graphql-tag'
import { execute, makePromise } from 'apollo-link'
import fetch from 'node-fetch'
import { createRestLink } from 'apollo-rest-link'

const link = createRestLink({ fetcher: fetch })

const query = gql`
  query userProfile($id: ID!) {
    userProfile(id: $id) @rest(type: "User", route: "/users/:id", params: { id: $id }) {
      id
      login
      friends @rest(
        type: "User"
        route: "/users/:userId/friends"
        provides: { userId: "id" } # map id from parent object to :userId route param
      ) {
        id
        login
      }
    }
  }
`

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

Tests

yarn test

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.1.91latest

Version History

VersionDownloads (Last 7 Days)Published
0.1.91
0.1.71
0.1.61
0.1.51
0.1.41
0.1.31
0.1.21
0.1.11
0.1.01

Package Sidebar

Install

npm i @n1ru4l/apollo-link-rest

Weekly Downloads

9

Version

0.1.9

License

MIT

Last publish

Collaborators

  • n1ru4l