diff --git a/architecture.md b/architecture.md new file mode 100644 index 0000000..f63274b --- /dev/null +++ b/architecture.md @@ -0,0 +1,44 @@ +i have a query made like this + +```ts +type ObjectTypeReq = { + args: ArgsType, + select: Record + include: Record +} + +type UnionTypeReq = { + args: ArgsType, + select: Record + include: Record + onA: ObjectTypeReq + onB: ObjectTypeReq +} + +type TypeReq = ObjectTypeReq | UnionTypeReq | boolean + +getCountries(req: TypeReq): Type +``` + +i need to create a class Client with top level methods: + +```ts +class Client { + getCountries(req: ObjectTypeReq): Countries { + const { query, variables } = buildQuery(req, 'getCountries') + } +} + +function buildQuery() +``` + +what if instead it used the builder pattern? + +```ts +client.getContinents({ + name: '', + include: { + countries: true, + }, +}) +``` diff --git a/cli/src/render/typeMap/renderTypeMap.ts b/cli/src/render/typeMap/renderTypeMap.ts index 0ba1226..af73e2a 100644 --- a/cli/src/render/typeMap/renderTypeMap.ts +++ b/cli/src/render/typeMap/renderTypeMap.ts @@ -12,6 +12,7 @@ import { isInterfaceType, isObjectType, isScalarType, + isUnionType, } from 'graphql' import { excludedTypes } from '../common/excludedTypes' @@ -67,7 +68,6 @@ export const renderTypeMap = (schema: GraphQLSchema, ctx: RenderContext) => { ctx.addCodeBlock( JSON.stringify(replaceTypeNamesWithIndexes(result), null, 4), ) - } diff --git a/integration-tests/generated/schema.graphql b/integration-tests/generated/schema.graphql index e72a9bf..d07a5e4 100644 --- a/integration-tests/generated/schema.graphql +++ b/integration-tests/generated/schema.graphql @@ -9,6 +9,8 @@ enum SomeEnum2 { world } +scalar MyCustomScalar + input InputWithRequiredFields { requiredField: String! optionalField: String @@ -42,6 +44,7 @@ type RecursiveType { type Repository { createdAt: String! forks(filter: String): ForkConnection + customScalar: MyCustomScalar } type ForkConnection { diff --git a/integration-tests/generated/schema.ts b/integration-tests/generated/schema.ts index e39955d..7021244 100644 --- a/integration-tests/generated/schema.ts +++ b/integration-tests/generated/schema.ts @@ -4,6 +4,7 @@ /* eslint-disable */ export type Scalars = { + MyCustomScalar: any, String: string, Int: number, Float: number, @@ -41,6 +42,7 @@ export interface RecursiveType { export interface Repository { createdAt: Scalars['String'] forks: (ForkConnection | null) + customScalar: (Scalars['MyCustomScalar'] | null) __typename: 'Repository' } @@ -159,6 +161,7 @@ export interface RecursiveTypeGenqlSelection{ export interface RepositoryGenqlSelection{ createdAt?: boolean | number forks?: (ForkConnectionGenqlSelection & { __args?: {filter?: (Scalars['String'] | null)} }) + customScalar?: boolean | number __typename?: boolean | number __scalar?: boolean | number } diff --git a/integration-tests/generated/types.ts b/integration-tests/generated/types.ts index eb02378..173e567 100644 --- a/integration-tests/generated/types.ts +++ b/integration-tests/generated/types.ts @@ -2,353 +2,358 @@ export default { "scalars": [ 0, 1, - 3, - 5, + 2, + 4, 6, - 17, - 27 + 7, + 18, + 28 ], "types": { "SomeEnum": {}, "SomeEnum2": {}, + "MyCustomScalar": {}, "InputWithRequiredFields": { "requiredField": [ - 3 + 4 ], "optionalField": [ - 3 + 4 ], "__typename": [ - 3 + 4 ] }, "String": {}, "Query": { "repository": [ - 9, + 10, { "name": [ - 3, + 4, "String!" ], "owner": [ - 3 + 4 ] } ], "queryWithDefaultArgs": [ - 3, + 4, { "input": [ - 7 + 8 ], "defaultValue": [ - 5 + 6 ] } ], "optionalArgs": [ - 9, + 10, { "name": [ - 3 + 4 ], "owner": [ - 3 + 4 ] } ], "user": [ - 13 + 14 ], "someScalarValue": [ - 3, + 4, { "x": [ - 6 + 7 ] } ], "recursiveType": [ - 8, + 9, { "requiredVal": [ - 3, + 4, "[String!]" ] } ], "throwsError": [ - 3 + 4 ], "requiredFields": [ - 3, + 4, { "input": [ - 2, + 3, "InputWithRequiredFields!" ] } ], "account": [ - 15 + 16 ], "coordinates": [ - 20 + 21 ], "unionThatImplementsInterface": [ - 25, + 26, { "typename": [ - 3 + 4 ] } ], "InterfaceNotImplemented": [ - 26 + 27 ], "__typename": [ - 3 + 4 ] }, "Int": {}, "Float": {}, "DefaultArgsInput": { "string": [ - 3 + 4 ], "__typename": [ - 3 + 4 ] }, "RecursiveType": { "value": [ - 3 + 4 ], "recurse": [ - 8, + 9, { "arg": [ - 5 + 6 ] } ], "__typename": [ - 3 + 4 ] }, "Repository": { "createdAt": [ - 3 + 4 ], "forks": [ - 10, + 11, { "filter": [ - 3 + 4 ] } ], + "customScalar": [ + 2 + ], "__typename": [ - 3 + 4 ] }, "ForkConnection": { "edges": [ - 11 + 12 ], "__typename": [ - 3 + 4 ] }, "ForkEdge": { "cursor": [ - 3 + 4 ], "node": [ - 12 + 13 ], "__typename": [ - 3 + 4 ] }, "Fork": { "name": [ - 3 + 4 ], "number": [ - 5 + 6 ], "__typename": [ - 3 + 4 ] }, "User": { "name": [ - 3 + 4 ], "common": [ - 5 + 6 ], "__typename": [ - 3 + 4 ] }, "Subscription": { "user": [ - 13 + 14 ], "__typename": [ - 3 + 4 ] }, "Account": { "on_User": [ - 13 + 14 ], "on_Guest": [ - 16 + 17 ], "__typename": [ - 3 + 4 ] }, "Guest": { "anonymous": [ - 17 + 18 ], "common": [ - 5 + 6 ], "__typename": [ - 3 + 4 ] }, "Boolean": {}, "House": { "owner": [ - 13 + 14 ], "x": [ - 3 + 4 ], "y": [ - 3 + 4 ], "__typename": [ - 3 + 4 ] }, "Bank": { "address": [ - 3 + 4 ], "x": [ - 3 + 4 ], "y": [ - 3 + 4 ], "__typename": [ - 3 + 4 ] }, "Point": { "x": [ - 3 + 4 ], "y": [ - 3 + 4 ], "on_House": [ - 18 + 19 ], "on_Bank": [ - 19 + 20 ], "__typename": [ - 3 + 4 ] }, "ClientError": { "message": [ - 3 + 4 ], "on_ClientErrorNameAlreadyTaken": [ - 22 + 23 ], "on_ClientErrorNameInvalid": [ - 23 + 24 ], "__typename": [ - 3 + 4 ] }, "ClientErrorNameAlreadyTaken": { "message": [ - 3 + 4 ], "ownProp1": [ - 3 + 4 ], "__typename": [ - 3 + 4 ] }, "ClientErrorNameInvalid": { "message": [ - 3 + 4 ], "ownProp2": [ - 3 + 4 ], "__typename": [ - 3 + 4 ] }, "ClientErrorWithoutInterface": { "ownProp3": [ - 3 + 4 ], "__typename": [ - 3 + 4 ] }, "GenericError": { "on_ClientErrorNameAlreadyTaken": [ - 22 + 23 ], "on_ClientErrorNameInvalid": [ - 23 + 24 ], "on_ClientErrorWithoutInterface": [ - 24 + 25 ], "on_ClientError": [ - 21 + 22 ], "__typename": [ - 3 + 4 ] }, "InterfaceNotImplemented": { "id": [ - 27 + 28 ], "title": [ - 3 + 4 ], "url": [ - 3 + 4 ], "permalink": [ - 3 + 4 ], "entry_id": [ - 27 + 28 ], "__typename": [ - 3 + 4 ] }, "ID": {} diff --git a/integration-tests/schema.graphql b/integration-tests/schema.graphql index f2bdf3d..85c9890 100644 --- a/integration-tests/schema.graphql +++ b/integration-tests/schema.graphql @@ -8,6 +8,8 @@ enum SomeEnum2 { world } +scalar MyCustomScalar + input InputWithRequiredFields { requiredField: String! optionalField: String @@ -40,6 +42,7 @@ type RecursiveType { type Repository { createdAt: String! forks(filter: String): ForkConnection + customScalar: MyCustomScalar } type ForkConnection { diff --git a/integration-tests/tests/execution.ts b/integration-tests/tests/execution.ts index ee63e1a..fe29104 100644 --- a/integration-tests/tests/execution.ts +++ b/integration-tests/tests/execution.ts @@ -86,6 +86,7 @@ describe('execute queries', async function () { repository: () => { return { createdAt: 'now', + customScalar: { x: true }, } }, account: () => { @@ -136,7 +137,7 @@ describe('execute queries', async function () { createdAt: true, }, }) - console.log(JSON.stringify(res, null, 2)) + console.log('first query',JSON.stringify(res, null, 2)) assert(res.repository.createdAt) assert(res.optionalArgs.createdAt) }), @@ -161,7 +162,7 @@ describe('execute queries', async function () { assert(res.optionalArgs.createdAt) }), ) - + it( 'simple ', withServer(async () => { @@ -230,7 +231,7 @@ describe('execute queries', async function () { name: 'genql', owner: 'remorses', }, - ...everything, + __scalar: true, forks: { __args: { filter: 'test' }, edges: { node: { name: true, number: true } }, @@ -242,6 +243,7 @@ describe('execute queries', async function () { res?.account // no optional chaining because repository is non null expectType(res.repository.createdAt) + assert(res.repository.customScalar) expectType>(res.repository.__typename) expectType[]>>( res.repository?.forks?.edges?.map((x) => x?.node?.name),