├── .gitignore ├── .jestrc ├── .npmignore ├── .prettierrc.yaml ├── .travis.yml ├── .vscode └── settings.json ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE ├── README.md ├── assets ├── output.png └── title.png ├── package.json ├── src └── index.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | build 3 | coverage 4 | node_modules 5 | yarn.lock 6 | -------------------------------------------------------------------------------- /.jestrc: -------------------------------------------------------------------------------- 1 | { 2 | "transform": { 3 | "^.+\\.tsx?$": "ts-jest" 4 | }, 5 | "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json"], 6 | "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$" 7 | } 8 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .env 2 | .travis.yml 3 | coverage 4 | src 5 | tsconfig.json 6 | tslint.json 7 | yarn.lock 8 | -------------------------------------------------------------------------------- /.prettierrc.yaml: -------------------------------------------------------------------------------- 1 | arrowParens: "avoid" 2 | bracketSpacing: true 3 | jsxBracketSameLine: false 4 | printWidth: 100 5 | semi: true 6 | singleQuote: true 7 | tabWidth: 2 8 | trailingComma: "es5" 9 | useTabs: false 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: "node" 3 | cache: 4 | yarn: true 5 | directories: 6 | - node_modules 7 | after_success: 8 | - npm run coveralls -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true 3 | } 4 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at contact@jpmonette.net. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Pull Requests 2 | 3 | When creating a pull-request you should: 4 | 5 | - __Open an issue first:__ Confirm that the change or feature will be accepted 6 | - __Lint your code:__ Use `prettier` to clean up your code 7 | - __Start message with a verb:__ Your commit message must start a lowercase verb such as "add", "fix", "refactor", "remove" 8 | - __Reference the issue__: Ensure that your commit message references the issue with ". Closes #N" 9 | - __Add to feature list__: If your pull-request is for a feature, make sure to add it to the Readme's feature list 10 | - __Add a GIF__ 11 | 12 | # Running Tests 13 | To run the test for the project: 14 | 15 | $ yarn test 16 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Expected behaviour 2 | 3 | 4 | ### Actual behaviour 5 | 6 | 7 | ### Steps to reproduce 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jean-Philippe Monette 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | salesforce graphql 3 |
4 | Build Status Coverage Status 5 |

6 | 7 |

salesforce-graphql - Bringing the GraphQL query language to Salesforce

