Giter VIP home page Giter VIP logo

node-red-contrib-graphql's Introduction

node-red-contrib-graphql

Platform Release NPM

A NodeRed node to execute GraphQL Queries.

Install

Run command on Node-RED installation directory.

npm install node-red-contrib-graphql

Change Log

Vers Changes
1.3.0 bump axios to address CVE-2021-3749
1.2.0 Fix node not showing in palette, bump axios
1.1.0 Error Handling & Config Templates, showDebug & customHeaders
1.0.1 Bump axios
1.0.0 pass Authorization via msg.authorization, PR #21

GraphQL Node

Provides a GraphQL node to support queries and a supporting Configuration node to point to a GraphQL server.

Node Fields

Name Use
GraphQL Endpoint URL to the endpoint
Query Query or Mutation template

Template flavors and uses

There are two template flavors:

  1. Plain
  2. Mustache

At the bottom of the template text area, you must select between plain or mustache template.

If you select mustache, your template will be processed by Mustache with the message's payload as an argument. I.e.

submitted_template = mustache("template in text area", msg.payload)

If you select plain, the template is left as it is.

Template variables

You can add GraphQL query variables to the submitted query by defining them in the msg.variables property. Your variables will be passed over to the GraphQL query.

For exemple, if you define

type Response {
  ok: boolean
}

input doSomethingInput {
  myVar: String
}

type Mutation {
  doSomething(input: doSomethingInput!): Response
}

you can pass the messageInput parameter as such in Node-Red msg:

msg.variables = {
  "input": {
    "myVar": "myValue"
  }
}

it will be added to the GraphQL query:

query: `mutation doSomething($input: messageInput!) {
  doSomething(input: $input) {
    ok
  }
}`,
variables: {
  input: {
    myVar: "myValue"
  }
}

When using a scalar type like JSON, the entire payload can conveniently be passed as an input parameter:

scalar JSON

type Response {
  ok: boolean
}

input payloadInput {
  payload: JSON
}

type Mutation {
  doSomething(input: payloadInput!): Response
}

In node-red flow, prepare payloadInput variables:

msg.variables = {
  "input": {
    "payload": msg.payload
  }
}

which will results in

query: `mutation doSomething($input: payloadInput!) {
  doSomething(input: $input) {
    ok
  }
}`,
variables: {
  input: {
    myVar: { whatever: "was in you msg.payload", val: 5, bool: true }
  }
}

The execution will return the value in:

msg.payload.doSomething

object.

Outputs

payload is loaded with the output of the Query or Mutation. If the Query is named doSomething, the results of the query will be in payload.doSomething.

//msg.payload is:
{
  doSomething: {
    ok: true
  }
}

ToDo's

  • Add support for authentication and a token
  • Test Mutations

Installing and using the Example Flow

This example flow uses the node-red-contrib-graphql node to query the Deutsche Bahn GraphQL service and get a station address and details on the next departure.

The example flow is in the file deutscheBahnFlow.json. Import this file from the clipboad under the NodeRed menu Import > Clipboard. You'll drag the example flow onto NodeRed.

Example Flow

This is the result sent to the debug window.

Example Flow Output

Example Queries

Here's a list of public GraphQL API's

Deutsche Bahn

Endpoint: https://developer.deutschebahn.com/free1bahnql/graphql

{
   search(searchTerm: "Herrenberg") {
     stations {
       name
       stationNumber
       primaryEvaId
     }
     operationLocations {
       name
       id
       regionId
       abbrev
       locationCode
     }
   }
}
{
  stationWithEvaId(evaId: 8004168) {
    name
  }
}
{
   stationWithStationNumber(stationNumber: 6071) {
    name
    mailingAddress {
      street
      city
      zipcode
    }
    federalState
    location {
      latitude
      longitude
    }
    szentrale {
      name
      email
      number
      phoneNumber
    }
    hasParking
    timetable {
      nextDepatures {
        type
        trainNumber
        platform
        time
        stops
      }
    }
    hasWiFi
    hasParking
  }
}
{
   stationWithStationNumber(stationNumber: 2726) {
    name
    mailingAddress {
      street
      city
      zipcode
    }
    federalState
    regionalArea {
      name
    }
    szentrale {
      name
      email
      number
      phoneNumber
    }
    hasWiFi
    hasParking
   }
}

getGolferById

{
  getGolferById(id: "3315181") {
    ghinNum
    firstName
    lastName
    state
    handicapIndex
    trend
  }
}

Template to grab GHIN Number:

This is the GHIN Number: {{payload.getGolferById.ghinNum}} 

getGolfers

{
    getGolfers(state: "WA", lastName: "Stephens", firstName: "M") {
        golferCount
        golfers {
          ghinNum
          firstName
          lastName
          trend
          handicapIndex
          email
          address1
          address2
          city
          state
          clubName
        }
    }
}

node-red-contrib-graphql's People

Contributors

adamgoose avatar dependabot[bot] avatar derfrickler avatar moritz89 avatar pbassham avatar rgstephens avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.