Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpmonette/salesforce-graphql
Bringing the GraphQL query language to Salesforce ☁️
https://github.com/jpmonette/salesforce-graphql
graphql react salesforce soql typescript
Last synced: 5 days ago
JSON representation
Bringing the GraphQL query language to Salesforce ☁️
- Host: GitHub
- URL: https://github.com/jpmonette/salesforce-graphql
- Owner: jpmonette
- License: mit
- Created: 2018-04-12T17:13:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-04-23T15:32:02.000Z (over 6 years ago)
- Last Synced: 2024-09-19T19:11:14.318Z (about 2 months ago)
- Topics: graphql, react, salesforce, soql, typescript
- Language: TypeScript
- Homepage:
- Size: 425 KB
- Stars: 49
- Watchers: 6
- Forks: 7
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
salesforce-graphql
- Bringing the GraphQL query language to Salesforce## Getting Started
### Installation
```sh
$ yarn add salesforce-graphql jsforce
```### Example
#### `app.ts`
```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`
```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: StringContacts(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:
```sh
$ yarn start
```Head over to `http://localhost:4000/playground` to test with the following query:
```graphql
{
Account(Id: "001E000001KnMkTIAV") {
Id
Name
Contacts(limit: 1) {
Name
AccountId
Account {
Name
}
}
}
}
```![Sample Output](assets/output.png)
## TODO
* Subscriptions
* Mutations
* Basically everything## References
- [`salesforce-graphql` on NPM](https://www.npmjs.com/package/salesforce-graphql)
- Learn more about [GraphQL](http://graphql.org/)
- [Salesforce REST API](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm) documentation## Extra
- Looking for [new opportunities](https://mavens.com/careers/)? Have a look at [Mavens](https://mavens.com/) website!