2727// more info: https://www.useanvil.com/docs/api/getting-started#terminology
2828
2929using Anvil . Client ;
30+ using Anvil . Payloads . Request ;
31+ using Anvil . Payloads . Response ;
32+ using Newtonsoft . Json ;
33+ using Newtonsoft . Json . Linq ;
3034
31- namespace AnvilExamples ;
35+ namespace AnvilExamples . examples ;
3236
3337class CreateUpdateWorkflowSubmission : RunnableBaseExample
3438{
35- public CreateUpdateWorkflowSubmission ( string apiKey ) : base ( apiKey )
39+ private async Task < JObject > GetWeld ( GraphQLClient client , string organizationSlug , string weldSlug )
3640 {
41+ // Ref docs:
42+ // https://www.useanvil.com/docs/api/graphql/reference/#operation-weld-Queries
43+ var weldQuery = @"
44+ query WeldQuery (
45+ $eid: String,
46+ $organizationSlug: String,
47+ $slug: String
48+ ) {
49+ weld (
50+ eid: $eid,
51+ organizationSlug: $organizationSlug,
52+ slug: $slug
53+ ) {
54+ eid
55+ name
56+ forges {
57+ eid
58+ slug
59+ name
60+ }
61+ }
62+ }
63+ " ;
64+
65+ var variables = new { slug = weldSlug , organizationSlug } ;
66+ return await client . SendQuery ( weldQuery , variables ) ;
67+ }
68+
69+ private async Task < ForgeSubmitPayload > SubmitToWorkflowWebform ( GraphQLClient client , ForgeSubmit payload )
70+ {
71+ var responseQuery = @"{
72+ eid
73+ createdAt
74+ updatedAt
75+ resolvedPayload
76+ weldData {
77+ eid
78+ displayTitle
79+ isTest
80+ createdAt
81+ updatedAt
82+ }
83+ }" ;
84+
85+ // Build the payload for the webform submission
86+ return await client . ForgeSubmit ( payload ) ;
87+ }
88+
89+ private object BuildWorkflowSubmissionDetailsUrl ( string organizationSlug , string weldSlug , string weldDataEid )
90+ {
91+ return $ "https://app.useanvil.com/org/{ organizationSlug } /w/{ weldSlug } /{ weldDataEid } ";
3792 }
3893
3994 public override async Task Run ( string apiKey , string otherArg )
4095 {
4196 var client = new GraphQLClient ( apiKey ) ;
97+ var organizationSlug = otherArg ;
98+ var weldSlug = "sample-workflow" ;
99+
100+ //
101+ // Find the workflow
102+ //
103+
104+ // Workflows are 'Weld' objects in Anvil's system
105+ Console . WriteLine ( $ ">>> Fetching Weld { organizationSlug } /{ weldSlug } ") ;
106+ var response = await GetWeld ( client , organizationSlug , weldSlug ) ;
107+ var weld = response [ "weld" ] ;
108+
109+ Console . WriteLine ( $ "Found weld: { weld [ "eid" ] } ") ;
110+
111+ //
112+ // Start the workflow
113+ //
114+
115+ // Now we get the workflow's first webform to start the workflow
116+ // Webforms are `Forge` objects in Anvil's system
117+ var startForge = weld [ "forges" ] [ 0 ] ;
118+ Console . WriteLine ( ">>> Starting workflow with webform" ) ;
119+ Console . WriteLine ( startForge ) ;
120+
121+ // Start the workflow by submitting data to the webform of your choice. You
122+ // will receive a new Submission object and a new WeldData.
123+ var startPayload = new ForgeSubmit
124+ {
125+ IsTest = true ,
126+ ForgeEid = ( string ? ) startForge [ "eid" ] ,
127+ Payload = new
128+ {
129+ shortText = $ "Workflow start! { DateTime . Now } "
130+ }
131+ } ;
132+ var submission = await SubmitToWorkflowWebform ( client , startPayload ) ;
133+ var weldData = ( JObject ) submission . ForgeSubmit . WeldData ;
134+
135+ // We have the newly created objects
136+ var detailsURL =
137+ BuildWorkflowSubmissionDetailsUrl ( organizationSlug , weldSlug , weldDataEid : ( string ) weldData [ "eid" ] ) ;
138+
139+ Console . WriteLine ( "Workflow started" ) ;
140+ Console . WriteLine ( $ "View on your daskboard { detailsURL } ") ;
141+ Console . WriteLine ( $ "Submission eid: { submission . ForgeSubmit . Eid } , WeldData eid: { weldData [ "eid" ] } ") ;
142+ Console . WriteLine ( JsonConvert . SerializeObject ( submission ) ) ;
143+
144+ //
145+ // Update the webform's submission data
146+ //
147+
148+ // Updating the submission uses the same mutation, but you just specify a
149+ // submission and weldData information. You can add more data or change
150+ // payload data
151+ Console . WriteLine ( ">>> Updating the submission..." ) ;
152+
153+ var updatePayload = new ForgeSubmit
154+ {
155+ IsTest = true ,
156+ ForgeEid = ( string ? ) startForge [ "eid" ] ,
157+ WeldDataEid = ( string ) weldData [ "eid" ] ,
158+ SubmissionEid = submission . ForgeSubmit . Eid ,
159+ Payload = new
160+ {
161+ // We'll update new fields, but you can also overwrite existing data
162+ name = new { firstName = "Sally" , lastName = "Jones" } ,
163+ 164+ }
165+ } ;
166+ var updatedSubmission = await SubmitToWorkflowWebform ( client , updatePayload ) ;
42167
43- var thing = await client . SendQuery ( ) ;
168+ // We have the new objects
169+ Console . WriteLine ( "Submission updated!" ) ;
170+ Console . WriteLine ( JsonConvert . SerializeObject ( updatedSubmission . ForgeSubmit ) ) ;
44171 }
45172}
0 commit comments