Skip to content

callstack/benz-ql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

benz-ql

An Apollo GraphQL server for the Mercedes-Benz REST APIs. Allows to query them via a single endpoint, using GraphQL.

query {
  getVehicle(id: "1234567890ABCD1234") {
    licenseplate
    stateofcharge {
      value
    }
    location {
      longitude {
        value
      }
    }
  }
}

Try it now

You can explore the query response by playing around with the online playground.

Getting started

To get started, install the package from the registry:

$ yarn add benz-ql

Configuraton

Setting up the server

This package can be used with different server frameworks supported by Apollo. In this example, we will be using Express.

import express from "express";
import { ApolloServer } from "apollo-server-express";
import benzQL, { scopes } from "benz-ql";

const app = express();
const PORT = 3000;
const server = new ApolloServer(benzQL(scopes.SANDBOX)); // chose your environment - SANDBOX or PROD

server.applyMiddleware({ app, path: "/benz-ql" });

app.listen(PORT, () => {
  console.log(`App listen on ${PORT}`);
  console.log(`Benz-ql avaliable on ${server.graphqlPath}`);
});

Connecting from the client

Once you set up a development server and start it successfuly, you can connect with it by using GraphQL client of your choice. In this section, we are using a apollo-boost package in context of a React Native application.

Note: You will need a token for accessing Mercedes APIs. Please consult official documentation for steps to do so.

Below example demonstrates accessing battery level from the API, including potential error handling.

import React from "react";
import { View, Text } from "react-native";
import ApolloClient, { gql } from "apollo-boost";

const client = new ApolloClient({
  uri: "http://localhost:3000/benz-ql",
  headers: {
    authorization: "a1b2c3d4-a1b2-a1b2-a1b2-a1b2c3d4e5f6"
  }
});

class Home extends React.Component {
  state = { batteryStatus: "unknown" };

  async componentDidMount() {
    const { data, error } = await client.query({
      query: gql`
        query {
          getVehicle(id: "1234567890ABCD1234") {
            stateofcharge {
              value
            }
          }
        }
      `
    });

    if (!error) {
      this.setState({
        batteryStatus: data.getVehicle.stateofcharge.value
      });
    }
  }

  render() {
    const { batteryStatus } = this.state;
    return (
      <View>
        <Text>Battery Status: {batteryStatus}</Text>
      </View>
    );
  }
}

Contributing

Run example server

This project comes with a development server that is useful for debugging and developing the library. In order to get started, follow the steps below:

  1. Clone this repo
  2. yarn install
  3. cd example
  4. node index.js
  5. Open http://localhost:3000

Note: Example server uses sandbox environment

Coverage table

Here is the list of supported APIs. Contributions are welcome to support the remaining list.

API Name API Resource Status
Connected Vehicle (experimental) Vehicles implemented
Tires implemented
Doors implemented
Location implemented
Odometer implemented
Fuel implemented
State of Charge implemented
Car Configurator References pending
Configurations pending
Images pending
Saved configurations pending
Dealer Dealer search pending
References pending
Electric Vehicle Status Container Electric Vehicle Status pending
Resources pending
State of charge resource pending
Range electric resource pending
Fuel Status Container Fuel Status pending
Resources pending
Tank level resource pending
Range liquid resource pending
Pay As You Drive Insurance Container Pay As You Drive Insurance pending
Resources pending
Odometer resource pending
Remote Diagnostic Support Resources pending
Electronical Control Units (ECU's) pending
Diagnostic Trouble Codes (DTC's) pending
Diagnostic Trouble Code (DTC) Snapshots pending
Vehicle Images Vehicle Images Basic pending
Vehicle Images 360 pending
Vehicle Lock Status Resources pending
Door Lock Status Resource pending
Door Lock Deck Lid Status Resource pending
Door Lock Gas Status Resource pending
Position Heading Resource pending
Vehicle Status Container Vehicle Status pending
Resources pending
Decklid resource pending
Front left door resource pending
Front right door resource pending
Rear left door resource pending
Rear right door resource pending
Interior front light resource pending
Interior rear light resource pending
Light switch position resource pending
Front left reading lamp resource pending
Front right reading lamp resource pending
Convertible (roof top) resource pending
Sunroof resource pending
Front left windows resource pending
Front right windows resource pending
Rear left windows resource pending
Rear right windows resource pending

Made with ❤️ at Callstack

This is an open source project and will always remain free to use. If you think it's cool, please star it 🌟. Callstack is a group of React and React Native geeks, contact us at hello@callstack.com if you need any help with these or just want to say hi!