UNPKG

@graphprotocol/graph-cli

Version:

CLI for building for and deploying to The Graph

75 lines (74 loc) 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHostedServiceSubgraphId = exports.chooseNodeUrl = exports.normalizeNodeUrl = exports.validateNodeUrl = exports.SUBGRAPH_STUDIO_URL = void 0; const url_1 = require("url"); const gluegun_1 = require("gluegun"); const constants_1 = require("../constants"); exports.SUBGRAPH_STUDIO_URL = 'https://api.studio.thegraph.com/deploy/'; const HOSTED_SERVICE_URL = 'https://api.thegraph.com/deploy/'; const HOSTED_SERVICE_INDEX_NODE_URL = 'https://api.thegraph.com/index-node/graphql'; const validateNodeUrl = (node) => new url_1.URL(node); exports.validateNodeUrl = validateNodeUrl; const normalizeNodeUrl = (node) => new url_1.URL(node).toString(); exports.normalizeNodeUrl = normalizeNodeUrl; function chooseNodeUrl({ product, studio, node, allowSimpleName, }) { if (node) { try { (0, exports.validateNodeUrl)(node); } catch (e) { gluegun_1.print.error(`Graph node "${node}" is invalid: ${e.message}`); process.exit(1); } } else { if (studio) { product = 'subgraph-studio'; } switch (product) { case 'subgraph-studio': node = exports.SUBGRAPH_STUDIO_URL; break; case 'hosted-service': node = HOSTED_SERVICE_URL; break; } } if (node?.match(/studio/) || product === 'subgraph-studio') { allowSimpleName = true; } return { node, allowSimpleName }; } exports.chooseNodeUrl = chooseNodeUrl; async function getHostedServiceSubgraphId({ subgraphName, }) { const response = await fetch(HOSTED_SERVICE_INDEX_NODE_URL, { method: 'POST', body: JSON.stringify({ query: /* GraphQL */ ` query GraphCli_getSubgraphId($subgraphName: String!) { indexingStatusForCurrentVersion(subgraphName: $subgraphName) { subgraph synced health } } `, variables: { subgraphName, }, }), headers: { 'content-type': 'application/json', ...constants_1.GRAPH_CLI_SHARED_HEADERS, }, }); const { data, errors } = await response.json(); if (errors) { throw new Error(errors[0].message); } if (!data.indexingStatusForCurrentVersion) { throw new Error(`Subgraph "${subgraphName}" not found on the hosted service`); } return data.indexingStatusForCurrentVersion; } exports.getHostedServiceSubgraphId = getHostedServiceSubgraphId;