项目作者: jpmonette

项目描述 :
Bringing the GraphQL query language to Salesforce ☁️
高级语言: TypeScript
项目地址: git://github.com/jpmonette/salesforce-graphql.git
创建时间: 2018-04-12T17:13:11Z
项目社区:https://github.com/jpmonette/salesforce-graphql

开源协议:MIT License

下载



salesforce graphql


Build Status Coverage Status

salesforce-graphql - Bringing the GraphQL query language to Salesforce

Getting Started

Installation

  1. $ yarn add salesforce-graphql jsforce

Example

app.ts

  1. import * as jsforce from 'jsforce';
  2. import * as path from 'path';
  3. import * as fs from 'fs';
  4. import { GraphQLServer } from 'graphql-yoga';
  5. import { Binding } from 'salesforce-graphql';
  6. const schemaFile = path.join(__dirname, 'schema.graphql');
  7. const typeDefs = fs.readFileSync(schemaFile, 'utf8');
  8. const { USERNAME, PASSWORD } = process.env;
  9. const resolvers = {
  10. Query: {
  11. Accounts: (parent, args, context, info) =>
  12. context.db.query({}, info).then(res => res.records),
  13. Account: (parent, args, context, info) =>
  14. context.db.query({}, info).then(res => res.records[0]),
  15. Contacts: (parent, args, context, info) =>
  16. context.db.query({}, info).then(res => res.records),
  17. Contact: (parentobj, args, context, info) =>
  18. context.db.query({}, info).then(res => res.records[0]),
  19. },
  20. Account: {
  21. Contacts: (parent, args, context, info) =>
  22. context.db.query({ AccountId: parent.Id }, info).then(res => res.records),
  23. },
  24. Contact: {
  25. Account: (parent, args, context, info) =>
  26. context.db.query({ Id: parent.AccountId }, info).then(res => res.records[0]),
  27. },
  28. };
  29. const conn = new jsforce.Connection({});
  30. function init() {
  31. const db = new Binding({ conn });
  32. const server = new GraphQLServer({
  33. typeDefs,
  34. resolvers,
  35. context: req => ({ ...req, db }),
  36. });
  37. server.start({ playground: '/playground' }, ({ port }) =>
  38. console.log('Server is running on localhost:' + port)
  39. );
  40. }
  41. conn.login(USERNAME, PASSWORD, (err, userinfo) => init());

schema.graphql

  1. type Query {
  2. Account(Id: ID!): Account
  3. Accounts(limit: Int): [Account]
  4. Contact(Id: ID!): Contact
  5. Contacts(limit: Int): [Contact]
  6. }
  7. type Account {
  8. Id: ID!
  9. IsDeleted: Boolean
  10. Name: String
  11. Type: String
  12. Contacts(limit: Int): [Contact]
  13. }
  14. type Contact {
  15. Id: ID!
  16. Account: Account
  17. AccountId: String
  18. LastName: String
  19. FirstName: String
  20. Salutation: String
  21. Name: String
  22. }

When you are ready, start the GraphQL server:

  1. $ yarn start

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

  1. {
  2. Account(Id: "001E000001KnMkTIAV") {
  3. Id
  4. Name
  5. Contacts(limit: 1) {
  6. Name
  7. AccountId
  8. Account {
  9. Name
  10. }
  11. }
  12. }
  13. }

Sample Output

TODO

  • Subscriptions
  • Mutations
  • Basically everything

References

Extra