Skip to content

Commit

Permalink
chore(docs): alias example (#1131)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Sep 26, 2024
1 parent 61ecd39 commit 06d3990
Show file tree
Hide file tree
Showing 35 changed files with 456 additions and 135 deletions.
2 changes: 0 additions & 2 deletions examples/$/generated-clients/pokemon/_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import './modules/Global.js'

export { create } from './modules/Client.js'
export { isError } from './modules/Error.js'
export { $Index as schemaModel } from './modules/SchemaRuntime.js'
export { Select } from './modules/Select.js'
export * as SelectionSets from './modules/SelectionSets.js'
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export namespace Enum {
// InputObject //
// ------------------------------------------------------------ //
export namespace InputObject {
export type DateFilter = $.InputObject<'DateFilter', {
gte: $.Input.Field<$.Input.Nullable<$Scalar.Float>>
lte: $.Input.Field<$.Input.Nullable<$Scalar.Float>>
}, true>

export type PokemonFilter = $.InputObject<'PokemonFilter', {
birthday: $.Input.Field<$.Input.Nullable<InputObject.DateFilter>>
name: $.Input.Field<$.Input.Nullable<InputObject.StringFilter>>
}, true>

Expand All @@ -71,6 +77,7 @@ export namespace Interface {
export namespace Object {
export type Pokemon = $.Object$2<'Pokemon', {
attack: $.Field<$.Output.Nullable<$Scalar.Int>, null>
birthday: $.Field<$.Output.Nullable<$Scalar.Int>, null>
defense: $.Field<$.Output.Nullable<$Scalar.Int>, null>
hp: $.Field<$.Output.Nullable<$Scalar.Int>, null>
id: $.Field<$.Output.Nullable<$Scalar.Int>, null>
Expand Down
7 changes: 7 additions & 0 deletions examples/$/generated-clients/pokemon/modules/SchemaRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import * as $Scalar from './Scalar.js'
import type { Index } from './SchemaIndex.js'
export const $defaultSchemaUrl = new URL('http://localhost:3000/graphql')

export const DateFilter = $.InputObject(`DateFilter`, {
gte: $.Input.Field($.Input.Nullable($Scalar.Float)),
lte: $.Input.Field($.Input.Nullable($Scalar.Float)),
}, true)

export const PokemonFilter = $.InputObject(`PokemonFilter`, {
birthday: $.Input.Field(() => $.Input.Nullable(DateFilter)),
name: $.Input.Field(() => $.Input.Nullable(StringFilter)),
}, true)

Expand All @@ -16,6 +22,7 @@ export const StringFilter = $.InputObject(`StringFilter`, {
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Pokemon = $.Object$(`Pokemon`, {
attack: $.field($.Output.Nullable($Scalar.Int)),
birthday: $.field($.Output.Nullable($Scalar.Int)),
defense: $.field($.Output.Nullable($Scalar.Int)),
hp: $.field($.Output.Nullable($Scalar.Int)),
id: $.field($.Output.Nullable($Scalar.Int)),
Expand Down
15 changes: 15 additions & 0 deletions examples/$/generated-clients/pokemon/modules/SelectionSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ export namespace Query {
//
//

export interface DateFilter {
gte?: number | undefined | null
lte?: number | undefined | null
}

export interface PokemonFilter {
birthday?: _RefDefs._DateFilter | undefined | null
name?: _RefDefs._StringFilter | undefined | null
}

Expand Down Expand Up @@ -230,6 +236,10 @@ export interface Pokemon extends $SelectionSet.Bases.ObjectLike {
* Select the `attack` field on the `Pokemon` object. Its type is `Int` (a `Scalar`).
*/
attack?: Pokemon.attack$Expanded | $SelectionSet.AliasInput<Pokemon.attack>
/**
* Select the `birthday` field on the `Pokemon` object. Its type is `Int` (a `Scalar`).
*/
birthday?: Pokemon.birthday$Expanded | $SelectionSet.AliasInput<Pokemon.birthday>
/**
* Select the `defense` field on the `Pokemon` object. Its type is `Int` (a `Scalar`).
*/
Expand Down Expand Up @@ -278,6 +288,10 @@ export namespace Pokemon {

export type attack = $SelectionSet.Indicator.NoArgsIndicator

export type birthday$Expanded = $SelectionSet.Indicator.NoArgsIndicator$Expanded

export type birthday = $SelectionSet.Indicator.NoArgsIndicator

export type defense$Expanded = $SelectionSet.Indicator.NoArgsIndicator$Expanded

export type defense = $SelectionSet.Indicator.NoArgsIndicator
Expand Down Expand Up @@ -371,6 +385,7 @@ export namespace Trainer {
export namespace _RefDefs {
export type _Mutation = Mutation
export type _Query = Query
export type _DateFilter = DateFilter
export type _PokemonFilter = PokemonFilter
export type _StringFilter = StringFilter
export type _Pokemon = Pokemon
Expand Down
16 changes: 12 additions & 4 deletions examples/$/schemas/pokemon/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const day = 1000 * 60 * 60 * 24
const year = day * 365.25

export namespace DatabaseServer {
export interface Trainer {
id: number
Expand All @@ -10,6 +13,10 @@ export namespace DatabaseServer {
hp: number
attack: number
defense: number
/**
* Milliseconds since Unix epoch.
*/
birthday: number
trainerId: number | null // Nullable, as a Pokémon may not be captured by a trainer
}

Expand Down Expand Up @@ -50,11 +57,12 @@ export namespace DatabaseServer {

database.trainers.push(ash, misty)

// dprint-ignore
database.pokemon.push(
{ id: 1, name: `Pikachu`, hp: 35, attack: 55, defense: 40, trainerId: 1 },
{ id: 2, name: `Charizard`, hp: 78, attack: 84, defense: 78, trainerId: 1 },
{ id: 3, name: `Squirtle`, hp: 44, attack: 48, defense: 65, trainerId: 2 },
{ id: 4, name: `Bulbasaur`, hp: 45, attack: 49, defense: 49, trainerId: null },
{ id: 1, name: `Pikachu`, hp: 35, attack: 55, defense: 40, trainerId: 1, birthday: new Date('1850').getTime() },
{ id: 2, name: `Charizard`, hp: 78, attack: 84, defense: 78, trainerId: 1, birthday: new Date(Date.now() - day * 5).getTime() },
{ id: 3, name: `Squirtle`, hp: 44, attack: 48, defense: 65, trainerId: 2, birthday: new Date('1910').getTime() },
{ id: 4, name: `Bulbasaur`, hp: 45, attack: 49, defense: 49, trainerId: null, birthday: new Date('2000').getTime() },
)
}
}
7 changes: 7 additions & 0 deletions examples/$/schemas/pokemon/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
input DateFilter {
gte: Float
lte: Float
}

type Mutation {
addPokemon(attack: Int!, defense: Int!, hp: Int!, name: String!): Pokemon
}

type Pokemon {
attack: Int
birthday: Int
defense: Int
hp: Int
id: Int
Expand All @@ -12,6 +18,7 @@ type Pokemon {
}

input PokemonFilter {
birthday: DateFilter
name: StringFilter
}

Expand Down
18 changes: 18 additions & 0 deletions examples/$/schemas/pokemon/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const Pokemon = builder.objectRef<DatabaseServer.Pokemon>(`Pokemon`).implement({
hp: t.int({ resolve: (pokemon) => pokemon.hp }),
attack: t.int({ resolve: (pokemon) => pokemon.attack }),
defense: t.int({ resolve: (pokemon) => pokemon.defense }),
birthday: t.int({ resolve: (pokemon) => pokemon.birthday }),
trainer: t.field({
type: Trainer,
nullable: true,
Expand Down Expand Up @@ -58,9 +59,17 @@ const StringFilter = builder.inputType(`StringFilter`, {
}),
})

const DateFilter = builder.inputType(`DateFilter`, {
fields: (t) => ({
lte: t.float(),
gte: t.float(),
}),
})

const PokemonFilter = builder.inputType(`PokemonFilter`, {
fields: (t) => ({
name: t.field({ type: StringFilter }),
birthday: t.field({ type: DateFilter }),
}),
})

Expand All @@ -80,6 +89,14 @@ builder.queryField(`pokemons`, (t) =>
return args.filter.name.in.includes(p.name)
}
}
if (args.filter?.birthday) {
if (args.filter.birthday.lte) {
return p.birthday <= args.filter.birthday.lte
}
if (args.filter.birthday.gte) {
return p.birthday >= args.filter.birthday.gte
}
}
return true
}),
}))
Expand Down Expand Up @@ -135,6 +152,7 @@ builder.mutationField(`addPokemon`, (t) =>
attack,
defense,
trainerId: null,
birthday: new Date().getTime(),
}
DatabaseServer.tenant(ctx.tenant).pokemon.push(newPokemon)
return newPokemon
Expand Down
28 changes: 28 additions & 0 deletions examples/55_generated/generated_alias__alias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* This example shows how to write GraphQL aliases in the TypeScript interface.
*/

import { Pokemon } from '../$/generated-clients/pokemon/__.js'
import { showJson } from '../$/helpers.js'

const pokemon = Pokemon.create()

const day = 1000 * 60 * 60 * 24
const year = day * 365.25
const yearsAgo100 = new Date(Date.now() - year * 100).getTime()
const yearsAgo1 = new Date(Date.now() - year).getTime()

const pokemons = await pokemon.query.$batch({
pokemons: [
[`elderPokemons`, {
$: { filter: { birthday: { lte: yearsAgo100 } } },
name: true,
}],
[`babyPokemons`, {
$: { filter: { birthday: { gte: yearsAgo1 } } },
name: true,
}],
],
})

showJson(pokemons)
2 changes: 1 addition & 1 deletion examples/55_generated/generated_arguments__arguments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This example shows how to use the TypeScript interface for GraphQL arguments.
* This example shows how to write field arguments in TypeScript interface.
*/

import { Pokemon } from '../$/generated-clients/pokemon/__.js'
Expand Down
2 changes: 1 addition & 1 deletion examples/55_generated/generated_document__document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This example shows how to use the TypeScript interface for whole GraphQL documents.
* This example shows how to write whole GraphQL documents in the TypeScript interface.
*/

import { Pokemon } from '../$/generated-clients/pokemon/__.js'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1727365576054'
'x-sent-at-time': '1727369127674'
},
signal: undefined,
method: 'post',
Expand Down
2 changes: 1 addition & 1 deletion examples/__outputs__/20_output/output_envelope.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
headers: Headers {
'content-type': 'application/graphql-response+json; charset=utf-8',
'content-length': '104',
date: 'Thu, 26 Sep 2024 15:46:16 GMT',
date: 'Thu, 26 Sep 2024 16:45:27 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=5'
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---------------------------------------- SHOW ----------------------------------------
{
"elderPokemons": [
{
"name": "Pikachu"
},
{
"name": "Squirtle"
}
],
"babyPokemons": [
{
"name": "Charizard"
}
]
}
Loading

0 comments on commit 06d3990

Please sign in to comment.