diff --git a/examples/internal.ts b/examples/internal.ts new file mode 100644 index 0000000..7364814 --- /dev/null +++ b/examples/internal.ts @@ -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() +}) + +const queryType = g.type('Query', { + greet: g.ref(personType) + .args({ + name: g.string().optional().default('Max'), + }) + .description('Greets a person') +}) + +type QueryType = Infer + +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') +}) diff --git a/src/index.ts b/src/index.ts index 2a19466..e4b9913 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { _name?: string @@ -48,6 +48,7 @@ export type AnyNumber = Type export type AnyInt = Type export type AnyFloat = Type export type AnyRef = Type +export type AnyInternal = Type export type AnyList = Type export type AnyPaginatedList = Type export type AnyUnion = Type @@ -115,6 +116,7 @@ export type InferShallow : T extends AnyArgs ? InferRaw : T extends AnyRef ? InferRaw : + T extends AnyInternal ? T['_shape'] : T export type InferArgs = ExpandRecursively> @@ -432,6 +434,28 @@ class GRef extends Type { } } +class GInternal extends Type { + constructor() { + super() + this.typeDef = { + type: 'Internal' + } + } + + optional() { + return new GOptional(this) + } + + required() { + this.typeDef.isRequired = true + return this + } + + omitResolver () { + return new GOmitResolver(this) + } +} + class GScalar extends Type { declare _output: O @@ -717,6 +741,10 @@ export class GarphSchema { return new GRef(ref) } + internal(){ + return new GInternal() + } + // enum(args: T[]) { // return new GEnum('', args) // } diff --git a/src/schema.ts b/src/schema.ts index 10d0908..5483f10 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -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) } @@ -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),