Skip to content

Commit

Permalink
Merge pull request #162 from remorses/prisma-like
Browse files Browse the repository at this point in the history
Fix bugs before major version change
  • Loading branch information
remorses authored Jan 18, 2024
2 parents 3021361 + d33b15c commit 5a6ac82
Show file tree
Hide file tree
Showing 33 changed files with 5,042 additions and 8,511 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
submodules: true
- uses: actions/setup-node@v3
with:
node-version: 18
Expand All @@ -25,7 +27,7 @@ jobs:
# setup pnpm
- uses: pnpm/action-setup@v2
with:
version: 7
version: 8
run_install: false
# scripts
- run: pnpm i --store-dir ~/pnpm
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "beskar"]
path = beskar
url = https://github.com/remorses/beskar
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
For new changes see the new changelog built with [Notaku](https://notaku.so) and Notion: https://changelog.genql.dev

## 2.11

- Fix custom scalars typed with object types
- Make required top level arguments with defaults non required
- \_\_scalar: true should exclude scalars with required arguments

## 2.10.0

Fixed problem generating interfaces without any implementation
Expand Down
43 changes: 43 additions & 0 deletions architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
i have a query made like this

```ts
type ObjectTypeReq = {
args: ArgsType,
select: Record<string, TypeReq>
include: Record<string, TypeReq>
}

type UnionTypeReq = {
args: ArgsType,
select: Record<string, TypeReq>
include: Record<string, TypeReq>
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<Include extends CountryInclude, Select extends CountrySelect>(
req: GetCountriesRequest<Include, Select>,
): Selection<Countries, Include, Select> {
const { query, variables } = buildQuery(req, 'getCountries')
}
}

type GetCountriesRequest<I, S> = {
args: GetCountriesArgs
select: Pick<Countries, S>
include: I
}

function buildQuery()
```


1 change: 1 addition & 0 deletions beskar
Submodule beskar added at e62cb3
2 changes: 1 addition & 1 deletion cli/src/render/requestTypes/objectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const toArgsString = (field: GraphQLField<any, any, any>) => {
.map(
(a) =>
`${argumentComment(a)}${a.name}${
isNonNullType(a.type) ? ':' : '?:'
isNonNullType(a.type) && a.defaultValue == null ? ':' : '?:'
} ${renderTyping(a.type)}`,
)
.join(', ')
Expand Down
3 changes: 2 additions & 1 deletion cli/src/render/typeMap/renderTypeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isInterfaceType,
isObjectType,
isScalarType,

isUnionType,
} from 'graphql'
import { excludedTypes } from '../common/excludedTypes'
Expand All @@ -37,6 +38,7 @@ export const renderTypeMap = (schema: GraphQLSchema, ctx: RenderContext) => {
result.types[t.name] = objectType(t, ctx)
else if (isUnionType(t)) result.types[t.name] = unionType(t, ctx)
else if (isScalarType(t) || isEnumType(t)) {

result.scalars.push(t.name)
result.types[t.name] = {}
}
Expand Down Expand Up @@ -67,7 +69,6 @@ export const renderTypeMap = (schema: GraphQLSchema, ctx: RenderContext) => {
ctx.addCodeBlock(
JSON.stringify(replaceTypeNamesWithIndexes(result), null, 4),
)



}
Expand Down
19 changes: 18 additions & 1 deletion cli/src/runtime/linkTypeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,24 @@ export const linkTypeMap = (
// type scalar properties
scalar: Object.keys(fields).filter((f) => {
const [type] = fields[f] || []
return type && typeMap.scalars.includes(type)

const isScalar =
type && typeMap.scalars.includes(type)
if (!isScalar) {
return false
}
const args = fields[f]?.[1]
const argTypes = Object.values(args || {})
.map((x) => x?.[1])
.filter(Boolean)

const hasRequiredArgs = argTypes.some(
(str) => str && str.endsWith('!'),
)
if (hasRequiredArgs) {
return false
}
return true
}),
// fields with corresponding `type` and `args`
fields: Object.assign(
Expand Down
9 changes: 4 additions & 5 deletions cli/src/runtime/typeSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ export type FieldsSelection<SRC extends Anify<DST> | undefined, DST> = {
? 'object'
: 'never']

type HandleObject<SRC extends Anify<DST>, DST> = SRC extends Nil
type HandleObject<SRC extends Anify<DST>, DST> = DST extends boolean
? SRC
: SRC extends Nil
? never
: Pick<
{
// using keyof SRC to maintain ?: relations of SRC type
[Key in keyof SRC]: Key extends keyof DST
? FieldsSelection<
SRC[Key],
NonNullable<DST[Key]>
>
? FieldsSelection<SRC[Key], NonNullable<DST[Key]>>
: SRC[Key]
},
Exclude<keyof DST, FieldsToRemove>
Expand Down
11 changes: 10 additions & 1 deletion cli/src/typeSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type SRC = {
a: string
optional?: string
}[]
customScalar: { json: true }
nested?: {
list?: {
edges?: {
Expand Down Expand Up @@ -100,6 +101,7 @@ type SRC = {

describe('pick', () => {
const req = {
customScalar: true,
category: {
a: 1,
b: 1,
Expand Down Expand Up @@ -149,6 +151,13 @@ describe('pick', () => {
z.argumentSyntax.a.toLocaleLowerCase
}),
)
test(
'custom scalars',
dontExecute(() => {
const custom = z.customScalar
custom.json
}),
)
})

describe('__scalar', () => {
Expand Down Expand Up @@ -246,7 +255,7 @@ describe('optional fields', () => {
optionalObject: {
x: 1,
optional: 1,
}
},
}
const z: FieldsSelection<SRC, typeof req> = {} as any
test(
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/__snapshots__/simple.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ subscription SomeName {
fragment f1 on User {
name
common
commonButDiffType
__typename
}
Expand All @@ -109,6 +110,7 @@ subscription {
fragment f1 on User {
name
common
commonButDiffType
__typename
}
Expand Down Expand Up @@ -139,6 +141,7 @@ fragment f1 on Fork {
fragment f2 on User {
name
common
commonButDiffType
__typename
}
Expand All @@ -161,6 +164,7 @@ subscription {
}
fragment f1 on User {
name
commonButDiffType
__typename
}
Expand Down
1 change: 1 addition & 0 deletions integration-tests/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ generate({
output: path.join(__dirname, 'generated'),
scalarTypes: {
MongoID: 'string',
MyCustomScalar: `{ x: string }`,
},
// verbose: false,
}).catch(console.error)
8 changes: 7 additions & 1 deletion integration-tests/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ enum SomeEnum2 {
world
}

scalar MyCustomScalar

input InputWithRequiredFields {
requiredField: String!
optionalField: String
Expand All @@ -17,7 +19,7 @@ input InputWithRequiredFields {
type Query {
"""Some description"""
repository(name: String!, owner: String): Repository!
queryWithDefaultArgs(input: DefaultArgsInput, defaultValue: Int = 3): String
queryWithDefaultArgs(input: DefaultArgsInput, defaultValue: Int = 3, requiredButDefault: Int! = 1): String
optionalArgs(name: String, owner: String): Repository!
user: User
someScalarValue(x: Float): String
Expand All @@ -42,6 +44,8 @@ type RecursiveType {
type Repository {
createdAt: String!
forks(filter: String): ForkConnection
scalarButWithRequiredArgs(x: Int!): String!
customScalar: MyCustomScalar
}

type ForkConnection {
Expand All @@ -62,6 +66,7 @@ type User {
"""Some description"""
name: String
common: Int
commonButDiffType: Int
}

type Subscription {
Expand All @@ -73,6 +78,7 @@ union Account = User | Guest
type Guest {
anonymous: Boolean
common: Int
commonButDiffType: String
}

type House implements Point {
Expand Down
11 changes: 10 additions & 1 deletion integration-tests/generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* eslint-disable */

export type Scalars = {
MyCustomScalar: { x: string },
String: string,
Int: number,
Float: number,
Expand Down Expand Up @@ -41,6 +42,8 @@ export interface RecursiveType {
export interface Repository {
createdAt: Scalars['String']
forks: (ForkConnection | null)
scalarButWithRequiredArgs: Scalars['String']
customScalar: (Scalars['MyCustomScalar'] | null)
__typename: 'Repository'
}

Expand All @@ -65,6 +68,7 @@ export interface User {
/** Some description */
name: (Scalars['String'] | null)
common: (Scalars['Int'] | null)
commonButDiffType: (Scalars['Int'] | null)
__typename: 'User'
}

Expand All @@ -78,6 +82,7 @@ export type Account = (User | Guest) & { __isUnion?: true }
export interface Guest {
anonymous: (Scalars['Boolean'] | null)
common: (Scalars['Int'] | null)
commonButDiffType: (Scalars['String'] | null)
__typename: 'Guest'
}

Expand Down Expand Up @@ -132,7 +137,7 @@ export interface InputWithRequiredFields {requiredField: Scalars['String'],optio
export interface QueryGenqlSelection{
/** Some description */
repository?: (RepositoryGenqlSelection & { __args: {name: Scalars['String'], owner?: (Scalars['String'] | null)} })
queryWithDefaultArgs?: { __args: {input?: (DefaultArgsInput | null), defaultValue?: (Scalars['Int'] | null)} } | boolean | number
queryWithDefaultArgs?: { __args: {input?: (DefaultArgsInput | null), defaultValue?: (Scalars['Int'] | null), requiredButDefault?: Scalars['Int']} } | boolean | number
optionalArgs?: (RepositoryGenqlSelection & { __args?: {name?: (Scalars['String'] | null), owner?: (Scalars['String'] | null)} })
user?: UserGenqlSelection
someScalarValue?: { __args: {x?: (Scalars['Float'] | null)} } | boolean | number
Expand All @@ -159,6 +164,8 @@ export interface RecursiveTypeGenqlSelection{
export interface RepositoryGenqlSelection{
createdAt?: boolean | number
forks?: (ForkConnectionGenqlSelection & { __args?: {filter?: (Scalars['String'] | null)} })
scalarButWithRequiredArgs?: { __args: {x: Scalars['Int']} }
customScalar?: boolean | number
__typename?: boolean | number
__scalar?: boolean | number
}
Expand Down Expand Up @@ -187,6 +194,7 @@ export interface UserGenqlSelection{
/** Some description */
name?: boolean | number
common?: boolean | number
commonButDiffType?: boolean | number
__typename?: boolean | number
__scalar?: boolean | number
}
Expand All @@ -206,6 +214,7 @@ export interface AccountGenqlSelection{
export interface GuestGenqlSelection{
anonymous?: boolean | number
common?: boolean | number
commonButDiffType?: boolean | number
__typename?: boolean | number
__scalar?: boolean | number
}
Expand Down
Loading

1 comment on commit 5a6ac82

@vercel
Copy link

@vercel vercel bot commented on 5a6ac82 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

genql – ./

genql.vercel.app
genql.dev
genql-gang.vercel.app
genql-git-master-gang.vercel.app
www.genql.dev

Please sign in to comment.