Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 6aa378d

Browse files
committed
Add custom getter for form
1 parent c64a57d commit 6aa378d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {initializeApollo} from '../connector'
2+
import queryFormById from './queryFormById'
3+
4+
/**
5+
* Retrieve single form by ID.
6+
*
7+
* @param {string} id Form ID.
8+
* @return {Object} Post data or error object.
9+
*/
10+
export default async function getFormById(id) {
11+
// Determine form global ID.
12+
const formId = Buffer.from(`GravityFormsForm:${id}`).toString('base64')
13+
14+
// Get/create Apollo instance.
15+
const apolloClient = initializeApollo()
16+
17+
// Execute query.
18+
const form = await apolloClient
19+
.query({query: queryFormById, variables: {id: formId}})
20+
.then((form) => form?.data?.gravityFormsForm ?? null)
21+
.catch((error) => {
22+
return {
23+
isError: true,
24+
message: error.message
25+
}
26+
})
27+
28+
if (!form) {
29+
return {
30+
isError: true,
31+
message: `An error occurred while trying to retrieve data for form "${id}."`
32+
}
33+
}
34+
35+
return form
36+
}

0 commit comments

Comments
 (0)