Skip to content

Commit d44093c

Browse files
committed
Added extend modifier
1 parent 3fdff10 commit d44093c

File tree

9 files changed

+193
-79
lines changed

9 files changed

+193
-79
lines changed

examples/extend.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { g, Infer, InferResolvers, buildSchema } from './../src/index'
2+
import { createYoga } from 'graphql-yoga'
3+
import { createServer } from 'http'
4+
5+
const node = {
6+
id: g.id()
7+
}
8+
9+
const name = {
10+
name: g.string()
11+
}
12+
13+
const test = g.type('Test', {}).extend([node, name])
14+
15+
const queryType = g.type('Query', {
16+
test: g.ref(() => test)
17+
})
18+
19+
type Test = Infer<typeof test>
20+
21+
const resolvers: InferResolvers<{ Query: typeof queryType, Test: typeof test }, {}> = {
22+
Query: {
23+
test: (parent, args, context, info) => {
24+
return {
25+
id: '123',
26+
name: 'Test'
27+
}
28+
}
29+
},
30+
Test: {
31+
id: (parent, args, context, info) => {
32+
return parent.id
33+
},
34+
name: (parent, args, context, info) => {
35+
return parent.name
36+
}
37+
}
38+
}
39+
40+
const schema = buildSchema({ g, resolvers })
41+
const yoga = createYoga({ schema })
42+
const server = createServer(yoga)
43+
server.listen(4000, () => {
44+
console.info('Server is running on http://localhost:4000/graphql')
45+
})

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "git",
66
"url": "https://github.com/stepci/garph"
77
},
8-
"version": "0.6.2",
8+
"version": "0.6.3",
99
"license": "MIT",
1010
"main": "dist/index.js",
1111
"scripts": {

src/index.ts

+31-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export type TypeDefinition<T> = {
3636
deprecated?: string
3737
scalarOptions?: ScalarOptions<any, any>
3838
defaultValue?: any
39-
interfaces?: AnyType[]
39+
interfaces?: AnyInterface[]
40+
extend?: AnyTypes[]
4041
}
4142

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

180-
constructor(name: string, shape: T, interfaces?: AnyInterface[]) {
181+
constructor(name: string, shape: T, interfaces?: AnyInterface[], extend?: AnyTypes[]) {
181182
super()
182183
this.typeDef = {
183184
name,
184185
type: 'ObjectType',
185186
shape,
186-
interfaces
187+
interfaces,
188+
extend
187189
}
188190
}
189191

190192
implements<D extends AnyInterface>(ref: D | D[]) {
191193
// This is temporary construct, until we figure out how to properly manage to shared schema
192194
this.typeDef.interfaces = Array.isArray(ref) ? ref : [ref]
193-
return new GType<N, T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref])
195+
return new GType<N, T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref], this.typeDef.extend)
196+
}
197+
198+
extend<D extends AnyTypes>(ref: D | D[]) {
199+
// This is temporary construct, until we figure out how to properly manage to shared schema
200+
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
201+
return new GType<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, this.typeDef.interfaces, Array.isArray(ref) ? ref : [ref])
194202
}
195203
}
196204

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

200-
constructor(name: string, shape: T) {
208+
constructor(name: string, shape: T, extend?: AnyTypes[]) {
201209
super()
202210
this.typeDef = {
203211
name,
204212
type: 'InputType',
205-
shape
213+
shape,
214+
extend
206215
}
207216
}
217+
218+
extend<D extends AnyTypes>(ref: D | D[]) {
219+
// This is temporary construct, until we figure out how to properly manage to shared schema
220+
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
221+
return new GInput<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref])
222+
}
208223
}
209224

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

213-
constructor(name: string, shape: T, interfaces?: AnyInterface[]) {
228+
constructor(name: string, shape: T, interfaces?: AnyInterface[], extend?: AnyTypes[]) {
214229
super()
215230
this.typeDef = {
216231
name,
217232
type: 'InterfaceType',
218233
shape,
219-
interfaces
234+
interfaces,
235+
extend
220236
}
221237
}
222238

223239
implements<D extends AnyInterface>(ref: D | D[]) {
224240
// This is temporary construct, until we figure out how to properly manage to shared schema
225241
this.typeDef.interfaces = Array.isArray(ref) ? ref : [ref]
226-
return new GInterface<N ,T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref])
242+
return new GInterface<N ,T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref], this.typeDef.extend)
243+
}
244+
245+
extend<D extends AnyTypes>(ref: D | D[]) {
246+
// This is temporary construct, until we figure out how to properly manage to shared schema
247+
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
248+
return new GInterface<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, this.typeDef.interfaces, Array.isArray(ref) ? ref : [ref])
227249
}
228250
}
229251

