mirror of
https://github.com/BradNut/gQuery
synced 2025-09-08 17:40:18 +00:00
cleans up repo a bit
This commit is contained in:
parent
1c6731897b
commit
e442693cdc
15 changed files with 51 additions and 215 deletions
|
|
@ -3,7 +3,6 @@ import "@graphql-codegen/plugin-helpers";
|
||||||
import { ClientSideBaseVisitor, } from "@graphql-codegen/visitor-plugin-common";
|
import { ClientSideBaseVisitor, } from "@graphql-codegen/visitor-plugin-common";
|
||||||
import pascalCase from "just-pascal-case";
|
import pascalCase from "just-pascal-case";
|
||||||
export const plugin = (schema, documents, config) => {
|
export const plugin = (schema, documents, config) => {
|
||||||
console.log("🛠️ Codegen Starting");
|
|
||||||
const allAst = concatAST(documents.map((d) => d.document));
|
const allAst = concatAST(documents.map((d) => d.document));
|
||||||
const allFragments = [
|
const allFragments = [
|
||||||
...allAst.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((fragmentDef) => ({
|
...allAst.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((fragmentDef) => ({
|
||||||
|
|
@ -28,17 +27,14 @@ type SubscribeWrapperArgs<T> = {
|
||||||
variables?: T,
|
variables?: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface CacheFunctionOptions {
|
interface CacheFunctionOptions {
|
||||||
update?: boolean
|
update?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
`;
|
`;
|
||||||
const ops = operations
|
const ops = operations
|
||||||
.map((o) => {
|
.map((o) => {
|
||||||
var _a;
|
var _a;
|
||||||
if (o) {
|
if (o) {
|
||||||
console.log("o", o);
|
|
||||||
const name = ((_a = o === null || o === void 0 ? void 0 : o.name) === null || _a === void 0 ? void 0 : _a.value) || "";
|
const name = ((_a = o === null || o === void 0 ? void 0 : o.name) === null || _a === void 0 ? void 0 : _a.value) || "";
|
||||||
const op = `${pascalCase(name)}${pascalCase(o.operation)}`;
|
const op = `${pascalCase(name)}${pascalCase(o.operation)}`;
|
||||||
const pascalName = pascalCase(name);
|
const pascalName = pascalCase(name);
|
||||||
|
|
@ -46,15 +42,7 @@ interface CacheFunctionOptions {
|
||||||
let operations = "";
|
let operations = "";
|
||||||
if (o.operation === "query") {
|
if (o.operation === "query") {
|
||||||
operations += `
|
operations += `
|
||||||
export const ${name} = writable<${op}>()
|
export const ${name} = writable<GFetchReturnWithErrors<${op}>>()
|
||||||
|
|
||||||
export const fetch${pascalName} = ({ variables, fetch}: FetchWrapperArgs<${opv}>):
|
|
||||||
Promise<GFetchReturnWithErrors<${op}>> =>
|
|
||||||
g.fetch<${op}>({
|
|
||||||
queries: [{ query: ${pascalName}Doc, variables }],
|
|
||||||
fetch
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
// Cached
|
// Cached
|
||||||
export async function get${pascalName}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
|
export async function get${pascalName}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
|
||||||
|
|
@ -62,7 +50,7 @@ export async function get${pascalName}({ fetch, variables }: GGetParameters<${op
|
||||||
queries: [{ query: ${pascalName}Doc, variables }],
|
queries: [{ query: ${pascalName}Doc, variables }],
|
||||||
fetch
|
fetch
|
||||||
})
|
})
|
||||||
await ${name}.set({ ...data, error: data?.error, gQueryStatus: 'LOADED' })
|
await ${name}.set({ ...data, errors: data?.errors, gQueryStatus: 'LOADED' })
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,7 +72,6 @@ Promise<GFetchReturnWithErrors<${op}>> =>
|
||||||
.join("\n");
|
.join("\n");
|
||||||
const imports = [
|
const imports = [
|
||||||
`import { writable } from "svelte/store"`,
|
`import { writable } from "svelte/store"`,
|
||||||
`import type { Writable } from "svelte/store"`,
|
|
||||||
`import { g } from '${config.gPath}'`,
|
`import { g } from '${config.gPath}'`,
|
||||||
`import type { GFetchReturnWithErrors, GGetParameters } from '@leveluptuts/g-query'`,
|
`import type { GFetchReturnWithErrors, GGetParameters } from '@leveluptu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,CAAC;aACO;iBAAM,IAAI,CAAC,CAAC,SAAS,KAAK,UAAU,EAAE;gBACrC,UAAU,IAAI;eACT,IAAI,2CAA2C,GAAG;iCAChC,EAAE;WACxB,EAAE;uBACU,UAAU;;;CAGhC,CAAC;aACO;YAED,OAAO,UAAU,CAAC;SACnB;IACH,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;IAEd,MAAM,OAAO,GAAG;QACd,yCAAyC;QACzC,sBAAsB,MAAM,CAAC,KAAK,GAAG;QACrC,oFAAoF;KACrF,CAAC;IAEF,OAAO;QACL,OAAO,EAAE,CAAC,GAAG,OAAO,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;QAC9C,OAAO,EAAE;YACP,YAAY;YACZ,OAAO,CAAC,SAAS;YACjB,GAAG,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC;YAChE,GAAG;SACJ,CAAC,IAAI,CAAC,IAAI,CAAC;KACb,CAAC;AACJ,CAAC,CAAC;AACF,OAAO;AACP,6EAA6E;AAC7E,4EAA4E"}
|
||||||
|
|
@ -18,8 +18,6 @@ export const plugin: PluginFunction<any> = (
|
||||||
documents: Types.DocumentFile[],
|
documents: Types.DocumentFile[],
|
||||||
config
|
config
|
||||||
) => {
|
) => {
|
||||||
console.log("🛠️ Codegen Starting");
|
|
||||||
|
|
||||||
const allAst = concatAST(documents.map((d) => d.document));
|
const allAst = concatAST(documents.map((d) => d.document));
|
||||||
|
|
||||||
const allFragments: LoadedFragment[] = [
|
const allFragments: LoadedFragment[] = [
|
||||||
|
|
@ -60,17 +58,14 @@ type SubscribeWrapperArgs<T> = {
|
||||||
variables?: T,
|
variables?: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface CacheFunctionOptions {
|
interface CacheFunctionOptions {
|
||||||
update?: boolean
|
update?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const ops = operations
|
const ops = operations
|
||||||
.map((o) => {
|
.map((o) => {
|
||||||
if (o) {
|
if (o) {
|
||||||
console.log("o", o);
|
|
||||||
const name = o?.name?.value || "";
|
const name = o?.name?.value || "";
|
||||||
const op = `${pascalCase(name)}${pascalCase(o.operation)}`;
|
const op = `${pascalCase(name)}${pascalCase(o.operation)}`;
|
||||||
const pascalName = pascalCase(name);
|
const pascalName = pascalCase(name);
|
||||||
|
|
@ -79,15 +74,7 @@ interface CacheFunctionOptions {
|
||||||
|
|
||||||
if (o.operation === "query") {
|
if (o.operation === "query") {
|
||||||
operations += `
|
operations += `
|
||||||
export const ${name} = writable<${op}>()
|
export const ${name} = writable<GFetchReturnWithErrors<${op}>>()
|
||||||
|
|
||||||
export const fetch${pascalName} = ({ variables, fetch}: FetchWrapperArgs<${opv}>):
|
|
||||||
Promise<GFetchReturnWithErrors<${op}>> =>
|
|
||||||
g.fetch<${op}>({
|
|
||||||
queries: [{ query: ${pascalName}Doc, variables }],
|
|
||||||
fetch
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
// Cached
|
// Cached
|
||||||
export async function get${pascalName}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
|
export async function get${pascalName}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
|
||||||
|
|
@ -95,7 +82,7 @@ export async function get${pascalName}({ fetch, variables }: GGetParameters<${op
|
||||||
queries: [{ query: ${pascalName}Doc, variables }],
|
queries: [{ query: ${pascalName}Doc, variables }],
|
||||||
fetch
|
fetch
|
||||||
})
|
})
|
||||||
await ${name}.set({ ...data, error: data?.error, gQueryStatus: 'LOADED' })
|
await ${name}.set({ ...data, errors: data?.errors, gQueryStatus: 'LOADED' })
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,7 +105,6 @@ Promise<GFetchReturnWithErrors<${op}>> =>
|
||||||
|
|
||||||
const imports = [
|
const imports = [
|
||||||
`import { writable } from "svelte/store"`,
|
`import { writable } from "svelte/store"`,
|
||||||
`import type { Writable } from "svelte/store"`,
|
|
||||||
`import { g } from '${config.gPath}'`,
|
`import { g } from '${config.gPath}'`,
|
||||||
`import type { GFetchReturnWithErrors, GGetParameters } from '@leveluptuts/g-query'`,
|
`import type { GFetchReturnWithErrors, GGetParameters } from '@leveluptuts/g-query'`,
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
import { generate } from "@graphql-codegen/cli";
|
import { generate } from "@graphql-codegen/cli";
|
||||||
|
import { execaCommand } from "execa";
|
||||||
const fileRegex = /\.(graphql)$/; // not used, will be for the vite rerun
|
const fileRegex = /\.(graphql)$/; // not used, will be for the vite rerun
|
||||||
export default function levelupViteCodegen(options) {
|
export default function levelupViteCodegen(options) {
|
||||||
if (!options.schema) {
|
if (!options.schema) {
|
||||||
throw new Error("No schema provided");
|
throw new Error("No schema provided");
|
||||||
}
|
}
|
||||||
if (!options.output) {
|
if (!options.output) {
|
||||||
throw new Error("No output directory specified");
|
throw new Error("No output directory specified for types.");
|
||||||
}
|
}
|
||||||
if (!options.gPath) {
|
if (!options.gPath) {
|
||||||
throw new Error("No gPath directory specified. gPath is where you've initialized the 'g' client");
|
throw new Error("No gPath directory specified. gPath is where you've initialized the 'g' client");
|
||||||
|
|
@ -17,8 +18,14 @@ export default function levelupViteCodegen(options) {
|
||||||
async buildStart() {
|
async buildStart() {
|
||||||
try {
|
try {
|
||||||
// *1. Remove all .gq files
|
// *1. Remove all .gq files
|
||||||
// TODO: Find and remove all .gq files
|
console.log("🧹 removing all .gq files");
|
||||||
|
// Find and remove all .gq files
|
||||||
|
await execaCommand("find ./ -path '*.gq.ts' -type f -prune -print -exec rm -f '{}' +; ", {
|
||||||
|
stdio: "inherit",
|
||||||
|
shell: true,
|
||||||
|
});
|
||||||
// *2. Generate
|
// *2. Generate
|
||||||
|
console.log("🤖 starting codegen");
|
||||||
await generate({
|
await generate({
|
||||||
schema,
|
schema,
|
||||||
documents: "./src/**/*.graphql",
|
documents: "./src/**/*.graphql",
|
||||||
|
|
@ -50,7 +57,8 @@ export default function levelupViteCodegen(options) {
|
||||||
}, true);
|
}, true);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.log("❓ gFetch Warning - No `.graphql` files found.");
|
console.log("❓ gFetch Error - Something Happened - Here is the error and some things to consider.", e);
|
||||||
|
console.log("❓ gFetch Error - Make sure `.graphql` are 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 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.");
|
console.log("❓ gFetch Warning - If you would like to add them, you will need to restart SvelteKit.");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,MAAM,SAAS,GAAG,cAAc,CAAC,CAAC,uCAAuC;AAEzE,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,OAAO;IAChD,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;QACnB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;KACvC;IACD,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;QACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;KAClD;IACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE;QAClB,MAAM,IAAI,KAAK,CACb,gFAAgF,CACjF,CAAC;KACH;IAED,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC;IAE1C,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAC9B,OAAO;QACL,I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D,mBAAmB,EAAE,SAAS;6BAC/B;4BACD,MAAM,EAAE,qBAAqB;4BAC7B,YAAY,EAAE;gCACZ,SAAS,EAAE,QAAQ;gCACnB,MAAM,EAAE,IAAI;gCACZ,aAAa,EAAE,aAAa;6BAC7B;4BACD,OAAO,EAAE;gCACP,uBAAuB;gCACvB,qCAAqC,EAAE,0BAA0B;6BAClE;yBACF;qBACF;iBACF,EACD,IAAI,CACL,CAAC;aACH;YAAC,OAAO,CAAC,EAAE;gBACV,OAAO,CAAC,GAAG,CACT,sFAAsF,EACtF,CAAC,CACF,CAAC;gBACF,OAAO,CAAC,GAAG,CAAC,wDAAwD,CAAC,CAAC;gBACtE,OAAO,CAAC,GAAG,CACT,0FAA0F,CAC3F,CAAC;gBACF,OAAO,CAAC,GAAG,CACT,uFAAuF,CACxF,CAAC;aACH;YACD,OAAO;QACT,CAAC;QAED,SAAS,CAAC,GAAG,EAAE,EAAE,IAAG,CAAC;KACtB,CAAC;AACJ,CAAC"}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { generate } from "@graphql-codegen/cli";
|
import { generate } from "@graphql-codegen/cli";
|
||||||
|
import { execaCommand } from "execa";
|
||||||
const fileRegex = /\.(graphql)$/; // not used, will be for the vite rerun
|
const fileRegex = /\.(graphql)$/; // not used, will be for the vite rerun
|
||||||
|
|
||||||
export default function levelupViteCodegen(options) {
|
export default function levelupViteCodegen(options) {
|
||||||
|
|
@ -6,7 +7,7 @@ export default function levelupViteCodegen(options) {
|
||||||
throw new Error("No schema provided");
|
throw new Error("No schema provided");
|
||||||
}
|
}
|
||||||
if (!options.output) {
|
if (!options.output) {
|
||||||
throw new Error("No output directory specified");
|
throw new Error("No output directory specified for types.");
|
||||||
}
|
}
|
||||||
if (!options.gPath) {
|
if (!options.gPath) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|
@ -22,9 +23,18 @@ export default function levelupViteCodegen(options) {
|
||||||
async buildStart() {
|
async buildStart() {
|
||||||
try {
|
try {
|
||||||
// *1. Remove all .gq files
|
// *1. Remove all .gq files
|
||||||
// TODO: Find and remove all .gq files
|
console.log("🧹 removing all .gq files");
|
||||||
// *2. Generate
|
// Find and remove all .gq files
|
||||||
|
await execaCommand(
|
||||||
|
"find ./ -path '*.gq.ts' -type f -prune -print -exec rm -f '{}' +; ",
|
||||||
|
{
|
||||||
|
stdio: "inherit",
|
||||||
|
shell: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// *2. Generate
|
||||||
|
console.log("🤖 starting codegen");
|
||||||
await generate(
|
await generate(
|
||||||
{
|
{
|
||||||
schema,
|
schema,
|
||||||
|
|
@ -58,7 +68,11 @@ export default function levelupViteCodegen(options) {
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("❓ gFetch Warning - No `.graphql` files found.");
|
console.log(
|
||||||
|
"❓ gFetch Error - Something Happened - Here is the error and some things to consider.",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
console.log("❓ gFetch Error - Make sure `.graphql` are files found.");
|
||||||
console.log(
|
console.log(
|
||||||
"❓ gFetch Warning - If you would like the gQuery generator to work (we reccomend you do)."
|
"❓ gFetch Warning - If you would like the gQuery generator to work (we reccomend you do)."
|
||||||
);
|
);
|
||||||
|
|
|
||||||
17
dist/gFetch.d.ts
vendored
17
dist/gFetch.d.ts
vendored
|
|
@ -1,6 +1,7 @@
|
||||||
import { DefinitionNode, DocumentNode } from "graphql";
|
import { DefinitionNode, DocumentNode } from "graphql";
|
||||||
export declare type GFetchQueryDefault = {
|
export declare type GFetchQueryDefault = {
|
||||||
errors?: string[];
|
errors?: Error[];
|
||||||
|
gQueryStatus: "LOADED";
|
||||||
};
|
};
|
||||||
declare type OptionalPropertyNames<T> = {
|
declare type OptionalPropertyNames<T> = {
|
||||||
[K in keyof T]-?: {} extends {
|
[K in keyof T]-?: {} extends {
|
||||||
|
|
@ -15,18 +16,10 @@ declare type Id<T> = T extends infer U ? {
|
||||||
} : never;
|
} : never;
|
||||||
declare type SpreadTwo<L, R> = Id<Pick<L, Exclude<keyof L, keyof R>> & Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> & Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> & SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L>>;
|
declare type SpreadTwo<L, R> = Id<Pick<L, Exclude<keyof L, keyof R>> & Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> & Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> & SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L>>;
|
||||||
declare type Spread<A extends readonly [...any]> = A extends [infer L, ...infer R] ? SpreadTwo<L, Spread<R>> : unknown;
|
declare type Spread<A extends readonly [...any]> = A extends [infer L, ...infer R] ? SpreadTwo<L, Spread<R>> : unknown;
|
||||||
export declare type GFetchQueryResult<F> = {
|
|
||||||
[k: string]: F;
|
|
||||||
};
|
|
||||||
export declare type GFetchQueries = {
|
export declare type GFetchQueries = {
|
||||||
query: DocumentNode;
|
query: DocumentNode;
|
||||||
variables?: Record<string, unknown>;
|
variables?: Record<string, unknown>;
|
||||||
};
|
};
|
||||||
export declare function gqlToString(tag: DocumentNode): string;
|
|
||||||
/**
|
|
||||||
* Finds the Name value from the OperationDefinition of a Document
|
|
||||||
*/
|
|
||||||
export declare const getOperationName: (query: DocumentNode) => string | undefined;
|
|
||||||
export declare const stringifyDocument: (node: string | DefinitionNode | DocumentNode) => string;
|
export declare const stringifyDocument: (node: string | DefinitionNode | DocumentNode) => string;
|
||||||
declare type gFetchProperties = {
|
declare type gFetchProperties = {
|
||||||
queries: GFetchQueries[];
|
queries: GFetchQueries[];
|
||||||
|
|
@ -35,10 +28,6 @@ declare type gFetchProperties = {
|
||||||
export declare type GClientOptions = {
|
export declare type GClientOptions = {
|
||||||
path?: string;
|
path?: string;
|
||||||
};
|
};
|
||||||
export declare type GReturn<T> = {
|
|
||||||
data: T;
|
|
||||||
errors?: Error;
|
|
||||||
};
|
|
||||||
export declare type GGetParameters<Variables> = {
|
export declare type GGetParameters<Variables> = {
|
||||||
variables?: Variables;
|
variables?: Variables;
|
||||||
fetch: typeof fetch;
|
fetch: typeof fetch;
|
||||||
|
|
@ -47,6 +36,6 @@ export declare type GFetchReturnWithErrors<T> = Spread<[T, GFetchQueryDefault]>;
|
||||||
export declare class GFetch extends Object {
|
export declare class GFetch extends Object {
|
||||||
path: string;
|
path: string;
|
||||||
constructor(options: GClientOptions);
|
constructor(options: GClientOptions);
|
||||||
fetch<T>({ queries, fetch, }?: gFetchProperties | {}): Promise<GFetchReturnWithErrors<T>>;
|
fetch<T>({ queries, fetch, }: gFetchProperties | undefined): Promise<GFetchReturnWithErrors<T>>;
|
||||||
}
|
}
|
||||||
export {};
|
export {};
|
||||||
|
|
|
||||||
29
dist/gFetch.js
vendored
29
dist/gFetch.js
vendored
|
|
@ -1,21 +1,4 @@
|
||||||
import { Kind, print } from "graphql";
|
import { print } from "graphql";
|
||||||
import { formatDocument as addTypenameToDocument } from "./utils/format";
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Finds the Name value from the OperationDefinition of a Document
|
|
||||||
*/
|
|
||||||
export const getOperationName = (query) => {
|
|
||||||
for (let i = 0, l = query.definitions.length; i < l; i++) {
|
|
||||||
const node = query.definitions[i];
|
|
||||||
if (node.kind === Kind.OPERATION_DEFINITION && node.name) {
|
|
||||||
return node.name.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
export const stringifyDocument = (node) => {
|
export const stringifyDocument = (node) => {
|
||||||
let str = (typeof node !== "string" ? print(node) : node)
|
let str = (typeof node !== "string" ? print(node) : node)
|
||||||
.replace(/([\s,]|#[^\n\r]+)+/g, " ")
|
.replace(/([\s,]|#[^\n\r]+)+/g, " ")
|
||||||
|
|
@ -31,9 +14,9 @@ export class GFetch extends Object {
|
||||||
}
|
}
|
||||||
// * gFetch
|
// * gFetch
|
||||||
// This is a fetcher that returns a promise that resolves to a graphql response
|
// This is a fetcher that returns a promise that resolves to a graphql response
|
||||||
async fetch({ queries, fetch, } = {}) {
|
async fetch({ queries, fetch, }) {
|
||||||
let document = addTypenameToDocument(queries[0].query);
|
// let document: DocumentNode = addTypenameToDocument(queries[0].query);
|
||||||
let documentString = stringifyDocument(document);
|
let documentString = stringifyDocument(queries[0].query);
|
||||||
const newQueries = {
|
const newQueries = {
|
||||||
...queries[0],
|
...queries[0],
|
||||||
query: documentString,
|
query: documentString,
|
||||||
|
|
@ -53,10 +36,6 @@ export class GFetch extends Object {
|
||||||
...data.data,
|
...data.data,
|
||||||
errors: data.errors,
|
errors: data.errors,
|
||||||
};
|
};
|
||||||
// } as GFetchReturnWithErrors<T>;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ! IDEAS
|
|
||||||
// Mutations should take care of updating a generated writeable.
|
|
||||||
// $tutorial is auto updated site wide
|
|
||||||
//# sourceMappingURL=gFetch.js.map
|
//# sourceMappingURL=gFetch.js.map
|
||||||
2
dist/gFetch.js.map
vendored
2
dist/gFetch.js.map
vendored
|
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"gFetch.js","sourceRoot":"","sources":["../src/gFetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEpE,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAkDzE,2EAA2E;AAC3E,iBAAiB;AACjB,MAAM,UAAU,WAAW,CAAC,GAAiB;IAC3C,OAAO,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;AAC7B,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAmB,EAAsB,EAAE;IAC1E,KAAK,IAAI,CAAC,GAAG,CAAC,E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AAC,KAAK,CAAI,EACpB,OAAO,EACP,KAAK,GACwB;QAC7B,wEAAwE;QACxE,IAAI,cAAc,GAAW,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACjE,MAAM,UAAU,GAAG;YACjB,GAAG,OAAO,CAAC,CAAC,CAAC;YACb,KAAK,EAAE,cAAc;SACtB,CAAC;QAEF,2DAA2D;QAC3D,gCAAgC;QAChC,wCAAwC;QACxC,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;YACjC,MAAM,EAAE,MAAM;YACd,WAAW,EAAE,SAAS;YACtB,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC;SACjC,CAAC,CAAC;QAEH,qCAAqC;QACrC,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;QAE9B,OAAO;YACL,GAAG,IAAI,CAAC,IAAI;YACZ,MAAM,EAAE,IAAI,CAAC,MAAM;SACpB,CAAC;IACJ,CAAC;CACF"}
|
||||||
13
dist/index.d.ts
vendored
13
dist/index.d.ts
vendored
|
|
@ -1,14 +1 @@
|
||||||
export declare const gCache: {
|
|
||||||
subscribe: (this: void, run: import("svelte/store").Subscriber<{}>, invalidate?: (value?: {}) => void) => import("svelte/store").Unsubscriber;
|
|
||||||
set: (this: void, value: {}) => void;
|
|
||||||
update: (this: void, updater: import("svelte/store").Updater<{}>) => void;
|
|
||||||
hydrate: (newData: any) => Promise<void>;
|
|
||||||
};
|
|
||||||
interface CacheFunctionOptions {
|
|
||||||
update?: boolean;
|
|
||||||
}
|
|
||||||
export declare function gQuery(typename: any, { query, variables }: {
|
|
||||||
query: any;
|
|
||||||
variables: any;
|
|
||||||
}, { update }?: CacheFunctionOptions): Promise<void>;
|
|
||||||
export * from "./gFetch";
|
export * from "./gFetch";
|
||||||
|
|
|
||||||
33
dist/index.js
vendored
33
dist/index.js
vendored
|
|
@ -1,35 +1,2 @@
|
||||||
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 based on the query, 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";
|
export * from "./gFetch";
|
||||||
//# sourceMappingURL=index.js.map
|
//# sourceMappingURL=index.js.map
|
||||||
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
|
|
@ -1 +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,8EAA8E;IAC9E,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"}
|
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"camel-case": "^4.1.2",
|
"camel-case": "^4.1.2",
|
||||||
|
"execa": "^6.0.0",
|
||||||
"pascal-case": "^3.1.2",
|
"pascal-case": "^3.1.2",
|
||||||
"svelte": "^3.44.0",
|
"svelte": "^3.44.0",
|
||||||
"vite": "^2.6.10"
|
"vite": "^2.6.10"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
import { DefinitionNode, DocumentNode, Kind, print } from "graphql";
|
import { DefinitionNode, DocumentNode, print } from "graphql";
|
||||||
|
|
||||||
import { formatDocument as addTypenameToDocument } from "./utils/format";
|
|
||||||
|
|
||||||
// What's the deal with *gFetch*?
|
// What's the deal with *gFetch*?
|
||||||
// gFetch is a 0 dependency fetcher for graphql that accepts a custom fetch function
|
// gFetch is a 0 dependency fetcher for graphql that accepts a custom fetch function
|
||||||
|
|
@ -17,7 +15,8 @@ import { formatDocument as addTypenameToDocument } from "./utils/format";
|
||||||
// 3. Plays nice with custom fetch functions
|
// 3. Plays nice with custom fetch functions
|
||||||
// 4. Doesn't use it's own cache, ie, would rely on Svelte's stores
|
// 4. Doesn't use it's own cache, ie, would rely on Svelte's stores
|
||||||
export declare type GFetchQueryDefault = {
|
export declare type GFetchQueryDefault = {
|
||||||
errors?: string[];
|
errors?: Error[];
|
||||||
|
gQueryStatus: "LOADED";
|
||||||
};
|
};
|
||||||
|
|
||||||
type OptionalPropertyNames<T> = {
|
type OptionalPropertyNames<T> = {
|
||||||
|
|
@ -41,33 +40,11 @@ type Spread<A extends readonly [...any]> = A extends [infer L, ...infer R]
|
||||||
? SpreadTwo<L, Spread<R>>
|
? SpreadTwo<L, Spread<R>>
|
||||||
: unknown;
|
: unknown;
|
||||||
|
|
||||||
export declare type GFetchQueryResult<F> = {
|
|
||||||
[k: string]: F;
|
|
||||||
};
|
|
||||||
|
|
||||||
export declare type GFetchQueries = {
|
export declare type GFetchQueries = {
|
||||||
query: DocumentNode;
|
query: DocumentNode;
|
||||||
variables?: Record<string, unknown>;
|
variables?: Record<string, unknown>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// This function accepts a graphql document and returns a string to be used
|
|
||||||
// in fetch calls
|
|
||||||
export function gqlToString(tag: DocumentNode): string {
|
|
||||||
return tag.loc.source.body;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds the Name value from the OperationDefinition of a Document
|
|
||||||
*/
|
|
||||||
export const getOperationName = (query: DocumentNode): string | undefined => {
|
|
||||||
for (let i = 0, l = query.definitions.length; i < l; i++) {
|
|
||||||
const node = query.definitions[i];
|
|
||||||
if (node.kind === Kind.OPERATION_DEFINITION && node.name) {
|
|
||||||
return node.name.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const stringifyDocument = (
|
export const stringifyDocument = (
|
||||||
node: string | DefinitionNode | DocumentNode
|
node: string | DefinitionNode | DocumentNode
|
||||||
): string => {
|
): string => {
|
||||||
|
|
@ -86,11 +63,6 @@ export type GClientOptions = {
|
||||||
path?: string;
|
path?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type GReturn<T> = {
|
|
||||||
data: T;
|
|
||||||
errors?: Error;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type GGetParameters<Variables> = {
|
export type GGetParameters<Variables> = {
|
||||||
variables?: Variables;
|
variables?: Variables;
|
||||||
fetch: typeof fetch;
|
fetch: typeof fetch;
|
||||||
|
|
@ -113,9 +85,9 @@ export class GFetch extends Object {
|
||||||
public async fetch<T>({
|
public async fetch<T>({
|
||||||
queries,
|
queries,
|
||||||
fetch,
|
fetch,
|
||||||
}: gFetchProperties | {} = {}): Promise<GFetchReturnWithErrors<T>> {
|
}: gFetchProperties | undefined): Promise<GFetchReturnWithErrors<T>> {
|
||||||
let document: DocumentNode = addTypenameToDocument(queries[0].query);
|
// let document: DocumentNode = addTypenameToDocument(queries[0].query);
|
||||||
let documentString: string = stringifyDocument(document);
|
let documentString: string = stringifyDocument(queries[0].query);
|
||||||
const newQueries = {
|
const newQueries = {
|
||||||
...queries[0],
|
...queries[0],
|
||||||
query: documentString,
|
query: documentString,
|
||||||
|
|
@ -138,10 +110,5 @@ export class GFetch extends Object {
|
||||||
...data.data,
|
...data.data,
|
||||||
errors: data.errors,
|
errors: data.errors,
|
||||||
};
|
};
|
||||||
// } as GFetchReturnWithErrors<T>;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ! IDEAS
|
|
||||||
// Mutations should take care of updating a generated writeable.
|
|
||||||
// $tutorial is auto updated site wide
|
|
||||||
|
|
|
||||||
49
src/index.ts
49
src/index.ts
|
|
@ -1,50 +1 @@
|
||||||
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();
|
|
||||||
|
|
||||||
interface CacheFunctionOptions {
|
|
||||||
update?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function gQuery(
|
|
||||||
typename,
|
|
||||||
{ query, variables },
|
|
||||||
{ update }: CacheFunctionOptions = {}
|
|
||||||
) {
|
|
||||||
const current = get(gCache);
|
|
||||||
|
|
||||||
// Extremely Naive cache
|
|
||||||
// Just checks to see if the data is there based on the query, 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";
|
export * from "./gFetch";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue