Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Headers injection and documents path config #24

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release

on:
push:
branches:
- main

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0
- uses: pnpm/[email protected]
with:
version: 6
- name: Setup Node.js 16.x
uses: actions/setup-node@v2
with:
node-version: 16.x
registry-url: https://registry.npmjs.org/
cache: pnpm
- run: pnpm publish --no-git-checks --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: npm install -g pnpm && pnpm install
command: npm install -g pnpm && pnpm dev
vscode:
extensions:
- dbaeumer.vscode-eslint

20 changes: 9 additions & 11 deletions codegen/codegen.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/codegen.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const plugin: PluginFunction<any> = (
);

// Visit all the documents
const visitorResult = oldVisit(allAst, { leave: visitor });
const visitorResult = oldVisit(allAst, { leave: visitor as any });

// Filter out the operations
const operations = allAst.definitions.filter(
Expand All @@ -63,6 +63,8 @@ export const plugin: PluginFunction<any> = (
const defaultTypes = `
type SubscribeWrapperArgs<T> = {
variables?: T,
headers?: { [key: string]: string },
fetch?: typeof fetch;
}

interface CacheFunctionOptions {
Expand All @@ -88,10 +90,11 @@ export const ${name} = writable<GFetchReturnWithErrors<${op}>>({
})

// Cached
export async function get${pascalName}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
export async function get${pascalName}({ fetch: f, variables, headers }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
const data = await g.fetch<${op}>({
queries: [{ query: ${pascalName}Doc, variables }],
fetch
fetch: f || fetch,
headers,
})
${name}.set({ ...data, errors: data?.errors, gQueryStatus: 'LOADED' })
return data
Expand All @@ -102,11 +105,12 @@ export async function get${pascalName}({ fetch, variables }: GGetParameters<${op
// This is where the mutation code is generated
// We're grabbing the mutation name and using it as a string in the generated code
operations += `
export const ${name} = ({ variables }: SubscribeWrapperArgs<${opv}>):
export const ${name} = ({ variables, headers, fetch: f }: SubscribeWrapperArgs<${opv}>):
Promise<GFetchReturnWithErrors<${op}>> =>
g.fetch<${op}>({
queries: [{ query: ${pascalName}Doc, variables }],
fetch,
fetch: f || fetch,
headers,
})
`;
}
Expand All @@ -120,7 +124,7 @@ Promise<GFetchReturnWithErrors<${op}>> =>
const imports = [
`import { writable } from "svelte/store"`,
`import { g } from '${config.gPath}'`,
`import type { GFetchReturnWithErrors, GGetParameters } from '@leveluptuts/g-query'`,
`import type { GFetchReturnWithErrors, GGetParameters } from '@juanvillacortac/g-query'`,
];

return {
Expand Down
3 changes: 2 additions & 1 deletion codegen/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Plugin } from "vite";
export default function levelupViteCodegen({ schema, out, gPath, debug, }: {
export default function levelupViteCodegen({ schema, out, gPath, documents, debug, }: {
schema: any;
out: any;
gPath: any;
documents?: string;
debug?: boolean;
}): Plugin;
10 changes: 5 additions & 5 deletions codegen/plugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/plugin.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions codegen/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ async function cleanGQ({ debug = false }) {
}

// Runs graphql codegen
async function gQueryGenerate({ schema, out, gPath, debug = false }) {
async function gQueryGenerate({ schema, out, gPath, debug = false, documents = "./src/**/*.graphql" }) {
debug && console.log("🤖 starting codegen");

// the actual codegen process.
await generate(
{
schema,
documents: "./src/**/*.graphql",
documents,
generates: {
// * Generates the types for your schema
[`${process.cwd()}/${out}/types.gq.ts`]: {
Expand All @@ -49,7 +49,7 @@ async function gQueryGenerate({ schema, out, gPath, debug = false }) {
},
plugins: [
"typescript-operations", // operations, gets you types for operations (queries and mutations)
"@leveluptuts/g-query/codegen-plugin", // g-query codegen plugin. ./codegen.ts
"@juanvillacortac/g-query/codegen-plugin", // g-query codegen plugin. ./codegen.ts
],
},
},
Expand All @@ -62,6 +62,7 @@ export default function levelupViteCodegen({
schema,
out,
gPath,
documents = "./src/**/*.graphql",
debug = false,
}): Plugin {
if (!schema) {
Expand All @@ -82,7 +83,7 @@ export default function levelupViteCodegen({
console.log("build start");
try {
await cleanGQ({ debug });
await gQueryGenerate({ schema, out, gPath, debug });
await gQueryGenerate({ schema, out, gPath, debug, documents });

return;
} catch (e) {
Expand Down
16 changes: 14 additions & 2 deletions dist/gFetch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,30 @@ export declare const stringifyDocument: (node: string | DefinitionNode | Documen
declare type gFetchProperties = {
queries: GFetchQueries[];
fetch: typeof fetch;
headers?: {
[key: string]: string;
};
};
export declare type GClientOptions = {
path?: string;
headers?: {
[key: string]: string;
};
};
export declare type GGetParameters<Variables> = {
variables?: Variables;
fetch: typeof fetch;
fetch?: typeof fetch;
headers?: {
[key: string]: string;
};
};
export declare type GFetchReturnWithErrors<T> = Spread<[T, GFetchQueryDefault]>;
export declare class GFetch extends Object {
path: string;
headers: {
[key: string]: string;
};
constructor(options: GClientOptions);
fetch<T>({ queries, fetch, }: gFetchProperties | undefined): Promise<GFetchReturnWithErrors<T>>;
fetch<T>({ queries, fetch, headers, }: gFetchProperties | undefined): Promise<GFetchReturnWithErrors<T>>;
}
export {};
Loading