Skip to content

Commit

Permalink
Added extend modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mishushakov committed Nov 5, 2023
1 parent 3fdff10 commit d44093c
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 79 deletions.
45 changes: 45 additions & 0 deletions examples/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { g, Infer, InferResolvers, buildSchema } from './../src/index'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'

const node = {
id: g.id()
}

const name = {
name: g.string()
}

const test = g.type('Test', {}).extend([node, name])

const queryType = g.type('Query', {
test: g.ref(() => test)
})

type Test = Infer<typeof test>

const resolvers: InferResolvers<{ Query: typeof queryType, Test: typeof test }, {}> = {
Query: {
test: (parent, args, context, info) => {
return {
id: '123',
name: 'Test'
}
}
},
Test: {
id: (parent, args, context, info) => {
return parent.id
},
name: (parent, args, context, info) => {
return parent.name
}
}
}

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')
})
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "git",
"url": "https://github.com/stepci/garph"
},
"version": "0.6.2",
"version": "0.6.3",
"license": "MIT",
"main": "dist/index.js",
"scripts": {
Expand Down
40 changes: 31 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export type TypeDefinition<T> = {
deprecated?: string
scalarOptions?: ScalarOptions<any, any>
defaultValue?: any
interfaces?: AnyType[]
interfaces?: AnyInterface[]
extend?: AnyTypes[]
}

export type AnyType = Type<any, any>
Expand Down Expand Up @@ -177,53 +178,74 @@ export type InferResolversStrict<T extends AnyTypes, X extends InferResolverConf
class GType<N extends string, T extends AnyTypes> extends Type<T, 'ObjectType'> {
declare _name: N

constructor(name: string, shape: T, interfaces?: AnyInterface[]) {
constructor(name: string, shape: T, interfaces?: AnyInterface[], extend?: AnyTypes[]) {
super()
this.typeDef = {
name,
type: 'ObjectType',
shape,
interfaces
interfaces,
extend
}
}

implements<D extends AnyInterface>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.interfaces = Array.isArray(ref) ? ref : [ref]
return new GType<N, T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref])
return new GType<N, T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref], this.typeDef.extend)
}

extend<D extends AnyTypes>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
return new GType<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, this.typeDef.interfaces, Array.isArray(ref) ? ref : [ref])
}
}

class GInput<N extends string, T extends AnyTypes> extends Type<T, 'InputType'> {
declare _name: N

constructor(name: string, shape: T) {
constructor(name: string, shape: T, extend?: AnyTypes[]) {
super()
this.typeDef = {
name,
type: 'InputType',
shape
shape,
extend
}
}

extend<D extends AnyTypes>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
return new GInput<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref])
}
}

class GInterface<N extends string, T extends AnyTypes> extends Type<T, 'InterfaceType'> {
declare _name: N

constructor(name: string, shape: T, interfaces?: AnyInterface[]) {
constructor(name: string, shape: T, interfaces?: AnyInterface[], extend?: AnyTypes[]) {
super()
this.typeDef = {
name,
type: 'InterfaceType',
shape,
interfaces
interfaces,
extend
}
}

implements<D extends AnyInterface>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.interfaces = Array.isArray(ref) ? ref : [ref]
return new GInterface<N ,T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref])
return new GInterface<N ,T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref], this.typeDef.extend)
}

extend<D extends AnyTypes>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
return new GInterface<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, this.typeDef.interfaces, Array.isArray(ref) ? ref : [ref])
}
}

Expand Down
22 changes: 21 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export function convertToGraphqlType(schemaComposer: SchemaComposer, name: strin
})
}

if (type.typeDef.extend) {
type.typeDef.extend.forEach(i => {
objType.addFields(parseFields(schemaComposer, name, i as any, config, resolvers))
})
}

