firebase-tools
Version:
Command-Line Interface for Firebase
89 lines (83 loc) • 4.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.execute_in_emulator = void 0;
const zod_1 = require("zod");
const tool_1 = require("../../tool");
const dataplane = require("../../../dataconnect/dataplaneClient");
const load_1 = require("../../../dataconnect/load");
const converter_1 = require("../../util/dataconnect/converter");
const emulator_1 = require("../../util/dataconnect/emulator");
exports.execute_in_emulator = (0, tool_1.tool)("dataconnect", {
name: "execute_in_emulator",
description: `Executes a GraphQL operation against a local Firebase SQL Connect Service instance emulator.
Grants access to run queries and mutations on the local emulator.
**Prerequisites:**
* The Firebase local emulators must be running (run \`firebase emulators:start\` in your project directory).
* A \`firebase.json\` file configured with a \`dataconnect\` service.
* A \`dataconnect.yaml\` configuration file inside the service's source directory.
* A defined SQL Connect schema (configured via the schema/schemas fields in \`dataconnect.yaml\`).
* Note: These files are ideally generated using the \`firebase_init\` MCP tool, or must follow the standard structure described in the \`firebase-data-connect-basics\` skill.
**When to use it:**
* Use this tool to execute local GraphQL queries or mutations for development and testing.
**How to use it:**
* Call \`execute_in_emulator\` with the GraphQL \`query\` string.
* Optionally provide \`service_id\`, \`location_id\`, \`variables_json\`, and \`auth_token_json\`.
**JSON Example:**
\`\`\`json
{
"query": "query GetUser($id: UUID!) { user(id: $id) { name } }",
"variables_json": "{\\"id\\": \\"123e4567-e89b-12d3-a456-426614174000\\"}"
}
\`\`\`
`,
humanReadableDescription: "Execute a GraphQL query or mutation against a Firebase SQL Connect service or emulator.",
inputSchema: zod_1.z.object({
query: zod_1.z.string().describe(`A Firebase SQL Connect GraphQL query or mutation to execute.
Example SQL Connect schema and example queries can be found in files ending in \`.graphql\` or \`.gql\`.
`),
service_id: zod_1.z
.string()
.optional()
.describe(`Service ID of the SQL Connect service to compile. Used to disambiguate when there are multiple SQL Connect services in firebase.json.`),
location_id: zod_1.z
.string()
.optional()
.describe(`SQL Connect Service location ID to disambiguate among multiple SQL Connect services.`),
variables_json: zod_1.z
.string()
.optional()
.describe("GraphQL variables to pass into the query. MUST be a valid stringified JSON object."),
auth_token_json: zod_1.z
.string()
.optional()
.describe("Firebase Auth Token JWT to use in this query. MUST be a valid stringified JSON object." +
'Importantly, when executing queries with `@auth(level: USER)` or `auth.uid`, a valid Firebase Auth Token JWT with "sub" field is required. ' +
'"auth.uid" expression in the query evaluates to the value of "sub" field in Firebase Auth token.'),
}),
annotations: {
title: "Execute Firebase SQL Connect Query in Emulator",
},
_meta: {
requiresProject: true,
requiresAuth: false,
},
}, async ({ query, service_id, location_id, variables_json: unparsedVariables, auth_token_json: unparsedAuthToken, }, { projectId, config, host }) => {
const serviceInfo = await (0, load_1.pickOneService)(projectId, config, service_id || undefined, location_id || undefined);
const apiClient = await (0, emulator_1.getDataConnectEmulatorClient)(host);
let executeGraphQL = dataplane.executeGraphQL;
if (query.startsWith("query")) {
executeGraphQL = dataplane.executeGraphQLRead;
}
const response = await executeGraphQL(apiClient, serviceInfo.serviceName, {
query,
variables: (0, converter_1.parseVariables)(unparsedVariables),
extensions: {
impersonate: unparsedAuthToken
? {
authClaims: (0, converter_1.parseVariables)(unparsedAuthToken),
}
: undefined,
},
});
return (0, converter_1.graphqlResponseToToolResponse)(response.body);
});