Code Monkey home page Code Monkey logo

salesforce-graphql's Introduction

salesforce graphql
Build Status Coverage Status

salesforce-graphql - Bringing the GraphQL query language to Salesforce

Getting Started

Installation

$ yarn add salesforce-graphql jsforce

Example

app.ts

import * as jsforce from 'jsforce';
import * as path from 'path';
import * as fs from 'fs';

import { GraphQLServer } from 'graphql-yoga';
import { Binding } from 'salesforce-graphql';

const schemaFile = path.join(__dirname, 'schema.graphql');
const typeDefs = fs.readFileSync(schemaFile, 'utf8');

const { USERNAME, PASSWORD } = process.env;

const resolvers = {
  Query: {
    Accounts: (parent, args, context, info) =>
      context.db.query({}, info).then(res => res.records),
    Account: (parent, args, context, info) =>
      context.db.query({}, info).then(res => res.records[0]),
    Contacts: (parent, args, context, info) =>
      context.db.query({}, info).then(res => res.records),
    Contact: (parentobj, args, context, info) =>
      context.db.query({}, info).then(res => res.records[0]),
  },
  Account: {
    Contacts: (parent, args, context, info) =>
      context.db.query({ AccountId: parent.Id }, info).then(res => res.records),
  },
  Contact: {
    Account: (parent, args, context, info) =>
      context.db.query({ Id: parent.AccountId }, info).then(res => res.records[0]),
  },
};

const conn = new jsforce.Connection({});

function init() {
  const db = new Binding({ conn });

  const server = new GraphQLServer({
    typeDefs,
    resolvers,
    context: req => ({ ...req, db }),
  });

  server.start({ playground: '/playground' }, ({ port }) =>
    console.log('Server is running on localhost:' + port)
  );
}

conn.login(USERNAME, PASSWORD, (err, userinfo) => init());

schema.graphql

type Query {
  Account(Id: ID!): Account
  Accounts(limit: Int): [Account]
  Contact(Id: ID!): Contact
  Contacts(limit: Int): [Contact]
}

type Account {
  Id: ID!
  IsDeleted: Boolean
  Name: String
  Type: String

  Contacts(limit: Int): [Contact]
}

type Contact {
  Id: ID!
  Account: Account
  AccountId: String
  LastName: String
  FirstName: String
  Salutation: String
  Name: String
}

When you are ready, start the GraphQL server:

$ yarn start

Head over to http://localhost:4000/playground to test with the following query:

{
  Account(Id: "001E000001KnMkTIAV") {
    Id
    Name
    Contacts(limit: 1) {
      Name
      AccountId
      Account {
        Name
      }
    }
  }
}

Sample Output

TODO

  • Subscriptions
  • Mutations
  • Basically everything

References

Extra

salesforce-graphql's People

Contributors

jpmonette avatar

Watchers

James Cloos avatar Mark Masterson 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.