return objType
case 'Enum':
return schemaComposer.createEnumTC({
Expand All @@ -116,11 +122,19 @@ export function convertToGraphqlType(schemaComposer: SchemaComposer, name: strin
resolveType: resolvers?.resolveType
})
case 'InputType':
return schemaComposer.createInputTC({
const inputType = schemaComposer.createInputTC({
name,
description: type.typeDef.description,
fields: parseFields(schemaComposer, name, type.typeDef.shape, config),
})

if (type.typeDef.extend) {
type.typeDef.extend.forEach(i => {
inputType.addFields(parseFields(schemaComposer, name, i as any, config, resolvers))
})
}

return inputType
case 'Scalar':
return schemaComposer.createScalarTC({
name,
Expand All @@ -145,6 +159,12 @@ export function convertToGraphqlType(schemaComposer: SchemaComposer, name: strin
})
}

if (type.typeDef.extend) {
type.typeDef.extend.forEach(i => {
interfaceType.addFields(parseFields(schemaComposer, name, i as any, config, resolvers))
})
}

return interfaceType
}
}
Expand Down
40 changes: 20 additions & 20 deletions www/api/classes/GarphSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#### Defined in

[index.ts:596](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L596)
[index.ts:618](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L618)

## Properties

Expand All @@ -27,7 +27,7 @@

#### Defined in

[index.ts:578](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L578)
[index.ts:600](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L600)

___

Expand All @@ -46,7 +46,7 @@ ___

#### Defined in

[index.ts:589](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L589)
[index.ts:611](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L611)

___

Expand All @@ -56,7 +56,7 @@ ___

#### Defined in

[index.ts:582](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L582)
[index.ts:604](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L604)

___

Expand All @@ -66,7 +66,7 @@ ___

#### Defined in

[index.ts:577](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L577)
[index.ts:599](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L599)

## Methods

Expand All @@ -80,7 +80,7 @@ ___

#### Defined in

[index.ts:681](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L681)
[index.ts:703](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L703)

___

Expand Down Expand Up @@ -108,7 +108,7 @@ ___

#### Defined in

[index.ts:612](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L612)
[index.ts:634](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L634)

___

Expand Down Expand Up @@ -136,7 +136,7 @@ ___

#### Defined in

[index.ts:622](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L622)
[index.ts:644](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L644)

___

Expand Down Expand Up @@ -164,7 +164,7 @@ ___

#### Defined in

[index.ts:641](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L641)
[index.ts:663](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L663)

___

Expand All @@ -178,7 +178,7 @@ ___

#### Defined in

[index.ts:677](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L677)
[index.ts:699](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L699)

___

Expand All @@ -192,7 +192,7 @@ ___

#### Defined in

[index.ts:669](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L669)
[index.ts:691](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L691)

___

Expand Down Expand Up @@ -220,7 +220,7 @@ ___

#### Defined in

[index.ts:635](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L635)
[index.ts:657](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L657)

___

Expand All @@ -234,7 +234,7 @@ ___

#### Defined in

[index.ts:673](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L673)
[index.ts:695](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L695)

___

Expand Down Expand Up @@ -262,7 +262,7 @@ ___

#### Defined in

[index.ts:659](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L659)
[index.ts:681](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L681)

___

Expand Down Expand Up @@ -290,7 +290,7 @@ ___

#### Defined in

[index.ts:606](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L606)
[index.ts:628](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L628)

___

Expand All @@ -316,7 +316,7 @@ ___

#### Defined in

[index.ts:687](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L687)
[index.ts:709](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L709)

___

Expand Down Expand Up @@ -344,7 +344,7 @@ ___

#### Defined in

[index.ts:653](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L653)
[index.ts:675](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L675)

___

Expand All @@ -358,7 +358,7 @@ ___

#### Defined in

[index.ts:665](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L665)
[index.ts:687](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L687)

___

Expand Down Expand Up @@ -386,7 +386,7 @@ ___

#### Defined in

[index.ts:600](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L600)
[index.ts:622](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L622)

___

Expand Down Expand Up @@ -414,4 +414,4 @@ ___

#### Defined in

[index.ts:647](https://github.com/stepci/garph/blob/f2d3630/src/index.ts#L647)
[index.ts:669](https://github.com/stepci/garph/blob/3fdff10/src/index.ts#L669)
Loading

0 comments on commit d44093c

Please sign in to comment.