【发布时间】:2021-07-20 02:37:20
【问题描述】:
我是 GraphQL 的初学者,将它与 Node 和 graphql-compose 一起使用来创建模式。
我不确定这样做是否正确:AddressTC 是可重用类型,当创建或更新UserTC 或其他类型时,我想自动触发AddressTC 的验证。如果你知道更好的方法,我会接受的。
// userType.js
// In this example we'll use UserTC but this can another type which also use AdressTC.
const UserTC = schemaComposer.createObjectTC({
name: 'User',
fields: {
id: 'String',
name: 'String',
slug: 'String',
actived: 'Boolean',
registered: 'Boolean',
address: AddressTC,
}
});
const UserITC = UserTC.getInputTypeComposer()
UserTC.addResolver({
kind: 'mutation',
name: 'create',
args: {
data: UserITC
},
type: UserTC,
resolve: async ({args: {data}, context}) => {
// Need to trigger validation and geocode of AddressTC
// do something...
// save in database
},
})
// addressType.js
// reusable Address Type
const AddressTC = schemaComposer.createObjectTC({
name: 'Address',
description: 'Type of address',
fields: {
street: 'String',
number: 'String',
postcode: 'String',
city: 'String',
comment: 'String',
country: 'String',
quality: QualityETC
}
});
const AddressITC = AddressTC.getInputTypeComposer()
AddressTC.addResolver({
kind: 'mutation',
name: 'validation',
args: {
data: AddressITC
},
type: AddressTC,
resolve: async ({args: {data}, context}) => {
// When address is puted or updated :
// Make validation
// Geocode {Lat,Lng} with map provider
// Save in DB
},
})
【问题讨论】:
标签: node.js graphql graphql-compose