Was this helpful?

[//]: # "Weight: 2"

Importing Locations

In certain integrations, collection locations may originate in a remote system, such as an existing website or CRM platform. It may be necessary to consider an import of locations into an Alpaca collection.

To assist this process, this topic covers the use of external-source and external-id attributes. These attributes can assist in the tracking and querying of data based on the identifiers within the source of the import.

Alpaca provides a method to be able to store custom or extended data through attributes. These allow you to push in additional data that can be queried in future calls. The two mentioned attribute ID's are specially recognised, in order to suit the use case of importing data with references to foriegn keys outside of the platform.

Prerequisites

  • Private API Key for querying and mutations

  • The collection ID for where we will be syncing information

Creating a Collection Location with external tracking

When creating a collection location, it may be beneficial to store your own record identifier against the collection location in order to be able to find this record again in the future to test whether the record exists.

1# Creates a collection location with a reference to an external primary key
2# reference, so that we can track ID's to synchronize data between platforms.
3# We leverage a named attribute that is supported for querying items and
4# obtaining locations from a collection based on their external references.
5
6mutation CreateCollectionLocationWithExternalReference {
7  # use the standard createCollectionLocation
8  createCollectionLocation(
9    # Supply the collection to place the location within
10    collectionId: "collection/ABC123"
11    # Supply the collection location
12    location: {
13      # Content about the location
14      title: "Dr. Morse Cafe"
15      # Supply the place data
16      place: {
17        # Supply the place position coordinates (mandatory)
18        position: { lon: 144.99414682388306, lat: -37.79990202116163 }
19        # Optionally relate to a known place, such as a place from ATDW if you
20        # are leveraging place information to be updated automatically without
21        # a re-import.
22        id: "place/atdw:product:5cae80be57a096cd7084b6ab"
23      }
24      # Leverage Attributes to extend the data stored and accessible for the
25      # location
26      attrs: [
27        # Track the ID from my platform into the "custom://external-id". This
28        # is a recognised attribute that can be later used to find the matching
29        # collection location based on your own IDs
30        { id: "custom://external-ref", value: "my-external-UUID" }
31        # Support for external sources, also for grouping together multiple
32        # different sources. Can be used to query all records from a specific
33        # source later using the query collectionItems()
34        { id: "custom://external-source", value: "example.com" }
35      ]
36    }
37  ) {
38    # Confirm the ID that is created
39    location {
40      id
41      __typename
42    }
43  }
44}

Sandbox: Configure | Try Operation

Note: These values are treated as string values. They can contain your own values as you see fit. We recommend the external-source to indicate the source of the data, which could be a label to differentiate multiple sources of data. When you query, you can query by external-source to see records just from that source.

Querying a collection for locations

You can optionally query for collection locations by supplying query criteria using the externalIds or externalSources parameters you have supplied when creating the collection location.

The current value per collection location can also be accessed using the attr(id="custom://external-ref") { value } or attr(id="custom://external-source") { value } attribute selector.

This can make it effective to query the platform for specific entries or to create a map of all values that are present in the platform currently, in order to identify what needs to be sync'd or removed.

1# Queries collection items based on a ID within an external platform. When
2# creating locations, you can set the attributes custom://external-ref and
3# custom://external-source in order to store identifiers and sources of your
4# platform. This enables you to reference and query items through your own
5# ID scheme. External Ref is intended to be used on a 'per record' ID, where
6# External source is to reference the source of all records, and can therefore
7# differentiate multiple sources of data and potentially overlapping IDs
8
9query QueryCollectionLocationWithExternalReference {
10  collectionItems(
11    # Query the collection
12    collectionIds: "collection/ABC123"
13    # Optionally query the external ID's supplied
14    externalIds: ["my-external-UUID", "my-other-external-UUID"]
15    # Optionally query a specific website source
16    externalSources: ["example.com"]
17    # Pagination controls for relay "cursor connections"
18    # See: https://relay.dev/graphql/connections.htm
19    first: 10 #, after: <cursor>
20  ) {
21    edges {
22      node {
23        # Matching collection item
24        id
25        __typename
26
27        # Timestamps
28        created
29        modified
30
31        # Obtain the values for the external refs
32        externalId: attr(id: "custom://external-ref") {
33          value
34        }
35        externalSource: attr(id: "custom://external-source") {
36          value
37        }
38      }
39      # Use the cursor and supply to "after" in operation to paginate
40      cursor
41    }
42    pageInfo {
43      hasNextPage
44    }
45    totalCount
46  }
47}

Sandbox: Configure | Try Operation

Note: The collectionItems query returns a cursor connection in order to paginate through the returned edges. You can read about Paginating using cursor connections

Updating an existing location

If you choose to later associate an external reference to an existing location, you can do so via a mutation similar to below. This mutation will insert or update an attribute value as required.

You can also use the same updateCollectionLocation operation to update your content, such as title, etc.

1# Update a record to store a corresponding identifier in an external system
2
3mutation UpdateCollectionLocationAddExternalReference {
4  # use the updateCollectionItem
5  updateCollectionLocation(
6    # Supply the ID for the record
7    id: "item/ABC123"
8    # Update the location data
9    location: {
10      # Update our title, description or any other content here...
11      # ...
12      # Use upsert to insert/update a record
13      upsertAttrs: [
14        # Supply an external ID for this record
15        { id: "custom/external-ref", value: "my-reference-UUID" }
16      ]
17    }
18  ) {
19    # Query back the location updated
20    location {
21      id
22      __typename
23      # Attribute will now exist on the collection item
24      externalId: attr(id: "custom://external-ref") {
25        value
26      }
27    }
28  }
29}

Sandbox: Configure | Try Operation

Removing a collection location

You can also remove locations from a collection using the following.

1# Removes a collection item from a collection
2
3mutation DeleteCollectionItem {
4  # Use the deleteCollectionItem() operation
5  deleteCollectionItem(
6    # Supply the ID of the record to remove
7    id: "item/ABC123"
8  ) {
9    # select back a response of the ID of the removed item
10    id
11  }
12}

Sandbox: Configure | Try Operation

alpaca.tech

Copyright © 2024 - Made with love ❤️ in Australia.