Skip to content

Commit e851bd3

Browse files
committed
feat(functionality to extend types): added createBuild().extendType(type: string, config: TypeConfig
fix #111
1 parent 5f04370 commit e851bd3

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { graphql } from 'graphql'
2+
import { createSchemaBuilder } from '..'
3+
4+
it('should not at inherit to interface definitions', async () => {
5+
const builder = createSchemaBuilder()
6+
7+
const model = builder.model('Model', {
8+
findOne: async () => ({
9+
id: '1234567890',
10+
created_at: new Date(),
11+
updated_at: new Date(),
12+
deleted_at: new Date(),
13+
}),
14+
})
15+
model.attr('someInt', 'Int')
16+
model.resolve(() => ({
17+
someInt: () => 123,
18+
}))
19+
20+
const build = builder.createBuild()
21+
build.extendType('Model', {
22+
fields: {
23+
someStr: 'String!',
24+
},
25+
resolver: {
26+
someStr: () => 'New String',
27+
},
28+
})
29+
30+
const schema = build.toSchema()
31+
32+
const { data } = await graphql({
33+
schema,
34+
source: '{ getModel(where: {}) { someInt someStr }}',
35+
})
36+
expect(data.getModel).toEqual({
37+
someInt: 123,
38+
someStr: 'New String',
39+
})
40+
})

src/createBuild/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IFieldResolver, makeExecutableSchema } from 'graphql-tools'
22

33
import { forEach, identity } from 'lodash'
44
import { generateTypeDefs } from './generateTypeDefs'
5-
import { createAddType } from './method-addType'
5+
import { AddObjectTypeConfig, createAddType } from './method-addType'
66
import { resolvablesCreator } from './method-resolvablesCreator'
77
import {
88
AddResolvableConfig,
@@ -102,6 +102,26 @@ export const createBuild = <BuildMode = null, Context = any>(
102102
},
103103
)
104104

105+
const extendType = <Source>(
106+
typeName: string,
107+
config: AddObjectTypeConfig<BuildMode, Source, Context>,
108+
) => {
109+
const baseConfig = types.type[typeName]
110+
if (!baseConfig)
111+
throw new Error('Cannot extend an type that is not defined!')
112+
if (config.resolver) addResolvers(typeName, config.resolver)
113+
const fields = convertSimpleFieldsToFields(buildModeResolver(config.fields))
114+
const interfaces = baseConfig.interface
115+
.split('&')
116+
.concat((buildModeResolver(config.interface) || '').split('&'))
117+
.filter(_ => _)
118+
types.type[typeName] = {
119+
// adding new fields
120+
fields: { ...baseConfig.fields, ...fields },
121+
interface: interfaces.join('&'),
122+
}
123+
}
124+
105125
const builder: Build<BuildMode, Context> = {
106126
type: GQLBUILDER,
107127
buildMode,
@@ -110,6 +130,7 @@ export const createBuild = <BuildMode = null, Context = any>(
110130
addResolver,
111131
addSubscription: createResolvable('Subscription'),
112132
addType,
133+
extendType,
113134
isScalar: type =>
114135
['String', 'Int', 'Float', 'ID'].includes(type) ||
115136
types.scalar.includes(type),

src/createBuild/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IFieldResolver, IResolvers, ITypeDefinitions } from 'graphql-tools'
33

44
import { GQLBUILDER } from '../types/constants'
55

6-
import { createAddType } from './method-addType'
6+
import { AddObjectTypeConfig, createAddType } from './method-addType'
77

88
// Context == GraphQLContext ({ user: {…}, db: DBConnection })
99
// BuildMode == BuildMode of the build ('user' | 'admin')
@@ -24,6 +24,10 @@ export interface Build<BuildMode, Context> {
2424
) => void
2525
addSubscription: AddResolvable<BuildMode, Context>
2626
addType: ReturnType<typeof createAddType>
27+
extendType: <Source>(
28+
typeName: string,
29+
config: AddObjectTypeConfig<BuildMode, Source, Context>,
30+
) => void
2731
isScalar: (type: string) => boolean
2832
isType: (type: string) => boolean
2933
toSchema: () => GraphQLSchema

0 commit comments

Comments
 (0)