Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal properties support #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { g, InferResolvers, buildSchema, Infer } from './../src/index'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'

const personType = g.type('Person', {
name: g.string(),
id: g.internal<number>()
})

const queryType = g.type('Query', {
greet: g.ref(personType)
.args({
name: g.string().optional().default('Max'),
})
.description('Greets a person')
})

type QueryType = Infer<typeof queryType>

const resolvers: InferResolvers<{ Query: typeof queryType }, {}> = {
Query: {
greet: (parent, args, context, info) => {
return {
name: `Hello, ${args.name}`,
id: 123
}
}
}
}

const schema = buildSchema({ g, resolvers })
const yoga = createYoga({ schema })
const server = createServer(yoga)
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql')
})
30 changes: 29 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TSEnumType, UnionToIntersection, getEnumProperties, ObjectToUnion, Expa
import { buildSchema, printSchema } from './schema'

type GraphQLRootType = 'Query' | 'Mutation' | 'Subscription'
type GarphType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID' | 'ObjectType' | 'InterfaceType' | 'InputType' | 'Scalar' | 'Enum' | 'List' | 'PaginatedList' | 'Union' | 'Ref' | 'Optional' | 'Args' | 'OmitResolver'
type GarphType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID' | 'ObjectType' | 'InterfaceType' | 'InputType' | 'Scalar' | 'Enum' | 'List' | 'PaginatedList' | 'Union' | 'Ref' | 'Internal' | 'Optional' | 'Args' | 'OmitResolver'

export abstract class Type<T, X extends GarphType> {
_name?: string
Expand Down Expand Up @@ -48,6 +48,7 @@ export type AnyNumber = Type<number, any>
export type AnyInt = Type<number, 'Int'>
export type AnyFloat = Type<number, 'Float'>
export type AnyRef = Type<any, 'Ref'>
export type AnyInternal = Type<any, 'Internal'>
export type AnyList = Type<any, 'List'>
export type AnyPaginatedList = Type<any, 'PaginatedList'>
export type AnyUnion = Type<any, 'Union'>
Expand Down Expand Up @@ -115,6 +116,7 @@ export type InferShallow<T, options extends InferOptions = { omitResolver: never
T extends AnyOmitResolver ? InferRaw<T['_shape'], options> :
T extends AnyArgs ? InferRaw<T['_shape'], options> :
T extends AnyRef ? InferRaw<T['_inner'], options> :
T extends AnyInternal ? T['_shape'] :
T

export type InferArgs<T extends AnyType> = ExpandRecursively<InferArgsRaw<T>>
Expand Down Expand Up @@ -432,6 +434,28 @@ class GRef<T> extends Type<T, 'Ref'> {
}
}

class GInternal<T> extends Type<T, 'Internal'> {
constructor() {
super()
this.typeDef = {
type: 'Internal'
}
}

optional() {
return new GOptional<this>(this)
}

required() {
this.typeDef.isRequired = true
return this
}

omitResolver () {
return new GOmitResolver<this>(this)
}
}

class GScalar<I, O> extends Type<I, 'Scalar'> {
declare _output: O

Expand Down Expand Up @@ -717,6 +741,10 @@ export class GarphSchema {
return new GRef<T>(ref)
}

internal<T>(){
return new GInternal<T>()
}

// enum<T extends string>(args: T[]) {
// return new GEnum<T>('', args)
// }
Expand Down
3 changes: 3 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export function getFieldType(schemaComposer: SchemaComposer, type: AnyType, conf
}

return isOptional(shape.typeDef.name, type, config)
case 'Internal':
break
default:
return isOptional(type.typeDef.name, type, config)
}
Expand Down Expand Up @@ -173,6 +175,7 @@ export function parseFields(schemaComposer: SchemaComposer, name: string, fields
const fieldsObj = {}
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName]
if (field.typeDef.type === 'Internal') return

fieldsObj[fieldName] = {
type: getFieldType(schemaComposer, field, config),
Expand Down