Skip to content

Commit 8e12e0b

Browse files
authored
feat(cli): generate from introspection (#1014)
1 parent ddf27ed commit 8e12e0b

File tree

21 files changed

+566
-68
lines changed

21 files changed

+566
-68
lines changed

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import configPrisma from 'eslint-config-prisma'
22
import tsEslint from 'typescript-eslint'
33

44
export default tsEslint.config({
5-
ignores: ['**/build/**/*', 'eslint.config.js', 'vite.config.ts', '**/generated/**/*'],
5+
ignores: ['**/build/**/*', 'eslint.config.js', 'vite.config.ts', '**/generated/**/*', '**/generated-clients/**/*'],
66
extends: configPrisma,
77
languageOptions: {
88
parserOptions: {

examples/config-fetch.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
/* eslint-disable */
22

3-
import { gql, Graffle } from '../src/entrypoints/alpha/main.js'
3+
import { CountriesClient } from './generated-clients/countries/__.js'
44

5-
const request = Graffle.create({ schema: `https://countries.trevorblades.com/graphql` }).use({
6-
name: `CustomFetch`,
7-
anyware: async ({ exchange }) => {
8-
return await exchange({
9-
using: {
10-
fetch: async () => {
11-
return new Response(JSON.stringify({ data: { countries: [{ name: `USA` }] } }))
5+
// todo: if used introspection query to get schema, then default schema to that URL.
6+
// todo: https://github.com/jasonkuhrt/graphql-request/issues/1015
7+
const countriesClient = CountriesClient.create({ schema: `https://countries.trevorblades.com/graphql` })
8+
.use({
9+
name: `CustomFetch`,
10+
anyware: async ({ exchange }) => {
11+
return await exchange({
12+
using: {
13+
fetch: async () => {
14+
return new Response(JSON.stringify({ data: { countries: [{ name: `Canada Mocked!` }] } }))
15+
},
1216
},
13-
},
14-
})
15-
},
16-
}).rawOrThrow
17+
})
18+
},
19+
})
1720

18-
const { data } = await request(
19-
gql`
20-
{
21-
countries {
22-
name
23-
}
24-
}
25-
`,
26-
)
21+
const countries = await countriesClient.query.countries({ name: true })
2722

28-
console.log(data)
23+
console.log(countries)

examples/config-http-headers.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
/* eslint-disable */
1+
import { CountriesClient } from './generated-clients/countries/__.js'
22

3-
import { gql, Graffle } from '../src/entrypoints/alpha/main.js'
4-
5-
const request = Graffle.create({
3+
const request = CountriesClient.create({
64
schema: `https://countries.trevorblades.com/graphql`,
75
headers: {
86
authorization: `Bearer MY_TOKEN`,
97
},
10-
}).rawOrThrow
8+
})
119

12-
const { data } = await request(
13-
gql`
14-
{
15-
countries {
16-
name
17-
}
18-
}
19-
`,
20-
)
10+
const continents = await request.query.continents({ name: true })
2111

22-
console.log(data)
12+
console.log(continents)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createPrefilled } from '../../../src/entrypoints/alpha/client.js'
2+
3+
import { $Index } from './SchemaRuntime.js'
4+
5+
export const create = createPrefilled(`countriesClient`, $Index)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type Include<T, U> = Exclude<T, Exclude<T, U>>
2+
3+
type ObjectWithTypeName = { __typename: string }
4+
5+
const ErrorObjectsTypeNameSelectedEnum = {} as Record<string, ObjectWithTypeName>
6+
7+
const ErrorObjectsTypeNameSelected = Object.values(ErrorObjectsTypeNameSelectedEnum)
8+
9+
type ErrorObjectsTypeNameSelected = (typeof ErrorObjectsTypeNameSelected)[number]
10+
11+
export const isError = <$Value>(value: $Value): value is Include<$Value, ErrorObjectsTypeNameSelected> => {
12+
return typeof value === 'object' && value !== null && '__typename' in value
13+
&& ErrorObjectsTypeNameSelected.some(_ => _.__typename === value.__typename)
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Index } from './Index.js'
2+
3+
declare global {
4+
export namespace GraphQLRequestTypes {
5+
export interface Schemas {
6+
countriesClient: {
7+
index: Index
8+
customScalars: {}
9+
featureOptions: {
10+
schemaErrors: true
11+
}
12+
}
13+
}
14+
}
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable */
2+
3+
import type * as Schema from './SchemaBuildtime.js'
4+
5+
export interface Index {
6+
name: 'countriesClient'
7+
Root: {
8+
Query: Schema.Root.Query
9+
Mutation: null
10+
Subscription: null
11+
}
12+
objects: {
13+
Continent: Schema.Object.Continent
14+
Country: Schema.Object.Country
15+
Language: Schema.Object.Language
16+
State: Schema.Object.State
17+
Subdivision: Schema.Object.Subdivision
18+
}
19+
unions: {}
20+
interfaces: {}
21+
error: {
22+
objects: {}
23+
objectsTypename: {}
24+
rootResultFields: {
25+
Query: {}
26+
Mutation: {}
27+
Subscription: {}
28+
}
29+
}
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../../../src/entrypoints/alpha/scalars.js'
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import type * as $ from '../../../src/entrypoints/alpha/schema.js'
2+
import type * as $Scalar from './Scalar.ts'
3+
4+
// ------------------------------------------------------------ //
5+
// Root //
6+
// ------------------------------------------------------------ //
7+
8+
export namespace Root {
9+
export type Query = $.Object$2<'Query', {
10+
continent: $.Field<
11+
$.Output.Nullable<Object.Continent>,
12+
$.Args<{
13+
code: $Scalar.ID
14+
}>
15+
>
16+
continents: $.Field<
17+
$.Output.List<Object.Continent>,
18+
$.Args<{
19+
filter: $.Input.Nullable<InputObject.ContinentFilterInput>
20+
}>
21+
>
22+
countries: $.Field<
23+
$.Output.List<Object.Country>,
24+
$.Args<{
25+
filter: $.Input.Nullable<InputObject.CountryFilterInput>
26+
}>
27+
>
28+
country: $.Field<
29+
$.Output.Nullable<Object.Country>,
30+
$.Args<{
31+
code: $Scalar.ID
32+
}>
33+
>
34+
language: $.Field<
35+
$.Output.Nullable<Object.Language>,
36+
$.Args<{
37+
code: $Scalar.ID
38+
}>
39+
>
40+
languages: $.Field<
41+
$.Output.List<Object.Language>,
42+
$.Args<{
43+
filter: $.Input.Nullable<InputObject.LanguageFilterInput>
44+
}>
45+
>
46+
}>
47+
}
48+
49+
// ------------------------------------------------------------ //
50+
// Enum //
51+
// ------------------------------------------------------------ //
52+
53+
export namespace Enum {
54+
// -- no types --
55+
}
56+
57+
// ------------------------------------------------------------ //
58+
// InputObject //
59+
// ------------------------------------------------------------ //
60+
61+
export namespace InputObject {
62+
export type ContinentFilterInput = $.InputObject<'ContinentFilterInput', {
63+
code: $.Input.Nullable<InputObject.StringQueryOperatorInput>
64+
}>
65+
66+
export type CountryFilterInput = $.InputObject<'CountryFilterInput', {
67+
code: $.Input.Nullable<InputObject.StringQueryOperatorInput>
68+
continent: $.Input.Nullable<InputObject.StringQueryOperatorInput>
69+
currency: $.Input.Nullable<InputObject.StringQueryOperatorInput>
70+
name: $.Input.Nullable<InputObject.StringQueryOperatorInput>
71+
}>
72+
73+
export type LanguageFilterInput = $.InputObject<'LanguageFilterInput', {
74+
code: $.Input.Nullable<InputObject.StringQueryOperatorInput>
75+
}>
76+
77+
export type StringQueryOperatorInput = $.InputObject<'StringQueryOperatorInput', {
78+
eq: $.Input.Nullable<$Scalar.String>
79+
in: $.Input.Nullable<$.Input.List<$Scalar.String>>
80+
ne: $.Input.Nullable<$Scalar.String>
81+
nin: $.Input.Nullable<$.Input.List<$Scalar.String>>
82+
regex: $.Input.Nullable<$Scalar.String>
83+
}>
84+
}
85+
86+
// ------------------------------------------------------------ //
87+
// Interface //
88+
// ------------------------------------------------------------ //
89+
90+
export namespace Interface {
91+
// -- no types --
92+
}
93+
94+
// ------------------------------------------------------------ //
95+
// Object //
96+
// ------------------------------------------------------------ //
97+
98+
export namespace Object {
99+
export type Continent = $.Object$2<'Continent', {
100+
code: $.Field<$Scalar.ID, null>
101+
countries: $.Field<$.Output.List<Object.Country>, null>
102+
name: $.Field<$Scalar.String, null>
103+
}>
104+
105+
export type Country = $.Object$2<'Country', {
106+
awsRegion: $.Field<$Scalar.String, null>
107+
capital: $.Field<$.Output.Nullable<$Scalar.String>, null>
108+
code: $.Field<$Scalar.ID, null>
109+
continent: $.Field<Object.Continent, null>
110+
currencies: $.Field<$.Output.List<$Scalar.String>, null>
111+
currency: $.Field<$.Output.Nullable<$Scalar.String>, null>
112+
emoji: $.Field<$Scalar.String, null>
113+
emojiU: $.Field<$Scalar.String, null>
114+
languages: $.Field<$.Output.List<Object.Language>, null>
115+
name: $.Field<
116+
$Scalar.String,
117+
$.Args<{
118+
lang: $.Input.Nullable<$Scalar.String>
119+
}>
120+
>
121+
native: $.Field<$Scalar.String, null>
122+
phone: $.Field<$Scalar.String, null>
123+
phones: $.Field<$.Output.List<$Scalar.String>, null>
124+
states: $.Field<$.Output.List<Object.State>, null>
125+
subdivisions: $.Field<$.Output.List<Object.Subdivision>, null>
126+
}>
127+
128+
export type Language = $.Object$2<'Language', {
129+
code: $.Field<$Scalar.ID, null>
130+
name: $.Field<$Scalar.String, null>
131+
native: $.Field<$Scalar.String, null>
132+
rtl: $.Field<$Scalar.Boolean, null>
133+
}>
134+
135+
export type State = $.Object$2<'State', {
136+
code: $.Field<$.Output.Nullable<$Scalar.String>, null>
137+
country: $.Field<Object.Country, null>
138+
name: $.Field<$Scalar.String, null>
139+
}>
140+
141+
export type Subdivision = $.Object$2<'Subdivision', {
142+
code: $.Field<$Scalar.ID, null>
143+
emoji: $.Field<$.Output.Nullable<$Scalar.String>, null>
144+
name: $.Field<$Scalar.String, null>
145+
}>
146+
}
147+
148+
// ------------------------------------------------------------ //
149+
// Union //
150+
// ------------------------------------------------------------ //
151+
152+
export namespace Union {
153+
// -- no types --
154+
}

0 commit comments

Comments
 (0)