From 2de47b51a2d11a53a08e187c461277b8a349292c Mon Sep 17 00:00:00 2001 From: Scott Tolinski Date: Wed, 6 Oct 2021 15:41:44 -0600 Subject: [PATCH] moves to monorepo --- codegen.tsconfig.json | 20 ++++++ codegen/codegen.js | 117 +++++++++++++++++++++++++++++++++ codegen/codegen.js.map | 1 + {src => codegen}/codegen.ts | 101 +++++++++++++++------------- codegen/plugin.js | 77 ++++++++++++++++++++++ codegen/plugin.js.map | 1 + codegen/plugin.ts | 90 +++++++++++++++++++++++++ dist/gFetch.js | 96 +++++++++++++++++++++++++++ dist/gFetch.js.map | 1 + dist/index.js | 35 ++++++++++ dist/index.js.map | 1 + package-lock.json | 127 ------------------------------------ package.json | 20 ++++-- src/index.ts | 2 + tsconfig.json | 26 ++++++++ 15 files changed, 537 insertions(+), 178 deletions(-) create mode 100644 codegen.tsconfig.json create mode 100644 codegen/codegen.js create mode 100644 codegen/codegen.js.map rename {src => codegen}/codegen.ts (65%) create mode 100644 codegen/plugin.js create mode 100644 codegen/plugin.js.map create mode 100644 codegen/plugin.ts create mode 100644 dist/gFetch.js create mode 100644 dist/gFetch.js.map create mode 100644 dist/index.js create mode 100644 dist/index.js.map delete mode 100644 package-lock.json create mode 100644 tsconfig.json diff --git a/codegen.tsconfig.json b/codegen.tsconfig.json new file mode 100644 index 0000000..61e2de2 --- /dev/null +++ b/codegen.tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "allowJs": true, + "baseUrl": ".", + "checkJs": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "importsNotUsedAsValues": "error", + "isolatedModules": true, + "lib": ["es2020", "DOM"], + "module": "es2020", + "moduleResolution": "node", + "resolveJsonModule": true, + "skipLibCheck": true, + + "sourceMap": true, + "target": "es2019" + }, + "include": ["codegen/*.ts"] +} diff --git a/codegen/codegen.js b/codegen/codegen.js new file mode 100644 index 0000000..e663394 --- /dev/null +++ b/codegen/codegen.js @@ -0,0 +1,117 @@ +import { ClientSideBaseVisitor, } from "@graphql-codegen/visitor-plugin-common"; +import { concatAST, Kind, visit, } from "graphql"; +import { pascalCase } from "pascal-case"; +module.exports = { + plugin: (schema, documents, config) => { + const allAst = concatAST(documents.map((d) => d.document)); + const allFragments = [ + ...allAst.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((fragmentDef) => ({ + node: fragmentDef, + name: fragmentDef.name.value, + onType: fragmentDef.typeCondition.name.value, + isExternal: false, + })), + ...(config.externalFragments || []), + ]; + const visitor = new ClientSideBaseVisitor(schema, allFragments, {}, { documentVariableSuffix: "Doc" }, documents); + const visitorResult = visit(allAst, { leave: visitor }); + const operations = allAst.definitions.filter((d) => d.kind === Kind.OPERATION_DEFINITION); + const defaultTypes = ` + +type FetchWrapperArgs = { + fetch: typeof fetch, + variables?: T, +} + +type SubscribeWrapperArgs = { + variables?: T, +} +`; + const ops = operations + .map((o) => { + var _a; + if (o) { + const name = ((_a = o === null || o === void 0 ? void 0 : o.name) === null || _a === void 0 ? void 0 : _a.value) || ""; + // const dsl = `export const ${pascalCase(op.name.value)}Doc = gql\` + // ${documents.find((d) => d.rawSDL.includes(`${op.operation} ${op.name.value}`)).rawSDL}\`` + const op = `${pascalCase(name)}${pascalCase(o.operation)}`; + const opv = `${op}Variables`; + let operations = ""; + if (o.operation === "query") { + operations += ` +export const ${name} = ({ variables, fetch}: FetchWrapperArgs<${opv}>): + Promise> => + g.fetch<${op}>({ + queries: [{ query: ${pascalCase(name)}Doc, variables }], + fetch + }) +`; + // If config is set to have subscription query, also write the + if (config.subscriptionQuery) { + operations += ` +export const ${name}Subscribe = ({ variables }: SubscribeWrapperArgs<${opv}>): +Readable> => + g.oFetch<${op}>({ + queries: [{ query: ${pascalCase(name)}Doc, variables }] + }) + `; + } + } + else if (o.operation === "mutation") { + operations += ` +export const ${name} = ({ variables }: SubscribeWrapperArgs<${opv}>): +Promise> => + g.fetch<${op}>({ + queries: [{ query: ${pascalCase(name)}Doc, variables }], + fetch, + }) +`; + } + return operations; + } + }) + .join("\n"); + const imports = [ + `import type { Readable } from "svelte/store"`, + `import { g } from '${config.gFetchPath}'`, + `import type { GFetchReturnWithErrors } from '$graphql/gfetchLib'`, + `import gql from "graphql-tag"`, + ]; + // let schemaInputs = getCachedDocumentNodeFromSchema(schema).definitions.filter((d) => { + // return d.kind === 'InputObjectTypeDefinition' + // }) + // let inputs = schemaInputs + // .map((d) => { + // console.log('/* START */') + // // @ts-ignore + // console.log('NAME: ', d.fields[0].name.value) + // // @ts-ignore + // let isReq = d.fields[0]?.type?.kind === 'NonNullType' + // console.log('REQUIRED: ', isReq ? '✅' : '❌') + // // @ts-ignore + // console.log('TYPE: ', isReq ? d.fields[0]?.type?.type?.name?.value : d.fields[0]?.type?.name?.value) + // // @ts-ignore + // // @ts-ignore + // console.log('d.fields[0]', d.fields[0]?.type) + // console.log('/* END */') + // console.log('') + // return ` + // const inputName = { + // ${d.fields[0].name.value}: ${isReq ? d.fields[0]?.type?.type?.name?.value : d.fields[0]?.type?.name?.value} + // } + // ` + // }) + // .join('\n') + // console.log('inputs', inputs) + return { + prepend: imports, + content: [ + defaultTypes, + visitor.fragments, + ...visitorResult.definitions.filter((t) => typeof t == "string"), + ops, + ].join("\n"), + }; + }, +}; +//# sourceMappingURL=codegen.js.map \ No newline at end of file diff --git a/codegen/codegen.js.map b/codegen/codegen.js.map new file mode 100644 index 0000000..571c37c --- /dev/null +++ b/codegen/codegen.js.map @@ -0,0 +1 @@ +{"version":3,"file":"codegen.js","sourceRoot":"","sources":["codegen.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,qBAAqB,GACtB,MAAM,wCAAwC,CAAC;AAChD,OAAO,EACL,SAAS,EAET,IAAI,EAEJ,KAAK,GACN,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,CAAC,OAAO,GAAG;IACf,MAAM,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE;QACpC,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C/G,IAAI;QACJ,MAAM;QACN,WAAW;QACX,oBAAoB;QACpB,oCAAoC;QAEpC,OAAO;YACL,OAAO,EAAE,OAAO;YAChB,OAAO,EAAE;gBACP,YAAY;gBACZ,OAAO,CAAC,SAAS;gBACjB,GAAG,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC;gBAChE,GAAG;aACJ,CAAC,IAAI,CAAC,IAAI,CAAC;SACb,CAAC;IACJ,CAAC;CACe,CAAC"} \ No newline at end of file diff --git a/src/codegen.ts b/codegen/codegen.ts similarity index 65% rename from src/codegen.ts rename to codegen/codegen.ts index 1fb7460..db15766 100644 --- a/src/codegen.ts +++ b/codegen/codegen.ts @@ -1,38 +1,47 @@ -import { CodegenPlugin } from '@graphql-codegen/plugin-helpers' -import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common' -import { concatAST, FragmentDefinitionNode, Kind, OperationDefinitionNode, visit } from 'graphql' -import { pascalCase } from 'pascal-case' - -const visitorPluginCommon = require('@graphql-codegen/visitor-plugin-common') +import type { CodegenPlugin } from "@graphql-codegen/plugin-helpers"; +import { + LoadedFragment, + ClientSideBaseVisitor, +} from "@graphql-codegen/visitor-plugin-common"; +import { + concatAST, + FragmentDefinitionNode, + Kind, + OperationDefinitionNode, + visit, +} from "graphql"; +import { pascalCase } from "pascal-case"; module.exports = { - plugin: (schema, documents, config, info) => { - const allAst = concatAST(documents.map((d) => d.document)) + plugin: (schema, documents, config) => { + const allAst = concatAST(documents.map((d) => d.document)); const allFragments: LoadedFragment[] = [ - ...(allAst.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION) as FragmentDefinitionNode[]).map( - (fragmentDef) => ({ - node: fragmentDef, - name: fragmentDef.name.value, - onType: fragmentDef.typeCondition.name.value, - isExternal: false, - }) - ), + ...( + allAst.definitions.filter( + (d) => d.kind === Kind.FRAGMENT_DEFINITION + ) as FragmentDefinitionNode[] + ).map((fragmentDef) => ({ + node: fragmentDef, + name: fragmentDef.name.value, + onType: fragmentDef.typeCondition.name.value, + isExternal: false, + })), ...(config.externalFragments || []), - ] + ]; - const visitor = new visitorPluginCommon.ClientSideBaseVisitor( + const visitor = new ClientSideBaseVisitor( schema, allFragments, {}, - { documentVariableSuffix: 'Doc' }, + { documentVariableSuffix: "Doc" }, documents - ) - const visitorResult = visit(allAst, { leave: visitor }) + ); + const visitorResult = visit(allAst, { leave: visitor }); const operations = allAst.definitions.filter( (d) => d.kind === Kind.OPERATION_DEFINITION - ) as OperationDefinitionNode[] + ) as OperationDefinitionNode[]; const defaultTypes = ` type FetchWrapperArgs = { @@ -43,59 +52,59 @@ type FetchWrapperArgs = { type SubscribeWrapperArgs = { variables?: T, } -` +`; const ops = operations .map((o) => { if (o) { - let name = o?.name?.value || '' + const name = o?.name?.value || ""; // const dsl = `export const ${pascalCase(op.name.value)}Doc = gql\` // ${documents.find((d) => d.rawSDL.includes(`${op.operation} ${op.name.value}`)).rawSDL}\`` - const op = `${pascalCase(name)}${pascalCase(o.operation)}` - const opv = `${op}Variables` - let operations = '' + const op = `${pascalCase(name)}${pascalCase(o.operation)}`; + const opv = `${op}Variables`; + let operations = ""; - if (o.operation === 'query') { + if (o.operation === "query") { operations += ` -export const ${name} = ({ variables, fetch}: FetchWrapperArgs<${opv}>): - Promise> => +export const ${name} = ({ variables, fetch}: FetchWrapperArgs<${opv}>): + Promise> => g.fetch<${op}>({ queries: [{ query: ${pascalCase(name)}Doc, variables }], fetch }) -` +`; // If config is set to have subscription query, also write the if (config.subscriptionQuery) { operations += ` -export const ${name}Subscribe = ({ variables }: SubscribeWrapperArgs<${opv}>): -Readable> => +export const ${name}Subscribe = ({ variables }: SubscribeWrapperArgs<${opv}>): +Readable> => g.oFetch<${op}>({ queries: [{ query: ${pascalCase(name)}Doc, variables }] }) - ` + `; } - } else if (o.operation === 'mutation') { + } else if (o.operation === "mutation") { operations += ` -export const ${name} = ({ variables }: SubscribeWrapperArgs<${opv}>): -Promise> => +export const ${name} = ({ variables }: SubscribeWrapperArgs<${opv}>): +Promise> => g.fetch<${op}>({ queries: [{ query: ${pascalCase(name)}Doc, variables }], fetch, }) -` +`; } - return operations + return operations; } }) - .join('\n') + .join("\n"); - let imports = [ + const imports = [ `import type { Readable } from "svelte/store"`, `import { g } from '${config.gFetchPath}'`, `import type { GFetchReturnWithErrors } from '$graphql/gfetchLib'`, `import gql from "graphql-tag"`, - ] + ]; // let schemaInputs = getCachedDocumentNodeFromSchema(schema).definitions.filter((d) => { // return d.kind === 'InputObjectTypeDefinition' @@ -130,9 +139,9 @@ Promise> => content: [ defaultTypes, visitor.fragments, - ...visitorResult.definitions.filter((t) => typeof t == 'string'), + ...visitorResult.definitions.filter((t) => typeof t == "string"), ops, - ].join('\n'), - } + ].join("\n"), + }; }, -} as CodegenPlugin +} as CodegenPlugin; diff --git a/codegen/plugin.js b/codegen/plugin.js new file mode 100644 index 0000000..440c38d --- /dev/null +++ b/codegen/plugin.js @@ -0,0 +1,77 @@ +import { generate } from "@graphql-codegen/cli"; +// import { fileURLToPath } from "url"; +// import { buildSchema, printSchema, parse, GraphQLSchema } from "graphql"; +// import * as fs from "fs"; +// import * as path from "path"; +// import * as typescriptPlugin from "@graphql-codegen/typescript"; +// const __filename = fileURLToPath(import.meta.url); +// const __dirname = path.dirname(__filename); +// const schema: GraphQLSchema = buildSchema(`type A { name: String }`); +const outputFile = "gquery.generated.ts"; +// const config = { +// documents: [], +// config: {}, // used by a plugin internally, although the 'typescript' plugin currently // returns the string output, rather than writing to a file +// filename: outputFile, +// schema: parse(printSchema(schema)), +// plugins: [ +// // Each plugin should be an object +// { +// typescript: {}, // Here you can pass configuration to the plugin +// }, +// ], +// pluginMap: { +// typescript: typescriptPlugin, +// }, +// }; +const fileRegex = /\.(graphql)$/; +export default function levelupViteCodegen(options) { + if (!options.schema) { + throw new Error("No schema provided"); + } + if (!options.output) { + throw new Error("No output directory specified"); + } + const { schema, output } = options; + console.log(`${process.cwd()}/${output}/${outputFile}`); + return { + name: "levelup-vite-codegen", + async buildStart() { + try { + const generatedFiles = await generate({ + schema, + documents: "./src/**/*.graphql", + generates: { + [`${process.cwd()}/${output}/gquery-types.generated.ts`]: { + plugins: ["typescript"], + }, + // [`${process.cwd()}/${output}/${outputFile}`]: { + // // preset: "import-types", + // // presetConfig: { + // // typesPath: `${output}/${outputFile}`, + // // }, + // // preset: "near-operation-file", + // // presetConfig: { + // // extension: ".generated.ts", + // // folder: "./", + // // baseTypesPath: `${output}/${outputFile}`, + // // }, + // plugins: [ + // "typescript", + // // "typescript-operations", + // "../packages/gQueryCodegen/codegen.js", + // ], + // }, + }, + }, true); + } + catch (e) { + console.log("❓ gFetch Warning - No `.graphql` files found."); + console.log("❓ gFetch Warning - If you would like the gQuery generator to work (we reccomend you do)."); + console.log("❓ gFetch Warning - If you would like to add them, you will need to restart SvelteKit."); + } + return; + }, + transform(src, id) { }, + }; +} +//# sourceMappingURL=plugin.js.map \ No newline at end of file diff --git a/codegen/plugin.js.map b/codegen/plugin.js.map new file mode 100644 index 0000000..0a69e50 --- /dev/null +++ b/codegen/plugin.js.map @@ -0,0 +1 @@ +{"version":3,"file":"plugin.js","sourceRoot":"","sources":["plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,uCAAuC;AAEvC,4EAA4E;AAC5E,4BAA4B;AAC5B,gCAAgC;AAChC,mEAAmE;AAEnE,qDAAqD;AACrD,8CAA8C;AAE9C,wEAAwE;AACxE,MAAM,UAAU,GAAG,qBAAqB,CAAC;AACzC,mBAAmB;AACnB,mBAAmB;AACnB,wJAAwJ;AACxJ,0BAA0B;AAC1B,wCAAwC;AACxC,eAAe;AACf,yCAAyC;AACzC,QAAQ;AACR,yEAAyE;AACzE,SAAS;AACT,OAAO;AACP,iBAAiB;AACjB,oCAAoC;AACpC,OAAO;AACP,KAAK;AAEL,MAAM,SAAS,GAAG,cAAc,CA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AE,IAAG,CAAC;KACtB,CAAC;AACJ,CAAC"} \ No newline at end of file diff --git a/codegen/plugin.ts b/codegen/plugin.ts new file mode 100644 index 0000000..c573e97 --- /dev/null +++ b/codegen/plugin.ts @@ -0,0 +1,90 @@ +import { generate } from "@graphql-codegen/cli"; +// import { fileURLToPath } from "url"; + +// import { buildSchema, printSchema, parse, GraphQLSchema } from "graphql"; +// import * as fs from "fs"; +// import * as path from "path"; +// import * as typescriptPlugin from "@graphql-codegen/typescript"; + +// const __filename = fileURLToPath(import.meta.url); +// const __dirname = path.dirname(__filename); + +// const schema: GraphQLSchema = buildSchema(`type A { name: String }`); +const outputFile = "gquery.generated.ts"; +// const config = { +// documents: [], +// config: {}, // used by a plugin internally, although the 'typescript' plugin currently // returns the string output, rather than writing to a file +// filename: outputFile, +// schema: parse(printSchema(schema)), +// plugins: [ +// // Each plugin should be an object +// { +// typescript: {}, // Here you can pass configuration to the plugin +// }, +// ], +// pluginMap: { +// typescript: typescriptPlugin, +// }, +// }; + +const fileRegex = /\.(graphql)$/; + +export default function levelupViteCodegen(options) { + if (!options.schema) { + throw new Error("No schema provided"); + } + if (!options.output) { + throw new Error("No output directory specified"); + } + + const { schema, output } = options; + console.log(`${process.cwd()}/${output}/${outputFile}`); + return { + name: "levelup-vite-codegen", + + async buildStart() { + try { + const generatedFiles = await generate( + { + schema, + documents: "./src/**/*.graphql", + generates: { + [`${process.cwd()}/${output}/gquery-types.generated.ts`]: { + plugins: ["typescript"], + }, + // [`${process.cwd()}/${output}/${outputFile}`]: { + // // preset: "import-types", + // // presetConfig: { + // // typesPath: `${output}/${outputFile}`, + // // }, + // // preset: "near-operation-file", + // // presetConfig: { + // // extension: ".generated.ts", + // // folder: "./", + // // baseTypesPath: `${output}/${outputFile}`, + // // }, + // plugins: [ + // "typescript", + // // "typescript-operations", + // "../packages/gQueryCodegen/codegen.js", + // ], + // }, + }, + }, + true + ); + } catch (e) { + console.log("❓ gFetch Warning - No `.graphql` files found."); + console.log( + "❓ gFetch Warning - If you would like the gQuery generator to work (we reccomend you do)." + ); + console.log( + "❓ gFetch Warning - If you would like to add them, you will need to restart SvelteKit." + ); + } + return; + }, + + transform(src, id) {}, + }; +} diff --git a/dist/gFetch.js b/dist/gFetch.js new file mode 100644 index 0000000..8107e39 --- /dev/null +++ b/dist/gFetch.js @@ -0,0 +1,96 @@ +import { readable, writable } from "svelte/store"; +// This function accepts a graphql document and returns a string to be used +// in fetch calls +export function gqlToString(tag) { + return tag.loc.source.body; +} +export class GFetch extends Object { + constructor(options) { + super(); + const { path } = options; + this.path = path; + this.fetch = this.fetch.bind(this); + this.oFetch = this.oFetch.bind(this); + } + // * gFetch + // This is a fetcher that returns a promise that resolves to a graphql response + async fetch({ queries, fetch, }) { + // Get all the queries and transform the docs into strings + // Fetching gql require them to be strings. + if (!fetch && window) { + fetch = window.fetch; + } + const newQueries = { + ...queries[0], + query: gqlToString(queries[0].query), + }; + // This is generic fetch, that is polyfilled via svelte kit + // graph ql fetches must be POST + // credentials include for user ssr data + const res = await fetch(this.path, { + method: "POST", + credentials: "include", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(newQueries), + }); + // Gets the data back from the server + const data = await res.json(); + return { + ...data.data, + }; + } + // * ogFetch + // This function is a fetcher that returns a svelte readable subscription + // This is to be used for client side fetching of data + oFetch({ queries, }) { + // 1. Build the store and initialize it as empty and error free + const initial = new Map(); + // Creates a store that will be used to subscribe to the data + const store = readable(initial, this.makeSubscribe(initial, queries)); + return store; + } + // A dummy function that is used to make subscribe happy. + unsubscribe() { + // Nothing to do in this case + } + // Part of ogFetch + // Designed this way to work will with Svelte's readable store + makeSubscribe(data, queries) { + // Create a closure with access to the + // initial data and initialization arguments + return (set) => { + // 3. This won't get executed until the store has + // its first subscriber. Kick off retrieval. + this.fetchDataForSubscription(data, set, queries); + // We're not waiting for the response. + // Return the unsubscribe function which doesn't do + // do anything here (but is part of the stores protocol). + return this.unsubscribe; + }; + } + // Part of ogFetch + // Runs gFetch and updates subscription + async fetchDataForSubscription(data, set, queries) { + try { + // Dispatch the request for the users + // This code is ONLY run client side, so fetch comes globally from the browser + const response = await this.fetch({ queries, fetch }); + set(response); + } + catch (error) { + // 6b. if there is a fetch error - deal with it + // and let observers know + data.error = error; + set(data); + } + } +} +export const data = writable(); +// ! IDEAS +// Mutations should take care of updating a generated writeable. +// import { tutorial } from '$graphql/state' +// import { updateTutorial } from '$graphql/gfetch.generated' +// updateTutorial() +// $tutorial is auto updated site wide +// Devtools based on svelte toy +//# sourceMappingURL=gFetch.js.map \ No newline at end of file diff --git a/dist/gFetch.js.map b/dist/gFetch.js.map new file mode 100644 index 0000000..51ff58b --- /dev/null +++ b/dist/gFetch.js.map @@ -0,0 +1 @@ +{"version":3,"file":"gFetch.js","sourceRoot":"","sources":["../src/gFetch.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAmDlD,2EAA2E;AAC3E,iBAAiB;AACjB,MAAM,UAAU,WAAW,CAAC,GAAiB;IAC3C,OAAO,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;AAC7B,CAAC;AAqBD,MAAM,OAAO,MAAO,SAAQ,MAAM;IAGhC,YAAY,OAA4B;QACtC,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;IAED,WAAW;IACX,+EAA+E;IACxE,KAAK,CAAC,KAAK,CAAI,EACpB,OAAO,EACP,KAAK,GACY;QACjB,0DAA0D;QAC1D,2CAA2C;QAC3C,IAAI,CAAC,KAAK,IAAI,MAAM,EAAE;YACpB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DAAyD;YACzD,OAAO,IAAI,CAAC,WAAW,CAAC;QAC1B,CAAC,CAAC;IACJ,CAAC;IAED,kBAAkB;IAClB,uCAAuC;IAC/B,KAAK,CAAC,wBAAwB,CAAC,IAAI,EAAE,GAAG,EAAE,OAAwB;QACxE,IAAI;YACF,qCAAqC;YACrC,8EAA8E;YAC9E,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;YACtD,GAAG,CAAC,QAAQ,CAAC,CAAC;SACf;QAAC,OAAO,KAAK,EAAE;YACd,+CAA+C;YAC/C,yBAAyB;YACzB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YACnB,GAAG,CAAC,IAAI,CAAC,CAAC;SACX;IACH,CAAC;CACF;AAED,MAAM,CAAC,MAAM,IAAI,GAAG,QAAQ,EAAE,CAAC;AAE/B,UAAU;AACV,gEAAgE;AAChE,4CAA4C;AAC5C,6DAA6D;AAC7D,mBAAmB;AACnB,sCAAsC;AACtC,+BAA+B"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..fe3d6be --- /dev/null +++ b/dist/index.js @@ -0,0 +1,35 @@ +import { writable, get } from "svelte/store"; +const newGCache = () => { + const { subscribe, update, set } = writable({}); + async function hydrate(newData) { + update((old) => { + return { + ...old, + ...newData, + }; + }); + } + return { + subscribe, + set, + update, + hydrate, + }; +}; +export const gCache = newGCache(); +export async function gQuery(typename, { query, variables }, { update } = {}) { + const current = get(gCache); + // Extremely Naive cache + // Just checks to see if the data is there, if it is, don't + // Hit the network again + // if update option is present, then we want to update the cache + if (!current?.[typename] || update) { + const newData = await query({ + variables, + fetch, + }); + await gCache.hydrate(newData); + } +} +export * from "./gFetch"; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/index.js.map b/dist/index.js.map new file mode 100644 index 0000000..5e9a537 --- /dev/null +++ b/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAE7C,MAAM,SAAS,GAAG,GAAG,EAAE;IACrB,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;IAEhD,KAAK,UAAU,OAAO,CAAC,OAAO;QAC5B,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,OAAO;gBACL,GAAG,GAAG;gBACN,GAAG,OAAO;aACX,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO;QACL,SAAS;QACT,GAAG;QACH,MAAM;QACN,OAAO;KACR,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;AAMlC,MAAM,CAAC,KAAK,UAAU,MAAM,CAC1B,QAAQ,EACR,EAAE,KAAK,EAAE,SAAS,EAAE,EACpB,EAAE,MAAM,KAA2B,EAAE;IAErC,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC;IAE5B,wBAAwB;IACxB,2DAA2D;IAC3D,wBAAwB;IACxB,gEAAgE;IAChE,IAAI,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,IAAI,MAAM,EAAE;QAClC,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC;YAC1B,SAAS;YACT,KAAK;SACN,CAAC,CAAC;QAEH,MAAM,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;KAC/B;AACH,CAAC;AAED,cAAc,UAAU,CAAC"} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 25cba47..0000000 --- a/package-lock.json +++ /dev/null @@ -1,127 +0,0 @@ -{ - "name": "gquery", - "version": "0.0.1", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "gquery", - "version": "0.0.1", - "license": "ISC", - "dependencies": { - "camel-case": "^4.1.2", - "graphql": "^15.6.0", - "pascal-case": "^3.1.2", - "svelte": "^3.43.0" - } - }, - "node_modules/camel-case": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/camel-case/-/camel-case-4.1.2.tgz", - "integrity": "sha512-gxGWBrTT1JuMx6R+o5PTXMmUnhnVzLQ9SNutD4YqKtI6ap897t3tKECYla6gCWEkplXnlNybEkZg9GEGxKFCgw==", - "dependencies": { - "pascal-case": "^3.1.2", - "tslib": "^2.0.3" - } - }, - "node_modules/graphql": { - "version": "15.6.0", - "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.6.0.tgz", - "integrity": "sha512-WJR872Zlc9hckiEPhXgyUftXH48jp2EjO5tgBBOyNMRJZ9fviL2mJBD6CAysk6N5S0r9BTs09Qk39nnJBkvOXQ==", - "engines": { - "node": ">= 10.x" - } - }, - "node_modules/lower-case": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz", - "integrity": "sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg==", - "dependencies": { - "tslib": "^2.0.3" - } - }, - "node_modules/no-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/no-case/-/no-case-3.0.4.tgz", - "integrity": "sha512-fgAN3jGAh+RoxUGZHTSOLJIqUc2wmoBwGR4tbpNAKmmovFoWq0OdRkb0VkldReO2a2iBT/OEulG9XSUc10r3zg==", - "dependencies": { - "lower-case": "^2.0.2", - "tslib": "^2.0.3" - } - }, - "node_modules/pascal-case": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/pascal-case/-/pascal-case-3.1.2.tgz", - "integrity": "sha512-uWlGT3YSnK9x3BQJaOdcZwrnV6hPpd8jFH1/ucpiLRPh/2zCVJKS19E4GvYHvaCcACn3foXZ0cLB9Wrx1KGe5g==", - "dependencies": { - "no-case": "^3.0.4", - "tslib": "^2.0.3" - } - }, - "node_modules/svelte": { - "version": "3.43.0", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.43.0.tgz", - "integrity": "sha512-T2pMPHrxXp+SM8pLLUXLQgkdo+JhTls7aqj9cD7z8wT2ccP+OrCAmtQS7h6pvMjitaZhXFNnCK582NxDpy8HSw==", - "engines": { - "node": ">= 8" - } - }, - "node_modules/tslib": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", - "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==" - } - }, - "dependencies": { - "camel-case": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/camel-case/-/camel-case-4.1.2.tgz", - "integrity": "sha512-gxGWBrTT1JuMx6R+o5PTXMmUnhnVzLQ9SNutD4YqKtI6ap897t3tKECYla6gCWEkplXnlNybEkZg9GEGxKFCgw==", - "requires": { - "pascal-case": "^3.1.2", - "tslib": "^2.0.3" - } - }, - "graphql": { - "version": "15.6.0", - "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.6.0.tgz", - "integrity": "sha512-WJR872Zlc9hckiEPhXgyUftXH48jp2EjO5tgBBOyNMRJZ9fviL2mJBD6CAysk6N5S0r9BTs09Qk39nnJBkvOXQ==" - }, - "lower-case": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz", - "integrity": "sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg==", - "requires": { - "tslib": "^2.0.3" - } - }, - "no-case": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/no-case/-/no-case-3.0.4.tgz", - "integrity": "sha512-fgAN3jGAh+RoxUGZHTSOLJIqUc2wmoBwGR4tbpNAKmmovFoWq0OdRkb0VkldReO2a2iBT/OEulG9XSUc10r3zg==", - "requires": { - "lower-case": "^2.0.2", - "tslib": "^2.0.3" - } - }, - "pascal-case": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/pascal-case/-/pascal-case-3.1.2.tgz", - "integrity": "sha512-uWlGT3YSnK9x3BQJaOdcZwrnV6hPpd8jFH1/ucpiLRPh/2zCVJKS19E4GvYHvaCcACn3foXZ0cLB9Wrx1KGe5g==", - "requires": { - "no-case": "^3.0.4", - "tslib": "^2.0.3" - } - }, - "svelte": { - "version": "3.43.0", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.43.0.tgz", - "integrity": "sha512-T2pMPHrxXp+SM8pLLUXLQgkdo+JhTls7aqj9cD7z8wT2ccP+OrCAmtQS7h6pvMjitaZhXFNnCK582NxDpy8HSw==" - }, - "tslib": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", - "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==" - } - } -} diff --git a/package.json b/package.json index 21defd9..ce9de57 100644 --- a/package.json +++ b/package.json @@ -4,27 +4,37 @@ "url": "https://github.com/leveluptuts/gQuery/issues" }, "dependencies": { + "@graphql-codegen/cli": "^2.2.0", "camel-case": "^4.1.2", - "graphql": "^15.6.0", "pascal-case": "^3.1.2", - "svelte": "^3.43.0" + "svelte": "^3.43.1", + "vite": "^2.6.2" }, "description": "Not like jQuery. A GraphQL Fetcher & Cache for Svelte Kit", + "devDependencies": { + "@graphql-codegen/plugin-helpers": "^2.1.1", + "@graphql-codegen/visitor-plugin-common": "^2.2.1", + "graphql": "^15.6.1", + "typescript": "^4.3.2" + }, "exports": { - "./gfetch": "src/gfetch.js" + ".": "./dist/index.js", + "./codegen": "./codegen/plugin.js", + "./package.json": "./package.json" }, "homepage": "https://github.com/leveluptuts/gQuery#readme", "keywords": [ "graphql" ], "license": "ISC", - "main": "src/index.js", - "name": "gquery", + "name": "@leveluptuts/gQuery", "repository": { "type": "git", "url": "git+https://github.com/leveluptuts/gQuery.git" }, "scripts": { + "dev": "tsc --watch", + "dev:codegen": "tsc -w --project codegen.tsconfig.json", "test": "echo \"Error: no test specified\" && exit 1" }, "type": "module", diff --git a/src/index.ts b/src/index.ts index 1a7f454..86f8f44 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,3 +46,5 @@ export async function gQuery( await gCache.hydrate(newData); } } + +export * from "./gFetch"; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..b7d0d53 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "module": "es2020", + "lib": ["es2020", "DOM"], + "outDir": "./dist", + "target": "es2020", + /** + svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript + to enforce using \`import type\` instead of \`import\` for Types. + */ + "importsNotUsedAsValues": "error", + "isolatedModules": true, + "resolveJsonModule": true, + /** + To have warnings/errors of the Svelte compiler at the correct position, + enable source maps by default. + */ + "sourceMap": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + }, + "exclude": ["node_modules"], + "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/codegen.ts"] +}