8 | 9 | ## Getting Started 10 | 11 | ### Installation 12 | 13 | ```sh 14 | $ yarn add salesforce-graphql jsforce 15 | ``` 16 | 17 | ### Example 18 | 19 | #### `app.ts` 20 | 21 | ```ts 22 | import * as jsforce from 'jsforce'; 23 | import * as path from 'path'; 24 | import * as fs from 'fs'; 25 | 26 | import { GraphQLServer } from 'graphql-yoga'; 27 | import { Binding } from 'salesforce-graphql'; 28 | 29 | const schemaFile = path.join(__dirname, 'schema.graphql'); 30 | const typeDefs = fs.readFileSync(schemaFile, 'utf8'); 31 | 32 | const { USERNAME, PASSWORD } = process.env; 33 | 34 | const resolvers = { 35 | Query: { 36 | Accounts: (parent, args, context, info) => 37 | context.db.query({}, info).then(res => res.records), 38 | Account: (parent, args, context, info) => 39 | context.db.query({}, info).then(res => res.records[0]), 40 | Contacts: (parent, args, context, info) => 41 | context.db.query({}, info).then(res => res.records), 42 | Contact: (parentobj, args, context, info) => 43 | context.db.query({}, info).then(res => res.records[0]), 44 | }, 45 | Account: { 46 | Contacts: (parent, args, context, info) => 47 | context.db.query({ AccountId: parent.Id }, info).then(res => res.records), 48 | }, 49 | Contact: { 50 | Account: (parent, args, context, info) => 51 | context.db.query({ Id: parent.AccountId }, info).then(res => res.records[0]), 52 | }, 53 | }; 54 | 55 | const conn = new jsforce.Connection({}); 56 | 57 | function init() { 58 | const db = new Binding({ conn }); 59 | 60 | const server = new GraphQLServer({ 61 | typeDefs, 62 | resolvers, 63 | context: req => ({ ...req, db }), 64 | }); 65 | 66 | server.start({ playground: '/playground' }, ({ port }) => 67 | console.log('Server is running on localhost:' + port) 68 | ); 69 | } 70 | 71 | conn.login(USERNAME, PASSWORD, (err, userinfo) => init()); 72 | ``` 73 | 74 | #### `schema.graphql` 75 | 76 | ```graphql 77 | type Query { 78 | Account(Id: ID!): Account 79 | Accounts(limit: Int): [Account] 80 | Contact(Id: ID!): Contact 81 | Contacts(limit: Int): [Contact] 82 | } 83 | 84 | type Account { 85 | Id: ID! 86 | IsDeleted: Boolean 87 | Name: String 88 | Type: String 89 | 90 | Contacts(limit: Int): [Contact] 91 | } 92 | 93 | type Contact { 94 | Id: ID! 95 | Account: Account 96 | AccountId: String 97 | LastName: String 98 | FirstName: String 99 | Salutation: String 100 | Name: String 101 | } 102 | ``` 103 | 104 | When you are ready, start the GraphQL server: 105 | 106 | ```sh 107 | $ yarn start 108 | ``` 109 | 110 | Head over to `http://localhost:4000/playground` to test with the following query: 111 | 112 | ```graphql 113 | { 114 | Account(Id: "001E000001KnMkTIAV") { 115 | Id 116 | Name 117 | Contacts(limit: 1) { 118 | Name 119 | AccountId 120 | Account { 121 | Name 122 | } 123 | } 124 | } 125 | } 126 | ``` 127 | 128 | ![Sample Output](assets/output.png) 129 | 130 | ## TODO 131 | 132 | * Subscriptions 133 | * Mutations 134 | * Basically everything 135 | 136 | ## References 137 | 138 | - [`salesforce-graphql` on NPM](https://www.npmjs.com/package/salesforce-graphql) 139 | - Learn more about [GraphQL](http://graphql.org/) 140 | - [Salesforce REST API](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm) documentation 141 | 142 | ## Extra 143 | 144 | - Looking for [new opportunities](https://mavens.com/careers/)? Have a look at [Mavens](https://mavens.com/) website! -------------------------------------------------------------------------------- /assets/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jpmonette/salesforce-graphql/9af50cb10ab5b43519f27406761dbd808fb9bc3e/assets/output.png -------------------------------------------------------------------------------- /assets/title.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jpmonette/salesforce-graphql/9af50cb10ab5b43519f27406761dbd808fb9bc3e/assets/title.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "salesforce-graphql", 3 | "author": "Jean-Philippe Monette (http://jpmonette.net/)", 4 | "bugs": "https://github.com/jpmonette/salesforce-graphql/issues", 5 | "repository": "jpmonette/salesforce-graphql.git", 6 | "homepage": "http://github.com/jpmonette/salesforce-graphql", 7 | "version": "0.0.3", 8 | "main": "build/index.js", 9 | "license": "MIT", 10 | "scripts": { 11 | "coveralls": "cat ./coverage/lcov.info | coveralls", 12 | "build": "rm -rf build && tsc", 13 | "prepare": "yarn build", 14 | "test": "jest --coverage" 15 | }, 16 | "devDependencies": { 17 | "@types/graphql": "^0.13.0", 18 | "@types/jest": "^22.2.3", 19 | "coveralls": "^3.0.0", 20 | "jest": "^22.4.3", 21 | "ts-jest": "^22.4.2", 22 | "typescript": "^2.8.1" 23 | }, 24 | "dependencies": { 25 | "salesforce-queries": "^0.0.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { SOQL } from 'salesforce-queries'; 2 | 3 | function getFields(info: any) { 4 | const fields: Set = new Set([]); 5 | info.fieldNodes.map((fieldNode: any) => { 6 | if (fieldNode.selectionSet) { 7 | fieldNode.selectionSet.selections.map((value: any) => { 8 | if (!value.selectionSet) fields.add(value.name.value); 9 | }); 10 | } 11 | }); 12 | return [...fields]; 13 | } 14 | 15 | function getWheres(info: any): any[] { 16 | const wheres: { field: string; value: string; operator: string }[] = []; 17 | info.fieldNodes.map((fieldNode: any) => { 18 | fieldNode.arguments.map((val: any) => { 19 | const field = val.name.value; 20 | const value = val.value.value; 21 | if (field !== 'limit') { 22 | wheres.push({ field, value, operator: '=' }); 23 | } 24 | }); 25 | }); 26 | return wheres; 27 | } 28 | 29 | function getLimit(info: any): number | void { 30 | let limit; 31 | info.fieldNodes.map((fieldNode: any) => { 32 | fieldNode.arguments.map((value: any) => { 33 | if (value.name.value === 'limit') { 34 | limit = value.value.value; 35 | } 36 | }); 37 | }); 38 | return limit; 39 | } 40 | 41 | export interface Options { 42 | conn: any; 43 | } 44 | 45 | class Salesforce { 46 | conn: any; 47 | 48 | constructor(props: Options) { 49 | this.conn = props.conn; 50 | } 51 | 52 | public query = (parent: { [key: string]: string }, info: any) => { 53 | const queryBuilder = new SOQL(info.returnType.ofType || info.returnType).select( 54 | getFields(info) 55 | ); 56 | const limit = getLimit(info); 57 | 58 | if (limit) { 59 | queryBuilder.limit(limit); 60 | } 61 | 62 | const wheres = getWheres(info); 63 | 64 | wheres.map(({ field, operator, value }) => queryBuilder.where(field, operator, value)); 65 | Object.keys(parent).map((key: string) => queryBuilder.where(key, '=', parent[key])); 66 | const query = queryBuilder.build(); 67 | return this.conn.query(query); 68 | }; 69 | } 70 | 71 | export { Salesforce }; 72 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "outDir": "build", 5 | "target": "es2016", 6 | "declaration": true, 7 | "alwaysStrict": true, 8 | "sourceMap": true, 9 | "noImplicitAny": true, 10 | "noImplicitReturns": true, 11 | "noImplicitThis": true, 12 | "noUnusedLocals": true, 13 | "noUnusedParameters": true, 14 | "strictFunctionTypes": true, 15 | "strictPropertyInitialization": true, 16 | "strictNullChecks": true, 17 | "lib": ["esnext","dom"] 18 | }, 19 | "include": ["src/**/*"], 20 | "exclude": ["node_modules", "**/node_modules/*"] 21 | } 22 | --------------------------------------------------------------------------------