1414$ yarn add salesforce-graphql jsforce
1515```
1616
17- ### Example
17+ ## Examples
1818
19- #### ` app.ts `
19+ Refer to the ` examples ` folder for examples:
2020
21- ``` ts
22- import * as jsforce from ' jsforce' ;
23- import * as path from ' path' ;
24- import * as fs from ' fs' ;
21+ > https://github.com/jpmonette/salesforce-graphql/tree/master/examples/apollo .
2522
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- ```
23+ ## Sample GraphIQL Output
12724
12825![ Sample Output] ( assets/output.png )
12926
@@ -135,10 +32,10 @@ Head over to `http://localhost:4000/playground` to test with the following query
13532
13633## References
13734
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
35+ * [ ` salesforce-graphql ` on NPM] ( https://www.npmjs.com/package/salesforce-graphql )
36+ * Learn more about [ GraphQL] ( http://graphql.org/ )
37+ * [ Salesforce REST API] ( https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm ) documentation
14138
14239## Extra
14340
144- - Looking for [ new opportunities] ( https://mavens.com/careers/ ) ? Have a look at [ Mavens] ( https://mavens.com/ ) website!
41+ * Looking for [ new opportunities] ( https://mavens.com/careers/ ) ? Have a look at [ Mavens] ( https://mavens.com/ ) website!
0 commit comments