2.3.0 • Published 6 years ago

remote-graphql-binding v2.3.0

Weekly downloads
14
License
MIT
Repository
github
Last release
6 years ago

remote-graphql-binding

npm version

Overview

remote-graphql-binding helps to generate sdk for remote graphql services and use power of intellisense & type safety (using Typescript).

How it works

  1. Create graphqlconfig file using graphql cli:
graphql init
  1. Add generation script in package.json:
{
  ...
  "scripts": {
    ...
    "generate": "graphql get-schema --all && graphql codegen"
  }
}
  1. Generate sdk for remote graphql service:
yarn generate
# or
npm run generate

Thats it, now you can use generated file to make remote calls, please check example

Install

yarn add remote-graphql-binding
# or
npm install --save remote-graphql-binding

Example

folder structure:

  • src/index.ts
  • .grpahqlconfig
  • .env

.grpahqlconfig

{
  "projects": {
    "Neo4jService": {
      "schemaPath": "schemas/db.graphql",
      "extensions": {
        "endpoints": {
          "dev": {
            "url": "${env:DB_SERVICE_URL}",
            "headers": {
              "Authorization": "basic ${env:DB_AUTH_TOKEN}"
            }
          }
        },
        "codegen": [
          {
            "generator": "remote-graphql-binding",
            "language": "typescript",
            "output": {
              "binding": "src/generated/neo4j-service.ts"
            }
          }
        ]
      }
    }
  }
}

.env

DB_SERVICE_URL=
DB_AUTH_TOKEN=

src/index.ts

import { Binding as DB } from './generated/neo4j-service'

const service = new DB({
  endpoint: process.env.DB_SERVICE_URL,
  authorizationHeader: `basic ${process.env.DB_AUTH_TOKEN}`,
})

service.query.Song({}).then(x => console.log(x))

API

constructor(options: Options): Binding

The Options type has the following fields:

KeyRequiredTypeDefaultNote
endpointYesstring-The endpoint of your Prisma service
authorizationHeaderNostringnullAuthorization header for secured services
fragmentReplacementsNoFragmentReplacementsnullA list of GraphQL fragment definitions, specifying fields that are required for the resolver to function correctly
debugNobooleanfalseLog all queries/mutations to the console

request

The request method lets you send GraphQL queries/mutations to your Prisma service. The functionality is identical to the auto-generated delegate resolves, but the API is more verbose as you need to spell out the full query/mutation. request uses graphql-request under the hood.

Here is an example of how it can be used:

const query = `
  query ($userId: ID!){
    user(id: $userId) {
      id
      name
    }
  }
`

const variables = { userId: 'abc' }

binding.request(query, variables)
  .then(result => console.log(result))
// sample result:
// {"data": { "user": { "id": "abc", "name": "Sarah" } } }
2.3.0

6 years ago

2.2.6

6 years ago

2.2.5

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.11

6 years ago

2.1.10

6 years ago

2.1.9

6 years ago

2.1.8

6 years ago

2.1.7

6 years ago

2.1.6

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago