Skip to content

Commit

Permalink
chore: update graphql templates to gqty (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoresende committed Aug 3, 2023
1 parent b27a0d6 commit 7f6e3a0
Show file tree
Hide file tree
Showing 21 changed files with 452 additions and 280 deletions.
73 changes: 0 additions & 73 deletions cli/template/graphql/graphql-next-planetscale-auth/builder.ts

This file was deleted.

52 changes: 52 additions & 0 deletions cli/template/graphql/graphql-next-planetscale-auth/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
Cache,
CacheOptions,
createClient as createGQtyClient,
QueryFetcher,
ScalarsEnumsHash,
Schema,
SubscriptionClient,
} from "gqty";

import { type SchemaTypes } from "./index";

export type {
Cache,
QueryFetcher,
ScalarsEnumsHash,
Schema,
SubscriptionClient,
CacheOptions,
} from "gqty";

export type ClientOptions = {
generatedSchema: Schema;
scalarsEnumsHash: ScalarsEnumsHash;
fetcher: QueryFetcher;
fetchOptions?: RequestInit;
subscriptionClient?: SubscriptionClient;
cacheOptions?: CacheOptions;
};

export function createClient<T extends SchemaTypes>(options: ClientOptions) {
type Query = T["query"];
type GeneratedSchema = { query: Query };

const cache = new Cache(undefined, {
maxAge: 0,
staleWhileRevalidate: 5 * 60 * 1000,
normalization: true,
...options.cacheOptions,
});

const client = createGQtyClient<GeneratedSchema>({
scalars: options.scalarsEnumsHash,
schema: options.generatedSchema,
cache,
fetchOptions: {
fetcher: options.fetcher,
},
});

return client;
}
17 changes: 0 additions & 17 deletions cli/template/graphql/graphql-next-planetscale-auth/codegen.ts

This file was deleted.

71 changes: 57 additions & 14 deletions cli/template/graphql/graphql-next-planetscale-auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
import { headers } from 'next/headers';
import { auth } from '@acme/auth';
import { createYoga } from 'graphql-yoga';
import { buildSchema, g, InferResolvers } from "garph";
import { InferClient } from "garph/dist/client";
import { createYoga, YogaInitialContext } from "graphql-yoga";
import { cookies } from "next/headers";
import { type NextRequest } from "next/server";

import { schema } from './schema';
import { auth, Session } from "@acme/auth";

const { handleRequest } = createYoga({
import { createClient, type QueryFetcher } from "./client";
import { createGeneratedSchema, createScalarsEnumsHash } from "./utils";

type Context = YogaInitialContext & {
session: Session | null;
request: NextRequest;
};

const queryType = g.type("Query", {
greetings: g.string().description("Greets a person"),
authorizedOnly: g
.string()
.optional()
.description("Sends a message only to authorized users"),
});

const resolvers: InferResolvers<
{ Query: typeof queryType },
{ context: Context }
> = {
Query: {
greetings: () => {
return "Greetings from GraphQL";
},
authorizedOnly: (_parent, _args, context) => {
if (context.session) {
return "Greetings from protected query";
} else {
return null;
}
},
},
};

export type SchemaTypes = InferClient<{ query: typeof queryType }>;

export const schema = buildSchema({ g, resolvers });

const { handleRequest } = createYoga<Context>({
fetchAPI: { Response },
schema: schema,
graphqlEndpoint: '/api/graphql',
context: async () => {
const authorizationHeader = headers().get('authorization');
const sessionToken = authorizationHeader?.split(' ').pop();
if (sessionToken) {
const session = await auth.validateSessionUser(sessionToken);
return { session };
}
graphqlEndpoint: "/api/graphql",
context: async ({ request }) => {
const authRequest = auth.handleRequest({ cookies, request });
const session = await authRequest.validateBearerToken();
return { session };
},
});

export { handleRequest };
export {
handleRequest,
createGeneratedSchema,
createScalarsEnumsHash,
createClient,
};
export type { QueryFetcher };
32 changes: 12 additions & 20 deletions cli/template/graphql/graphql-next-planetscale-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,22 @@
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"typecheck": "tsc --noEmit",
"codegen:schema": "graphql-codegen --config codegen.ts",
"codegen:client": "npx @genql/cli --output ./genql --schema ./schema.graphql --esm",
"codegen": "pnpm codegen:schema && pnpm codegen:client",
"dev": "chokidar \"**/*.ts\" --silent --initial -i \"node_modules\" -i \"genql\" -c \"pnpm codegen\""
"format": "pnpm prettier . --check --ignore-unknown",
"format:write": "pnpm format --writer"
},
"license": "MIT",
"peerDependencies": {
"@pothos/core": "^3.24.0"
},
"devDependencies": {
"@genql/cli": "3.0.5",
"@graphql-codegen/cli": "^3.3.1",
"@graphql-codegen/schema-ast": "^3.0.1",
"@types/node": "^20.2.5",
"chokidar-cli": "^3.0.0",
"next": "13.4.5"
},
"dependencies": {
"@acme/auth": "file:../../auth/auth-planetscale-next",
"@acme/database": "file:../../database/database-planetscale-auth",
"@pothos/core": "^3.30.0",
"@pothos/plugin-scope-auth": "^3.19.2",
"graphql": "^16.6.0",
"graphql-yoga": "^3.9.1"
"garph": "^0.5.7",
"gqty": "3.0.0-alpha-577a9298.0",
"graphql": "^16.7.1",
"graphql-yoga": "^4.0.3"
},
"devDependencies": {
"@acme/config": "file:../../config",
"@ianvs/prettier-plugin-sort-imports": "^4.1.0",
"next": "13.4.12",
"prettier": "^3.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @typedef {import("@ianvs/prettier-plugin-sort-imports").PluginConfig} SortImportsConfig*/
/** @typedef {import("prettier").Config} PrettierConfig*/
const baseConfig = require("@acme/config/prettier.config");

/** @type { PrettierConfig | SortImportsConfig | TailwindConfig } */
const config = {
...baseConfig,
plugins: [require.resolve("@ianvs/prettier-plugin-sort-imports")],
importOrder: [
"<THIRD_PARTY_MODULES>",
"",
"^@acme/(.*)$",
"",
"^@/(.*)$",
"^[./]",
],
importOrderParserPlugins: ["typescript", "decorators-legacy"],
};

module.exports = config;
6 changes: 0 additions & 6 deletions cli/template/graphql/graphql-next-planetscale-auth/schema.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {},
"extends": "../../tsconfig.json",
"include": [
"index.ts"
]
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7f6e3a0

Please sign in to comment.