src/schema.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export function convertToGraphqlType(schemaComposer: SchemaComposer, name: strin
9898
})
9999
}
100100

101+
if (type.typeDef.extend) {
102+
type.typeDef.extend.forEach(i => {
103+
objType.addFields(parseFields(schemaComposer, name, i as any, config, resolvers))
104+
})
105+
}
106+
101107
return objType
102108
case 'Enum':
103109
return schemaComposer.createEnumTC({
@@ -116,11 +122,19 @@ export function convertToGraphqlType(schemaComposer: SchemaComposer, name: strin
116122
resolveType: resolvers?.resolveType
117123
})
118124
case 'InputType':
119-
return schemaComposer.createInputTC({
125+
const inputType = schemaComposer.createInputTC({
120126
name,
121127
description: type.typeDef.description,
122128
fields: parseFields(schemaComposer, name, type.typeDef.shape, config),
123129
})
130+
131+
if (type.typeDef.extend) {
132+
type.typeDef.extend.forEach(i => {
133+
inputType.addFields(parseFields(schemaComposer, name, i as any, config, resolvers))
134+
})
135+
}
136+
137+
return inputType
124138
case 'Scalar':
125139
return schemaComposer.createScalarTC({
126140
name,
@@ -145,6 +159,12 @@ export function convertToGraphqlType(schemaComposer: SchemaComposer, name: strin
145159
})
146160
}
147161

162+
if (type.typeDef.extend) {
163+
type.typeDef.extend.forEach(i => {
164+
interfaceType.addFields(parseFields(schemaComposer, name, i as any, config, resolvers))
165+
})
166+
}
167+
148168
return interfaceType
149169
}
150170
}

www/api/classes/GarphSchema.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#### Defined in
1919

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

2222
## Properties
2323

@@ -27,7 +27,7 @@
2727

2828
#### Defined in
2929

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

3232
___
3333

@@ -46,7 +46,7 @@ ___
4646

4747
#### Defined in
4848

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

5151
___
5252

@@ -56,7 +56,7 @@ ___
5656

5757
#### Defined in
5858

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

6161
___
6262

@@ -66,7 +66,7 @@ ___
6666

6767
#### Defined in
6868

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

7171
## Methods
7272

@@ -80,7 +80,7 @@ ___
8080

8181
#### Defined in
8282

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

8585
___
8686

@@ -108,7 +108,7 @@ ___
108108

109109
#### Defined in
110110

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

113113
___
114114

@@ -136,7 +136,7 @@ ___
136136

137137
#### Defined in
138138

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

141141
___
142142

@@ -164,7 +164,7 @@ ___
164164

165165
#### Defined in
166166

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

169169
___
170170

@@ -178,7 +178,7 @@ ___
178178

179179
#### Defined in
180180

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

183183
___
184184

@@ -192,7 +192,7 @@ ___
192192

193193
#### Defined in
194194

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

197197
___
198198

@@ -220,7 +220,7 @@ ___
220220

221221
#### Defined in
222222

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

225225
___
226226

@@ -234,7 +234,7 @@ ___
234234

235235
#### Defined in
236236

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

239239
___
240240

@@ -262,7 +262,7 @@ ___
262262

263263
#### Defined in
264264

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

267267
___
268268

@@ -290,7 +290,7 @@ ___
290290

291291
#### Defined in
292292

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

295295
___
296296

@@ -316,7 +316,7 @@ ___
316316

317317
#### Defined in
318318

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

321321
___
322322

@@ -344,7 +344,7 @@ ___
344344

345345
#### Defined in
346346

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

349349
___
350350

@@ -358,7 +358,7 @@ ___
358358

359359
#### Defined in
360360

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

363363
___
364364

@@ -386,7 +386,7 @@ ___
386386

387387
#### Defined in
388388

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

391391
___
392392

@@ -414,4 +414,4 @@ ___
414414

415415
#### Defined in
416416

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

0 commit comments

Comments
 (0)