-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
459 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "apollo", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "nodemon -e ts -x ts-node src/index.ts" | ||
}, | ||
"dependencies": { | ||
"apollo-server-express": "^1.3.5", | ||
"body-parser": "^1.18.2", | ||
"cookie-parser": "^1.4.3", | ||
"cookie-session": "^2.0.0-beta.3", | ||
"dataloader": "^1.4.0", | ||
"dotenv": "^5.0.1", | ||
"express": "^4.16.3", | ||
"express-session": "^1.15.6", | ||
"graphql": "^0.13.2", | ||
"graphql-tools": "^2.24.0", | ||
"jsforce": "^1.8.4", | ||
"passport": "^0.4.0", | ||
"passport-forcedotcom": "^0.1.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^9.6.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { graphiqlExpress, graphqlExpress } from 'apollo-server-express'; | ||
import * as bodyParser from 'body-parser'; | ||
import * as cookieParser from 'cookie-parser'; | ||
import * as cookieSession from 'cookie-session'; | ||
import * as DataLoader from 'dataloader'; | ||
import * as express from 'express'; | ||
import { makeExecutableSchema } from 'graphql-tools'; | ||
import * as jsforce from 'jsforce'; | ||
import * as passport from 'passport'; | ||
import { Strategy } from 'passport-forcedotcom'; | ||
|
||
import { cookieOptions, strategyOptions } from './lib/config'; | ||
import typeDefs from './lib/typeDefs'; | ||
import resolvers from './resolvers'; | ||
|
||
const app = express(); | ||
|
||
passport.use(new Strategy(strategyOptions, (token, _1, _2, done) => done(null, token.params))); | ||
app.use(cookieParser()); | ||
app.use(cookieSession(cookieOptions)); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
|
||
passport.serializeUser((token, done) => done(null, token)); | ||
passport.deserializeUser((token, done) => done(null, token)); | ||
|
||
app.get('/auth/salesforce', passport.authenticate('forcedotcom')); | ||
|
||
app.get( | ||
'/services/oauth2/callback', | ||
passport.authenticate('forcedotcom', { failureRedirect: '/error' }), | ||
(req, res) => res.redirect('/graphiql') | ||
); | ||
|
||
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' })); | ||
|
||
const executeQuery = ({ access_token, instance_url }, query) => | ||
new jsforce.Connection({ | ||
version: '42.0', | ||
accessToken: access_token, | ||
instanceUrl: instance_url, | ||
}) | ||
.query(query) | ||
.then(response => response.records); | ||
|
||
const insert = ({ access_token, instance_url }, sobjectName, data) => | ||
new jsforce.Connection({ | ||
version: '42.0', | ||
accessToken: access_token, | ||
instanceUrl: instance_url, | ||
}) | ||
.sobject(sobjectName) | ||
.create(data); | ||
|
||
app.use('/graphql', bodyParser.json(), (req: any, res, next) => { | ||
const queryLoader = new DataLoader(keys => | ||
Promise.all(keys.map(query => executeQuery(req.user, query))) | ||
); | ||
|
||
const db = { insert: (sobjectName, data) => insert(req.user, sobjectName, data) }; | ||
|
||
return graphqlExpress({ | ||
schema: makeExecutableSchema({ typeDefs: typeDefs, resolvers: resolvers as any }), | ||
context: { user: req.user, loaders: { query: queryLoader }, db }, | ||
})(req, res, next); | ||
}); | ||
|
||
app.listen(3000, () => console.log('Now browse to localhost:3000/graphiql')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require('dotenv').config(); | ||
|
||
export const { | ||
SALESFORCE_USERNAME, | ||
SALESFORCE_PASSWORD, | ||
JWT_SECRET, | ||
CALLBACK_URL, | ||
CLIENT_ID, | ||
CLIENT_SECRET, | ||
} = process.env; | ||
|
||
export const cookieOptions = { | ||
name: 'session', | ||
keys: ['key1'], | ||
maxAge: 24 * 60 * 60 * 1000, | ||
}; | ||
|
||
export const strategyOptions = { | ||
clientID: CLIENT_ID, | ||
clientSecret: CLIENT_SECRET, | ||
scope: ['id', 'chatter_api', 'api'], | ||
callbackURL: CALLBACK_URL, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
export const contactFields = [ | ||
'AccountId', | ||
'AssistantName', | ||
'AssistantPhone', | ||
'Birthdate', | ||
'CreatedById', | ||
'CreatedDate', | ||
'CurrencyIsoCode', | ||
'Department', | ||
'Description', | ||
'Email', | ||
'EmailBouncedDate', | ||
'EmailBouncedReason', | ||
'Fax', | ||
'FirstName', | ||
'HomePhone', | ||
'Id', | ||
'IsDeleted', | ||
'IsEmailBounced', | ||
'Jigsaw', | ||
'JigsawContactId', | ||
'LastActivityDate', | ||
'LastCURequestDate', | ||
'LastCUUpdateDate', | ||
'LastModifiedById', | ||
'LastModifiedDate', | ||
'LastName', | ||
'LastReferencedDate', | ||
'LastViewedDate', | ||
'LeadSource', | ||
'MailingAddress', | ||
'MailingGeocodeAccuracy', | ||
'MasterRecordId', | ||
'MobilePhone', | ||
'Name', | ||
'OtherAddress', | ||
'OtherGeocodeAccuracy', | ||
'OtherPhone', | ||
'OwnerId', | ||
'Phone', | ||
'PhotoUrl', | ||
'ReportsToId', | ||
'Salutation', | ||
'SystemModstamp', | ||
'Title', | ||
]; | ||
export const accountFields = [ | ||
'AccountNumber', | ||
'AccountSource', | ||
'AnnualRevenue', | ||
'BillingAddress', | ||
'BillingGeocodeAccuracy', | ||
'CreatedById', | ||
'CreatedDate', | ||
'CurrencyIsoCode', | ||
'Description', | ||
'Fax', | ||
'Id', | ||
'Industry', | ||
'IsCustomerPortal', | ||
'IsDeleted', | ||
'IsPartner', | ||
'Jigsaw', | ||
'JigsawCompanyId', | ||
'LastActivityDate', | ||
'LastModifiedById', | ||
'LastModifiedDate', | ||
'LastReferencedDate', | ||
'LastViewedDate', | ||
'MasterRecordId', | ||
'Name', | ||
'NumberOfEmployees', | ||
'OwnerId', | ||
'Ownership', | ||
'ParentId', | ||
'Phone', | ||
'PhotoUrl', | ||
'Rating', | ||
'RecordTypeId', | ||
'ShippingAddress', | ||
'ShippingGeocodeAccuracy', | ||
'Sic', | ||
'SicDesc', | ||
'Site', | ||
'SystemModstamp', | ||
'TickerSymbol', | ||
'Type', | ||
'Website', | ||
]; |
Oops, something went wrong.