Skip to content

Commit

Permalink
refactor: client layer property modules (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Sep 24, 2024
1 parent ca12b80 commit f339f7b
Show file tree
Hide file tree
Showing 31 changed files with 481 additions and 389 deletions.
6 changes: 2 additions & 4 deletions src/entrypoints/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
export { execute } from '../layers/0_functions/execute.js'
export { type TypedDocumentString } from '../layers/0_functions/types.js'
export { createExtension, type Extension } from '../layers/6_client/extension.js'
export { createExtension, type Extension } from '../layers/6_client/extension/extension.js'
export { gql } from '../layers/6_helpers/gql.js'
// todo figure this export out. Was just put there to resolve a type error about "...cannot be named..."
export * from '../layers/6_client/Settings/Input.js'
// todo figure this export out. Was just put there to resolve a type error about "...cannot be named..."
export type { BuilderRequestMethods } from '../layers/6_client/client.js'
export { type Config as BuilderConfig } from '../layers/6_client/Settings/Config.js'
export * from '../layers/6_client/Settings/Input.js'
export { type WithInput } from '../layers/6_client/Settings/inputIncrementable/inputIncrementable.js'
export * from '../lib/prelude.js'
export * from './__Graffle.js'
Expand Down
2 changes: 1 addition & 1 deletion src/entrypoints/utilities-for-generated.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export { type Simplify } from 'type-fest'
export * from '../layers/2_SelectionSet/__.js'
export { type DocumentRunner } from '../layers/6_client/document.js'
export type {
ConfigGetOutputError,
ResolveOutputReturnRootField,
ResolveOutputReturnRootType,
} from '../layers/6_client/handleOutput.js'
export { type DocumentRunner } from '../layers/6_client/requestMethods/document.js'
export type { Config } from '../layers/6_client/Settings/Config.js'
export { type AddTypenameToSelectedRootTypeResultFields } from '../layers/6_client/Settings/Config.js'
export { HKT } from '../lib/hkt/__.js'
Expand Down
10 changes: 8 additions & 2 deletions src/layers/0_functions/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { DocumentTypeDecoration } from '@graphql-typed-document-node/core'
import type { DocumentNode, TypedQueryDocumentNode } from 'graphql'
import type { HasRequiredKeys, IsEmptyObject } from 'type-fest'
import type { StandardScalarVariables } from '../../lib/graphql.js'
import type { SomeData, StandardScalarVariables } from '../../lib/graphql.js'
import type { Negate } from '../../lib/prelude.js'
import type { DocumentInput, OperationNameInput } from '../6_client/types.js'

export type DocumentInput<$Data extends SomeData = SomeData, V = any> =
| string
| TypedDocumentString<$Data, V>
| TypedQueryDocumentNode<$Data, V>

type OperationNameInput = string

export type BaseInput_ = {
document: DocumentNode | string
Expand Down
2 changes: 2 additions & 0 deletions src/layers/4_document/_.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './document.js'
export * from './print.js'
1 change: 1 addition & 0 deletions src/layers/4_document/__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as Document from './_.js'
42 changes: 42 additions & 0 deletions src/layers/4_document/document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { type OperationType, type RootTypeNameMutation, type RootTypeNameQuery } from '../../lib/graphql.js'
import type { FirstNonUnknownNever, IsKeyInObjectOptional, Values } from '../../lib/prelude.js'

export type OperationName = string

export interface SomeDocumentOperation {
[k: string]: object
}

export interface SomeDocument {
mutation?: SomeDocumentOperation
query?: SomeDocumentOperation
}

// // dprint-ignore
// type IsHasMultipleOperations<$Document extends SomeDocument> =
// All<[
// IsHasMultipleKeys<$Document[OperationType.Query]>,
// IsHasMultipleKeys<$Document[OperationType.Mutation]>,
// ]>

// dprint-ignore
export type GetOperationNames<$Document extends SomeDocument> = Values<
{
[$OperationType in keyof $Document]: keyof $Document[$OperationType] & string
}
>

// dprint-ignore
export type GetRootTypeNameOfOperation<$Document extends SomeDocument, $Name extends OperationName> =
IsKeyInObjectOptional<$Document[OperationType.Mutation], $Name> extends true ? RootTypeNameMutation :
IsKeyInObjectOptional<$Document[OperationType.Query], $Name> extends true ? RootTypeNameQuery :
never

// dprint-ignore
export type GetOperation<$Document extends SomeDocument, $Name extends string> =
FirstNonUnknownNever<[
// @ts-expect-error could be unknown
$Document[OperationType.Mutation][$Name],
// @ts-expect-error could be unknown
$Document[OperationType.Query][$Name]
]>
39 changes: 39 additions & 0 deletions src/layers/4_document/print.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { operationTypeNameToRootTypeName, OperationTypes } from '../../lib/graphql.js'
import { SelectionSet } from '../2_SelectionSet/__.js'
import type { Context, DocumentObject } from '../2_SelectionSet/print.js'

// todo this is currently unused by graffle internally. Remove?
export const print = (
context: Context,
document: DocumentObject,
) => {
const operations = [
...(Object.entries(document.query || {}).map(([operationName, selectionSet]) => ({
operationName,
selectionSet,
operationType: OperationTypes.query,
}))),
...(Object.entries(document.mutation || {}).map(([operationName, selectionSet]) => ({
operationName,
selectionSet,
operationType: OperationTypes.mutation,
}))),
]

return operations.map(({ operationName, selectionSet, operationType }) => {
const rootType = operationTypeNameToRootTypeName[operationType]
const rootTypeDocument = selectionSet

const schemaRootType = context.schemaIndex[`Root`][rootType]
if (!schemaRootType) throw new Error(`Schema has no ${rootType} root type`)

const documentString = SelectionSet.Print.resolveRootType(
context,
schemaRootType,
rootTypeDocument,
operationName,
)

return documentString
}).join(`\n\n`)
}
Loading

0 comments on commit f339f7b

Please sign in to comment.