-
Notifications
You must be signed in to change notification settings - Fork 32
/
parsing-api-response.liquid
95 lines (73 loc) · 2.67 KB
/
parsing-api-response.liquid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
converter: markdown
metadata:
title: Parsing an API Response
description: This guide will help you parse an API response. In this example, you will store part of the response in a Property for later reference using GraphQL mutations.
---
This guide will help you parse an API response.
In this example, you will store part of the response in a Property for later reference using GraphQL mutations in `callback`, since you are able to access response body via `response.body`.
The tutorial assumes the API returns JSON like:
```json
{
"third_party_api_id": "id-1"
}
```
and you want store the `third_party_api_id` value (which is `id-1`).
## Requirements
To follow the steps in this tutorial, you should understand the concept of users and Properties. You'll use the user profile and API Call Notification created in previous tutorials.
* [Users](/developer-guide/users/users)
* [Properties](/developer-guide/properties/properties)
* [Creating a User Profile](/developer-guide/users/creating-user-profile) (previous tutorial)
* [Creating an API Call Notification](/developer-guide/notifications/creating-api-call-notification) (previous tutorial)
## Steps
Parsing an API response is a four-step process:
<div data-autosteps></div>
### Step 1: Create Property
As a first step, create a Property in the user profile. In this example, you will use `default_profile`.
### Step 2: Add GraphQL mutation
Add GraphQL mutation:
##### app/graphql/store_third_party_api_id.graphql
```graphql
mutation store_third_party_api_id($user_id: ID!, $third_party_api_id: String!) {
user_update(
id: $user_id
user: {
profiles: [
{
name: "default",
values: {
properties: [{ name: "third_party_api_id", value: $third_party_api_id }]
}
}
]
}
form_name: "callback_third_party_api_id"
) {
id
}
}
```
### Step 3: Create Form
The mutation you created provides input for the Form called `callback_third_party_api_id`, so create this Form:
##### app/forms/callback_third_party_api_id.liquid
```yaml
---
resource: User
fields:
profiles:
default:
properties:
third_party_api_id:
---
```
### Step 4: Extend API Call Notification with callback
Extend the API Call Notification by adding the proper callback, which executes the mutation with the input received form the third party API:
##### app/api_calls/ping_example_com_on_user_sign_up.liquid
```liquid
{% raw %}
---
to: 'https://example.com/endpoint/{{ form.id }}'
callback: "{%- assign response_json = response.body | to_hash -%}{% graphql res = 'persist_custom_attribute', id: form.id, third_party_api_id: response_json.third_party_api_id %}"
---
{% endraw %}
```