Skip to content

Commit a8bd534

Browse files
feat: refactor type imports and enhance GraphQL schema definitions
1 parent af53be0 commit a8bd534

File tree

8 files changed

+87
-25
lines changed

8 files changed

+87
-25
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
"types": "./dist/utils/index.d.ts",
3232
"import": "./dist/utils/index.js"
3333
},
34-
"./types": {
35-
"types": "./dist/types.d.ts",
36-
"import": "./dist/types.js"
37-
},
3834
"./internal": {
3935
"types": "./dist/internal/index.d.ts",
4036
"import": "./dist/internal/index.js"

playground-nuxt/nuxt.config.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// TODO: temporary solution
2-
import 'nitro-graphql/types'
2+
// import 'nitro-graphql/types'
33

44
export default defineNuxtConfig({
55
compatibilityDate: '2024-07-01',
@@ -11,19 +11,7 @@ export default defineNuxtConfig({
1111
modules: ['nitro-graphql'],
1212
// GraphQL Yoga configuration
1313
graphql: {
14-
endpoint: '/api/graphql',
15-
playground: true,
16-
cors: {
17-
origin: '*',
18-
credentials: true,
19-
},
20-
client: {
21-
enabled: true,
22-
watchPatterns: [
23-
'server/graphql/**/*.graphql',
24-
'server/graphql/**/*.gql',
25-
],
26-
},
14+
2715
},
2816
},
2917

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { rollupConfig } from './rollup'
1111
import { relativeWithDot, scanDefs, scanResolvers } from './utils'
1212
import { clientTypeGeneration, serverTypeGeneration } from './utils/server-type-generation'
1313

14+
export type * from './types'
15+
16+
// This is the main entry point for the nitro-graphql module.
17+
// It sets up the module, configures the GraphQL endpoints, and handles type generation.
18+
export interface Resolvers {}
19+
1420
export default defineNitroModule({
1521
name: 'nitro-graphql',
1622
async setup(nitro: Nitro) {
@@ -169,7 +175,7 @@ export default defineNitroModule({
169175
const graphqlDtsContent = `// Auto-generated by nitro-graphql
170176
import type { Resolvers as Test } from './nitro-graphql-server.d.ts'
171177
172-
declare module 'nitro-graphql/types' {
178+
declare module 'nitro-graphql' {
173179
interface Resolvers extends Test {}
174180
}
175181
`

src/types.ts renamed to src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { IResolvers } from '@graphql-tools/utils'
55
import type { YogaServerOptions } from 'graphql-yoga'
66
import type { IncomingMessage, ServerResponse } from 'node:http'
77

8+
export type { StandardSchemaV1 } from './standard-schema'
9+
810
export type CodegenServerConfig = TypeScriptPluginConfig & TypeScriptResolversPluginConfig
911

1012
declare module 'nitropack/types' {
@@ -74,5 +76,3 @@ export interface NitroGraphQLOptions {
7476
}
7577
yogaConfig?: Partial<YogaServerOptions<any, any>>
7678
}
77-
78-
export interface Resolvers {}

src/types/standard-schema.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/** The Standard Schema interface. */
2+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
3+
/** The Standard Schema properties. */
4+
readonly '~standard': StandardSchemaV1.Props<Input, Output>
5+
}
6+
7+
// eslint-disable-next-line ts/no-namespace
8+
export declare namespace StandardSchemaV1 {
9+
/** The Standard Schema properties interface. */
10+
export interface Props<Input = unknown, Output = Input> {
11+
/** The version number of the standard. */
12+
readonly version: 1
13+
/** The vendor name of the schema library. */
14+
readonly vendor: string
15+
/** Validates unknown input values. */
16+
readonly validate: (
17+
value: unknown,
18+
) => Result<Output> | Promise<Result<Output>>
19+
/** Inferred types associated with the schema. */
20+
readonly types?: Types<Input, Output> | undefined
21+
}
22+
23+
/** The result interface of the validate function. */
24+
export type Result<Output> = SuccessResult<Output> | FailureResult
25+
26+
/** The result interface if validation succeeds. */
27+
export interface SuccessResult<Output> {
28+
/** The typed output value. */
29+
readonly value: Output
30+
/** The non-existent issues. */
31+
readonly issues?: undefined
32+
}
33+
34+
/** The result interface if validation fails. */
35+
export interface FailureResult {
36+
/** The issues of failed validation. */
37+
readonly issues: ReadonlyArray<Issue>
38+
}
39+
40+
/** The issue interface of the failure output. */
41+
export interface Issue {
42+
/** The error message of the issue. */
43+
readonly message: string
44+
/** The path of the issue, if any. */
45+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined
46+
}
47+
48+
/** The path segment interface of the issue. */
49+
export interface PathSegment {
50+
/** The key representing a path segment. */
51+
readonly key: PropertyKey
52+
}
53+
54+
/** The Standard Schema types interface. */
55+
export interface Types<Input = unknown, Output = Input> {
56+
/** The input type of the schema. */
57+
readonly input: Input
58+
/** The output type of the schema. */
59+
readonly output: Output
60+
}
61+
62+
/** Infers the input type of a Standard Schema. */
63+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<
64+
Schema['~standard']['types']
65+
>['input']
66+
67+
/** Infers the output type of a Standard Schema. */
68+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<
69+
Schema['~standard']['types']
70+
>['output']
71+
72+
// biome-ignore lint/complexity/noUselessEmptyExport: needed for granular visibility control of TS namespace
73+
export {}
74+
}

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { YogaServerOptions } from 'graphql-yoga'
2-
import type { GraphQLSchemaConfig, Resolvers } from 'nitro-graphql/types'
2+
import type { GraphQLSchemaConfig, Resolvers } from 'nitro-graphql'
33
import type { Nitro } from 'nitropack'
44
import { join, relative } from 'pathe'
55
import { glob } from 'tinyglobby'

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"module": "ESNext",
99
"moduleResolution": "bundler",
1010
"paths": {
11-
"nitro-graphql/types": ["./src/types"],
1211
"#nitro-internal-virtual/*": ["./src/virtual/*"],
1312
"nitro-graphql/internal": ["./src/internal/index.ts"]
1413
},

tsdown.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default defineConfig({
1515
'src/client-watcher.ts',
1616
'src/context.ts',
1717
'src/utils/index.ts',
18-
'src/types.ts',
1918
'src/routes',
2019
'src/internal/index.ts',
2120
'src/graphql.d.ts',
@@ -27,7 +26,7 @@ export default defineConfig({
2726
name: 'nitro-graphql',
2827
unbundle: true,
2928
external: [
30-
'nitro-graphql/types',
29+
'nitro-graphql',
3130
'nitropack',
3231
'nitropack/types',
3332
'nitropack/kit',

0 commit comments

Comments
 (0)