@@ -16,6 +16,8 @@ import {
1616import { checkIfFileExists , upsertFile } from "./file-utils" ;
1717import { DataConnectService } from "./service" ;
1818import { DATA_CONNECT_EVENT_NAME } from "../analytics" ;
19+ import { dataConnectConfigs } from "./config" ;
20+ import { firstWhereDefined } from "../utils/signal" ;
1921
2022export function registerAdHoc (
2123 dataConnectService : DataConnectService ,
@@ -47,6 +49,7 @@ export function registerAdHoc(
4749 async function schemaReadData (
4850 document : DocumentNode ,
4951 ast : ObjectTypeDefinitionNode ,
52+ documentPath : string ,
5053 ) {
5154 // TODO(rrousselGit) - this is a temporary solution due to the lack of a "schema".
5255 // As such, we hardcoded the list of allowed primitives.
@@ -62,8 +65,12 @@ export function registerAdHoc(
6265 "Any" ,
6366 ] ) ;
6467
65- const basePath = vscode . workspace . rootPath + "/dataconnect/" ;
66- const filePath = vscode . Uri . file ( `${ basePath } ${ ast . name . value } _read.gql` ) ;
68+ const configs = await firstWhereDefined ( dataConnectConfigs ) ;
69+ const dataconnectConfig =
70+ configs . tryReadValue ?. findEnclosingServiceForPath ( documentPath ) ;
71+
72+ const basePath = dataconnectConfig ?. path ;
73+ const filePath = vscode . Uri . file ( `${ basePath } /${ ast . name . value } _read.gql` ) ;
6774
6875 // Recursively build a query for the object type.
6976 // Returns undefined if the query is empty.
@@ -136,12 +143,20 @@ query {
136143 * File will be created (unsaved) in operations/ folder, with an auto-generated named based on the schema type
137144 * Mutation will be generated with all
138145 * */
139- async function schemaAddData ( ast : ObjectTypeDefinitionNode ) {
146+ async function schemaAddData (
147+ ast : ObjectTypeDefinitionNode ,
148+ documentPath : string ,
149+ ) {
140150 // generate content for the file
141151 const preamble =
142152 "# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file." ;
143- const introspect = ( await dataConnectService . introspect ( ) ) ?. data ;
144- const schema = buildClientSchema ( introspect ! ) ;
153+
154+ const introspect = await dataConnectService . introspect ( ) ;
155+ if ( ! introspect . data ) {
156+ vscode . window . showErrorMessage ( "Failed to generate mutation. Please check your compilation errors." ) ;
157+ return ;
158+ }
159+ const schema = buildClientSchema ( introspect . data ) ;
145160 const dataType = schema . getType ( `${ ast . name . value } _Data` ) ;
146161 if ( ! isInputObjectType ( dataType ) ) return ;
147162
@@ -152,8 +167,14 @@ query {
152167 ) ,
153168 ) ;
154169 const content = [ preamble , adhocMutation ] . join ( "\n" ) ;
155- const basePath = vscode . workspace . rootPath + "/dataconnect/" ;
156- const filePath = vscode . Uri . file ( `${ basePath } ${ ast . name . value } _insert.gql` ) ;
170+
171+ // get root where dataconnect.yaml lives
172+ const configs = await firstWhereDefined ( dataConnectConfigs ) ;
173+ const dataconnectConfig =
174+ configs . tryReadValue ?. findEnclosingServiceForPath ( documentPath ) ;
175+ const basePath = dataconnectConfig ?. path ;
176+
177+ const filePath = vscode . Uri . file ( `${ basePath } /${ ast . name . value } _insert.gql` ) ;
157178 const doesFileExist = await checkIfFileExists ( filePath ) ;
158179
159180 if ( ! doesFileExist ) {
@@ -202,7 +223,10 @@ query {
202223 selections : [
203224 {
204225 kind : Kind . FIELD ,
205- name : { kind : Kind . NAME , value : `${ singularName . charAt ( 0 ) . toLowerCase ( ) } ${ singularName . slice ( 1 ) } _insert` } ,
226+ name : {
227+ kind : Kind . NAME ,
228+ value : `${ singularName . charAt ( 0 ) . toLowerCase ( ) } ${ singularName . slice ( 1 ) } _insert` ,
229+ } ,
206230 arguments : [
207231 {
208232 kind : Kind . ARGUMENT ,
@@ -251,16 +275,16 @@ query {
251275 return Disposable . from (
252276 vscode . commands . registerCommand (
253277 "firebase.dataConnect.schemaAddData" ,
254- ( ast ) => {
278+ ( ast , uri ) => {
255279 telemetryLogger . logUsage ( DATA_CONNECT_EVENT_NAME . ADD_DATA ) ;
256- schemaAddData ( ast ) ;
280+ schemaAddData ( ast , uri ) ;
257281 } ,
258282 ) ,
259283 vscode . commands . registerCommand (
260284 "firebase.dataConnect.schemaReadData" ,
261- ( document , ast ) => {
285+ ( document , ast , uri ) => {
262286 telemetryLogger . logUsage ( DATA_CONNECT_EVENT_NAME . READ_DATA ) ;
263- schemaReadData ( document , ast ) ;
287+ schemaReadData ( document , ast , uri ) ;
264288 } ,
265289 ) ,
266290 ) ;
0 commit comments