-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): generate from introspection (#1014)
- Loading branch information
1 parent
ddf27ed
commit 8e12e0b
Showing
21 changed files
with
566 additions
and
68 deletions.
There are no files selected for viewing
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
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,28 +1,23 @@ | ||
/* eslint-disable */ | ||
|
||
import { gql, Graffle } from '../src/entrypoints/alpha/main.js' | ||
import { CountriesClient } from './generated-clients/countries/__.js' | ||
|
||
const request = Graffle.create({ schema: `https://countries.trevorblades.com/graphql` }).use({ | ||
name: `CustomFetch`, | ||
anyware: async ({ exchange }) => { | ||
return await exchange({ | ||
using: { | ||
fetch: async () => { | ||
return new Response(JSON.stringify({ data: { countries: [{ name: `USA` }] } })) | ||
// todo: if used introspection query to get schema, then default schema to that URL. | ||
// todo: https://github.com/jasonkuhrt/graphql-request/issues/1015 | ||
const countriesClient = CountriesClient.create({ schema: `https://countries.trevorblades.com/graphql` }) | ||
.use({ | ||
name: `CustomFetch`, | ||
anyware: async ({ exchange }) => { | ||
return await exchange({ | ||
using: { | ||
fetch: async () => { | ||
return new Response(JSON.stringify({ data: { countries: [{ name: `Canada Mocked!` }] } })) | ||
}, | ||
}, | ||
}, | ||
}) | ||
}, | ||
}).rawOrThrow | ||
}) | ||
}, | ||
}) | ||
|
||
const { data } = await request( | ||
gql` | ||
{ | ||
countries { | ||
name | ||
} | ||
} | ||
`, | ||
) | ||
const countries = await countriesClient.query.countries({ name: true }) | ||
|
||
console.log(data) | ||
console.log(countries) |
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,22 +1,12 @@ | ||
/* eslint-disable */ | ||
import { CountriesClient } from './generated-clients/countries/__.js' | ||
|
||
import { gql, Graffle } from '../src/entrypoints/alpha/main.js' | ||
|
||
const request = Graffle.create({ | ||
const request = CountriesClient.create({ | ||
schema: `https://countries.trevorblades.com/graphql`, | ||
headers: { | ||
authorization: `Bearer MY_TOKEN`, | ||
}, | ||
}).rawOrThrow | ||
}) | ||
|
||
const { data } = await request( | ||
gql` | ||
{ | ||
countries { | ||
name | ||
} | ||
} | ||
`, | ||
) | ||
const continents = await request.query.continents({ name: true }) | ||
|
||
console.log(data) | ||
console.log(continents) |
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,5 @@ | ||
import { createPrefilled } from '../../../src/entrypoints/alpha/client.js' | ||
|
||
import { $Index } from './SchemaRuntime.js' | ||
|
||
export const create = createPrefilled(`countriesClient`, $Index) |
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,14 @@ | ||
type Include<T, U> = Exclude<T, Exclude<T, U>> | ||
|
||
type ObjectWithTypeName = { __typename: string } | ||
|
||
const ErrorObjectsTypeNameSelectedEnum = {} as Record<string, ObjectWithTypeName> | ||
|
||
const ErrorObjectsTypeNameSelected = Object.values(ErrorObjectsTypeNameSelectedEnum) | ||
|
||
type ErrorObjectsTypeNameSelected = (typeof ErrorObjectsTypeNameSelected)[number] | ||
|
||
export const isError = <$Value>(value: $Value): value is Include<$Value, ErrorObjectsTypeNameSelected> => { | ||
return typeof value === 'object' && value !== null && '__typename' in value | ||
&& ErrorObjectsTypeNameSelected.some(_ => _.__typename === value.__typename) | ||
} |
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,15 @@ | ||
import type { Index } from './Index.js' | ||
|
||
declare global { | ||
export namespace GraphQLRequestTypes { | ||
export interface Schemas { | ||
countriesClient: { | ||
index: Index | ||
customScalars: {} | ||
featureOptions: { | ||
schemaErrors: true | ||
} | ||
} | ||
} | ||
} | ||
} |
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,30 @@ | ||
/* eslint-disable */ | ||
|
||
import type * as Schema from './SchemaBuildtime.js' | ||
|
||
export interface Index { | ||
name: 'countriesClient' | ||
Root: { | ||
Query: Schema.Root.Query | ||
Mutation: null | ||
Subscription: null | ||
} | ||
objects: { | ||
Continent: Schema.Object.Continent | ||
Country: Schema.Object.Country | ||
Language: Schema.Object.Language | ||
State: Schema.Object.State | ||
Subdivision: Schema.Object.Subdivision | ||
} | ||
unions: {} | ||
interfaces: {} | ||
error: { | ||
objects: {} | ||
objectsTypename: {} | ||
rootResultFields: { | ||
Query: {} | ||
Mutation: {} | ||
Subscription: {} | ||
} | ||
} | ||
} |
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 @@ | ||
export * from '../../../src/entrypoints/alpha/scalars.js' |
154 changes: 154 additions & 0 deletions
154
examples/generated-clients/countries/SchemaBuildtime.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,154 @@ | ||
import type * as $ from '../../../src/entrypoints/alpha/schema.js' | ||
import type * as $Scalar from './Scalar.ts' | ||
|
||
// ------------------------------------------------------------ // | ||
// Root // | ||
// ------------------------------------------------------------ // | ||
|
||
export namespace Root { | ||
export type Query = $.Object$2<'Query', { | ||
continent: $.Field< | ||
$.Output.Nullable<Object.Continent>, | ||
$.Args<{ | ||
code: $Scalar.ID | ||
}> | ||
> | ||
continents: $.Field< | ||
$.Output.List<Object.Continent>, | ||
$.Args<{ | ||
filter: $.Input.Nullable<InputObject.ContinentFilterInput> | ||
}> | ||
> | ||
countries: $.Field< | ||
$.Output.List<Object.Country>, | ||
$.Args<{ | ||
filter: $.Input.Nullable<InputObject.CountryFilterInput> | ||
}> | ||
> | ||
country: $.Field< | ||
$.Output.Nullable<Object.Country>, | ||
$.Args<{ | ||
code: $Scalar.ID | ||
}> | ||
> | ||
language: $.Field< | ||
$.Output.Nullable<Object.Language>, | ||
$.Args<{ | ||
code: $Scalar.ID | ||
}> | ||
> | ||
languages: $.Field< | ||
$.Output.List<Object.Language>, | ||
$.Args<{ | ||
filter: $.Input.Nullable<InputObject.LanguageFilterInput> | ||
}> | ||
> | ||
}> | ||
} | ||
|
||
// ------------------------------------------------------------ // | ||
// Enum // | ||
// ------------------------------------------------------------ // | ||
|
||
export namespace Enum { | ||
// -- no types -- | ||
} | ||
|
||
// ------------------------------------------------------------ // | ||
// InputObject // | ||
// ------------------------------------------------------------ // | ||
|
||
export namespace InputObject { | ||
export type ContinentFilterInput = $.InputObject<'ContinentFilterInput', { | ||
code: $.Input.Nullable<InputObject.StringQueryOperatorInput> | ||
}> | ||
|
||
export type CountryFilterInput = $.InputObject<'CountryFilterInput', { | ||
code: $.Input.Nullable<InputObject.StringQueryOperatorInput> | ||
continent: $.Input.Nullable<InputObject.StringQueryOperatorInput> | ||
currency: $.Input.Nullable<InputObject.StringQueryOperatorInput> | ||
name: $.Input.Nullable<InputObject.StringQueryOperatorInput> | ||
}> | ||
|
||
export type LanguageFilterInput = $.InputObject<'LanguageFilterInput', { | ||
code: $.Input.Nullable<InputObject.StringQueryOperatorInput> | ||
}> | ||
|
||
export type StringQueryOperatorInput = $.InputObject<'StringQueryOperatorInput', { | ||
eq: $.Input.Nullable<$Scalar.String> | ||
in: $.Input.Nullable<$.Input.List<$Scalar.String>> | ||
ne: $.Input.Nullable<$Scalar.String> | ||
nin: $.Input.Nullable<$.Input.List<$Scalar.String>> | ||
regex: $.Input.Nullable<$Scalar.String> | ||
}> | ||
} | ||
|
||
// ------------------------------------------------------------ // | ||
// Interface // | ||
// ------------------------------------------------------------ // | ||
|
||
export namespace Interface { | ||
// -- no types -- | ||
} | ||
|
||
// ------------------------------------------------------------ // | ||
// Object // | ||
// ------------------------------------------------------------ // | ||
|
||
export namespace Object { | ||
export type Continent = $.Object$2<'Continent', { | ||
code: $.Field<$Scalar.ID, null> | ||
countries: $.Field<$.Output.List<Object.Country>, null> | ||
name: $.Field<$Scalar.String, null> | ||
}> | ||
|
||
export type Country = $.Object$2<'Country', { | ||
awsRegion: $.Field<$Scalar.String, null> | ||
capital: $.Field<$.Output.Nullable<$Scalar.String>, null> | ||
code: $.Field<$Scalar.ID, null> | ||
continent: $.Field<Object.Continent, null> | ||
currencies: $.Field<$.Output.List<$Scalar.String>, null> | ||
currency: $.Field<$.Output.Nullable<$Scalar.String>, null> | ||
emoji: $.Field<$Scalar.String, null> | ||
emojiU: $.Field<$Scalar.String, null> | ||
languages: $.Field<$.Output.List<Object.Language>, null> | ||
name: $.Field< | ||
$Scalar.String, | ||
$.Args<{ | ||
lang: $.Input.Nullable<$Scalar.String> | ||
}> | ||
> | ||
native: $.Field<$Scalar.String, null> | ||
phone: $.Field<$Scalar.String, null> | ||
phones: $.Field<$.Output.List<$Scalar.String>, null> | ||
states: $.Field<$.Output.List<Object.State>, null> | ||
subdivisions: $.Field<$.Output.List<Object.Subdivision>, null> | ||
}> | ||
|
||
export type Language = $.Object$2<'Language', { | ||
code: $.Field<$Scalar.ID, null> | ||
name: $.Field<$Scalar.String, null> | ||
native: $.Field<$Scalar.String, null> | ||
rtl: $.Field<$Scalar.Boolean, null> | ||
}> | ||
|
||
export type State = $.Object$2<'State', { | ||
code: $.Field<$.Output.Nullable<$Scalar.String>, null> | ||
country: $.Field<Object.Country, null> | ||
name: $.Field<$Scalar.String, null> | ||
}> | ||
|
||
export type Subdivision = $.Object$2<'Subdivision', { | ||
code: $.Field<$Scalar.ID, null> | ||
emoji: $.Field<$.Output.Nullable<$Scalar.String>, null> | ||
name: $.Field<$Scalar.String, null> | ||
}> | ||
} | ||
|
||
// ------------------------------------------------------------ // | ||
// Union // | ||
// ------------------------------------------------------------ // | ||
|
||
export namespace Union { | ||
// -- no types -- | ||
} |
Oops, something went wrong.