graphql-service-binding
The `ServiceBinding` class is used to create [`GraphQL Bindings`](https://github.com/graphql-binding/graphql-binding).
Last updated a year ago by abhiaiyer .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install graphql-service-binding 
SYNC missed versions from official npm registry.

Service Bindings

The ServiceBinding class is used to create GraphQL Bindings.

Background

GraphQL bindings takes a GraphQL endpoint and turns it into an SDK that can be used inside another application. A common parallel to a GraphQL binding can be considered a GraphQL-Aware API client.

Getting Started

yarn add graphql-service-binding

Creating a Binding

The example below shows creating a binding for a Service called jobs, deployed to Kubernetes.

// src/ServiceBindings/JobsBinding.ts

import ServiceBinding from "graphql-service-binding";

// Or import this from a .graphql file
const typeDefs = `
    type User {
        id: ID!
    }

    type Job {
        id: ID!
        hiringManager: User
    }

    type Query {
        jobPost(id: ID!): Job
    }
`;

export default function createJobBinding({ uri }) {
  return new ServiceBinding({
    uri,
    typeDefs,
    headersToForward: ["x-csod-user-id"]
  });
}

Configuring in your service

You can import your binding and use it within another GraphQL Server, Webhook, or REST API. Below is an example of using your binding in a simple ExpressJS server.

import express from "express";
import createJobBinding from "types/lib/ServiceBindings/createJobBinding";

const server = express();

const jobServiceClient = createJobBinding({
  uri: "http://jobs-svc/graphql/jobs"
});

server.get("/jobs/:id", async (req, res) => {
  const userId = req.userId;

  const projection = `
        {
            id
            hiringManager {
                id
            }
        }
    `;

  const job = await jobServiceClient.query.jobPost(
    {
      id: req.params.id
    },
    projection,
    {
      context: {
        "x-user-id": userId
      }
    }
  );

  return res.json(job);
});

server.listen(1337, () => {
  console.log("Running");
});

Current Tags

  • 0.0.4                                ...           latest (a year ago)

3 Versions

  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 3
Last Day 0
Last Week 3
Last Month 0
Dependencies (7)
Dev Dependencies (4)

Copyright 2014 - 2017 © taobao.org |