-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update graphql templates to gqty (#24)
- Loading branch information
1 parent
b27a0d6
commit 7f6e3a0
Showing
21 changed files
with
452 additions
and
280 deletions.
There are no files selected for viewing
73 changes: 0 additions & 73 deletions
73
cli/template/graphql/graphql-next-planetscale-auth/builder.ts
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
cli/template/graphql/graphql-next-planetscale-auth/client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
cli/template/graphql/graphql-next-planetscale-auth/codegen.ts
This file was deleted.
Oops, something went wrong.
71 changes: 57 additions & 14 deletions
71
cli/template/graphql/graphql-next-planetscale-auth/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
cli/template/graphql/graphql-next-planetscale-auth/prettier.config.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
cli/template/graphql/graphql-next-planetscale-auth/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
10 changes: 0 additions & 10 deletions
10
cli/template/graphql/graphql-next-planetscale-auth/types/greeting.ts
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
cli/template/graphql/graphql-next-planetscale-auth/types/protected.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.