@@ -5,21 +5,11 @@ import * as path from "path";
55
66import { TargetType , default as generate } from "../../generate" ;
77
8- import {
9- buildClientSchema ,
10- visit ,
11- extendSchema ,
12- buildASTSchema
13- } from "graphql" ;
14-
15- import { loadSchema } from "../../load-schema" ;
16-
178import { engineFlags } from "../../engine-cli" ;
18- import { loadQueryDocuments } from "apollo-codegen-core/lib/loading" ;
199
2010import { Gaze } from "gaze" ;
2111
22- import { getOperationPathsForConfig } from "../../config" ;
12+ import { resolveDocumentSets , ResolvedDocumentSet } from "../../config" ;
2313import { loadConfigStep } from "../../load-config" ;
2414
2515const waitForKey = async ( ) => {
@@ -196,103 +186,115 @@ export default class Generate extends Command {
196186 }
197187
198188 const tasks : Listr = new Listr ( [
199- loadConfigStep ( ( msg ) => this . error ( msg ) , flags , false , true ) ,
189+ loadConfigStep ( flags ) ,
200190 {
201- title : "Scanning for GraphQL queries " ,
191+ title : "Resolving GraphQL document sets and dependencies " ,
202192 task : async ( ctx , task ) => {
203- const paths = getOperationPathsForConfig ( ctx . config ) ;
204- task . title = `Scanning for GraphQL queries (${ paths . length } found)` ;
193+ ctx . documentSets = await resolveDocumentSets ( ctx . config , true ) ;
194+ task . title = `Scanning for GraphQL queries (${
195+ ctx . documentSets . length
196+ } found)`; // this is a bogus wrong value
205197
206- const excludedPaths = [
207- flags . clientSchema ? path . resolve ( flags . clientSchema ) : undefined ,
208- ctx . config . schema ? path . resolve ( ctx . config . schema ) : undefined
209- ] ;
198+ // const excludedPaths = [
199+ // flags.clientSchema ? path.resolve(flags.clientSchema) : undefined,
200+ // ctx.config.schema ? path.resolve(ctx.config.schema) : undefined
201+ // ];
210202
211- ctx . queryPaths = paths . filter (
212- p => ! excludedPaths . some ( v => v == path . resolve ( p ) )
213- ) . map ( p => path . relative ( ctx . config . projectFolder , p ) ) ;
214- }
215- } ,
216- {
217- title : "Fetching current schema" ,
218- task : async ctx => {
219- ctx . schema = await loadSchema ( ctx . config ) . catch ( this . error ) ;
220- }
221- } ,
222- {
223- title : "Parsing GraphQL schema" ,
224- task : async ( ctx , task ) => {
225- if ( ctx . schema ) {
226- ctx . schema = buildClientSchema ( { __schema : ctx . schema } ) ;
227- } else {
228- task . skip ( "No server-side schema provided" ) ;
229- }
203+ // TODO: move filtering somewhere else
204+ // ctx.queryPaths = paths.filter(
205+ // p => !excludedPaths.some(v => v == path.resolve(p))
206+ // ).map(p => path.relative(ctx.config.projectFolder, p));
230207 }
231208 } ,
232- {
233- title : "Loading client-side GraphQL schema" ,
234- task : async ( ctx , task ) => {
235- if ( ! flags . clientSchema ) {
236- task . skip ( "Path to client schema not provided" ) ;
237- } else {
238- const foundDocuments = loadQueryDocuments ( [
239- path . resolve ( flags . clientSchema )
240- ] ) ;
241- if ( foundDocuments . length == 0 ) {
242- this . error ( "Found no query documents, aborting" ) ;
243- }
209+ // {
210+ // title: "Fetching current schema",
211+ // task: async ctx => {
212+ // ctx.schema = await loadSchema(ctx.config).catch(this.error);
213+ // }
214+ // },
215+ // {
216+ // title: "Parsing GraphQL schema",
217+ // task: async (ctx, task) => {
218+ // if (ctx.schema) {
219+ // ctx.schema = buildClientSchema({ __schema: ctx.schema });
220+ // } else {
221+ // task.skip("No server-side schema provided");
222+ // }
223+ // }
224+ // },
225+ // {
226+ // title: "Loading client-side GraphQL schema",
227+ // task: async (ctx, task) => {
228+ // if (!flags.clientSchema) {
229+ // task.skip("Path to client schema not provided");
230+ // } else {
231+ // const foundDocuments = loadQueryDocuments([
232+ // path.resolve(flags.clientSchema)
233+ // ]);
234+ // if (foundDocuments.length == 0) {
235+ // this.error("Found no query documents, aborting");
236+ // }
244237
245- if ( foundDocuments . length > 1 ) {
246- this . warn (
247- "Found more than one query document, using the first one"
248- ) ;
249- }
238+ // if (foundDocuments.length > 1) {
239+ // this.warn(
240+ // "Found more than one query document, using the first one"
241+ // );
242+ // }
250243
251- const ast = foundDocuments [ 0 ] ;
252- visit ( ast , {
253- enter ( node ) {
254- if ( node . kind == "FieldDefinition" ) {
255- ( node as any ) . __client = true ;
256- }
257- }
258- } ) ;
244+ // const ast = foundDocuments[0];
245+ // visit(ast, {
246+ // enter(node) {
247+ // if (node.kind == "FieldDefinition") {
248+ // (node as any).__client = true;
249+ // }
250+ // }
251+ // });
259252
260- if ( ctx . schema ) {
261- ctx . schema = extendSchema ( ctx . schema , ast ) ;
262- } else {
263- ctx . schema = buildASTSchema ( ast ) ;
264- }
265- }
266- }
267- } ,
253+ // if (ctx.schema) {
254+ // ctx.schema = extendSchema(ctx.schema, ast);
255+ // } else {
256+ // ctx.schema = buildASTSchema(ast);
257+ // }
258+ // }
259+ // }
260+ // },
268261 {
269262 title : "Generating query files" ,
270263 task : async ( ctx , task ) => {
271264 task . title = `Generating query files with '${ inferredTarget } ' target` ;
272- const writtenFiles = generate (
273- ctx . queryPaths ,
274- ctx . schema ,
275- typeof args . output === "string" ? args . output : "__generated__" ,
276- flags . only ,
277- inferredTarget ,
278- flags . tagName as string ,
279- ! flags . outputFlat ,
280- {
281- passthroughCustomScalars :
282- flags . passthroughCustomScalars || ! ! flags . customScalarsPrefix ,
283- customScalarsPrefix : flags . customScalarsPrefix || "" ,
284- addTypename : flags . addTypename ,
285- namespace : flags . namespace ,
286- operationIdsPath : flags . operationIdsPath ,
287- generateOperationIds : ! ! flags . operationIdsPath ,
288- mergeInFieldsFromFragmentSpreads :
289- flags . mergeInFieldsFromFragmentSpreads ,
290- useFlowExactObjects : flags . useFlowExactObjects ,
291- useFlowReadOnlyTypes : flags . useFlowReadOnlyTypes
292- }
293- ) ;
265+ if ( ctx . documentSets . length == 0 ) {
266+ // error
267+ } else if ( ctx . documentSets . length == 1 ) {
268+ const set = ctx . documentSets [ 0 ] as ResolvedDocumentSet ;
269+ const writtenFiles = generate (
270+ set . documentPaths . map ( p =>
271+ path . relative ( ctx . config . projectFolder , p )
272+ ) ,
273+ set . schema ! ,
274+ typeof args . output === "string" ? args . output : "__generated__" ,
275+ flags . only ,
276+ inferredTarget ,
277+ flags . tagName as string ,
278+ ! flags . outputFlat ,
279+ {
280+ passthroughCustomScalars :
281+ flags . passthroughCustomScalars || ! ! flags . customScalarsPrefix ,
282+ customScalarsPrefix : flags . customScalarsPrefix || "" ,
283+ addTypename : flags . addTypename ,
284+ namespace : flags . namespace ,
285+ operationIdsPath : flags . operationIdsPath ,
286+ generateOperationIds : ! ! flags . operationIdsPath ,
287+ mergeInFieldsFromFragmentSpreads :
288+ flags . mergeInFieldsFromFragmentSpreads ,
289+ useFlowExactObjects : flags . useFlowExactObjects ,
290+ useFlowReadOnlyTypes : flags . useFlowReadOnlyTypes
291+ }
292+ ) ;
294293
295- task . title = `Generating query files with '${ inferredTarget } ' target - wrote ${ writtenFiles } files` ;
294+ task . title = `Generating query files with '${ inferredTarget } ' target - wrote ${ writtenFiles } files` ;
295+ } else {
296+ // TODO
297+ }
296298 }
297299 }
298300 ] ) ;
0 commit comments