Example 1: TypeScript
A script to create many features and feature properties for an app.
class GraphqlAPI {
private baseUrl: string;
private apiKey: string;
constructor(baseUrl: string, apiKey: string) {
this.baseUrl = baseUrl;
this.apiKey = apiKey;
}
protected async graphqlRequest(query: string, variables: Record<string, unknown>) {
const headers = {
"Content-Type": "application/json",
Authorization: `Api-Key ${this.apiKey}`,
};
const payload = {
method: "POST",
headers,
body: JSON.stringify({ query, variables }),
};
const response = await fetch(this.baseUrl, payload);
const json = await response.json();
return json.data;
}
}
class TroopMarketplaceAPI extends GraphqlAPI {
async createOneFeature(input: {
appId: string;
title: string;
country?: string;
region?: string;
city?: string;
type?: string;
coordinates?: string;
bbox: number[];
}) {
const result = await this.graphqlRequest(
`mutation CreateOneFeature($input: CreateOneFeatureInput!) {
createOneFeature(input: $input) {
id
appId
created
updated
title
bbox
type
country
region
city
address
coordinates
}
}`,
{ input },
);
return result?.createOneFeature;
}
async createManyFeatureProperties(input: {
appId?: string;
featureId: string;
featureProperties: {
appId?: string;
dataType: string;
name: string;
value: string;
}[];
}) {
const result = await this.graphqlRequest(
`mutation CreateManyFeatureProperties($input: CreateManyFeaturePropertiesInput!) {
createManyFeatureProperties(input: $input) {
count
}
}`,
{ input },
);
return result?.createManyFeatureProperties;
}
async updateMap(appId: string) {
await this.graphqlRequest(
`mutation UpdateTilesetSource($appId: ID!) {
updateTilesetSource(appId: $appId) {
status
message
}
}`,
{ appId },
);
}
}
const importDataIntoApp = async (appId: string, apiKey: string) => {
const troopMarketplaceApi = new TroopMarketplaceAPI("{{TROOP_API_URL}}", apiKey);
const featuresAndFeatureProperties = [
{
title: "Barbados",
country: "BB",
properties: [
{ name: "risk_rating", value: "Low", dataType: "STRING" },
{ name: "source", value: "https://example.com", dataType: "STRING" },
{ name: "legend_title", value: "Low", dataType: "STRING" },
{ name: "fill_color", value: "#FDFE78", dataType: "STRING" },
{ name: "line_color", value: "#CCCCCC", dataType: "STRING" },
],
},
{
title: "Bangladesh",
country: "BD",
properties: [
{ name: "risk_rating", value: "High", dataType: "STRING" },
{ name: "source", value: "https://example.com", dataType: "STRING" },
{ name: "legend_title", value: "High", dataType: "STRING" },
{ name: "fill_color", value: "#F4030E", dataType: "STRING" },
{ name: "line_color", value: "#CCCCCC", dataType: "STRING" },
],
},
{
title: "Belgium",
country: "BE",
properties: [
{ name: "risk_rating", value: "Low", dataType: "STRING" },
{ name: "source", value: "https://example.com", dataType: "STRING" },
{ name: "legend_title", value: "Low", dataType: "STRING" },
{ name: "fill_color", value: "#FDFE78", dataType: "STRING" },
{ name: "line_color", value: "#CCCCCC", dataType: "STRING" },
],
},
{
title: "Burkina Faso",
country: "BF",
properties: [
{ name: "risk_rating", value: "Medium", dataType: "STRING" },
{ name: "source", value: "https://example.com", dataType: "STRING" },
{ name: "legend_title", value: "Medium", dataType: "STRING" },
{ name: "fill_color", value: "#FEB100", dataType: "STRING" },
{ name: "line_color", value: "#CCCCCC", dataType: "STRING" },
],
},
];
for (const feature of featuresAndFeatureProperties) {
const createdFeature = await troopMarketplaceApi.createOneFeature({
appId,
title: feature.title,
country: feature.country,
bbox: [],
});
console.info("Feature '%s' created successfully. ID: %s.", feature.title, createdFeature.id);
const createdFeaturePropertiesResponse = await troopMarketplaceApi.createManyFeatureProperties({
appId,
featureId: createdFeature.id,
featureProperties: feature.properties,
});
console.info("Number of feature properties created: %s.", createdFeaturePropertiesResponse.count);
await troopMarketplaceApi.updateMap(appId);
console.info("Map update scheduled successfully.");
}
};
const API_KEY = "{{YOUR_API_KEY}}";
const APP_ID = "{{YOUR_APP_ID}}";
importDataIntoApp(APP_ID, API_KEY)
.then(() => {
console.info("All done!");
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exit(1);
});
Last updated