UNPKG

@gqty/cli

Version:

## Visit [https://gqty.dev/docs/cli/codegen](https://gqty.dev/docs/cli/codegen)

5 lines (4 loc) 3.09 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const fs=require("fs"),graphql=require("graphql"),path=require("path"),config=require("./config.js"),deps=require("./deps.js"),introspection=require("./introspection.js"),writeGenerate=require("./writeGenerate.js"),{buildClientSchema,buildSchema}=graphql;async function inspectWriteGenerate({endpoint:i,destination:n,generateOptions:h,cli:d,headers:p,transformSchemaOptions:m}={}){n&&(config.defaultConfig.destination=n);const{config:s,filepath:u}=await config.loadOrGenerateConfig({writeConfigFile:!0});if(i)config.defaultConfig.introspection.endpoint=i;else if(fs.existsSync(path.resolve("./schema.gql")))i="./schema.gql",config.defaultConfig.introspection.endpoint=i;else{const t=s.introspection&&s.introspection.endpoint;if(t&&t!==config.DUMMY_ENDPOINT)i=t;else throw console.error(` Please modify "${u.endsWith("package.json")?"gqty":"config"}.introspection.endpoint" in: "${u}".`),Error("ERROR: No introspection endpoint specified in configuration file.")}n||(n=s.destination||config.defaultConfig.destination),n=path.resolve(n);const g=Object.assign({},s,h);let r;if(config.defaultConfig.introspection.endpoint=i,config.defaultConfig.introspection.headers=p||{},i.startsWith("http://")||i.startsWith("https://"))r=await introspection.getRemoteSchema(i,{headers:p});else{config.defaultConfig.introspection.endpoint=config.DUMMY_ENDPOINT;const t=await deps.fg(i);if(t.length){const c=[".graphql",".gql"],o=t.filter(e=>path.extname(e)===".json"),a=t.filter(e=>c.includes(path.extname(e)));if(t.length>o.length+a.length)throw Error("Received invalid files. Type generation can only use .gql, .graphql or .json files");if(o.length&&a.length)throw Error("Received mixed file inputs. Can not combine JSON and GQL files");if(o.length&&o.length>1)throw Error("Received multiple JSON introspection files, shoud only be one");const l=await Promise.all(t.map(e=>fs.promises.readFile(e,{encoding:"utf-8"})));if(path.extname(t[0])===".json"){const e=JSON.parse(l[0]);let f;if(typeof e=="object"&&("data"in e&&e.data?f=e.data:"__schema"in e&&(f=e)),typeof f!="object")throw Error('Invalid JSON introspection result, expected "__schema" or "data.__schema" field.');r=buildClientSchema(f)}else r=buildSchema(l.join(` `))}else throw Error(`File "${i}" doesn't exists. If you meant to inspect a GraphQL API, make sure to put http:// or https:// in front of it.`)}const w=await writeGenerate.writeGenerate(r,n,g,async t=>{var c,o;const a=(c=g.subscriptions)!=null?c:s.subscriptions,l=(o=g.react)!=null?o:s.react,e=` If you meant to change this, please remove "${n}" and re-run code generation.`;a&&(t.includes("createSubscriptionsClient")||console.warn(`[Warning] You've changed the option "subscriptions" to 'true', which is different from your existing "${n}".`+e)),l&&(t.includes("createReactClient")||console.warn(`[Warning] You've changed the option "react" to 'true', which is different from your existing "${n}".`+e))},m);d&&console.log("Code generated successfully at "+w)}exports.inspectWriteGenerate=inspectWriteGenerate;