我有以下数据模型:
type Job {
// ...
example: String
selections: [Selection!]
// ...
}
type Selection {
...
question: String
...
}我这样定义我的对象类型:
export const Job = prismaObjectType({
name: 'Job',
definition(t) {
t.prismaFields([
// ...
'example',
{
name: 'selections',
},
// ...
])
},
})我这样做我的解析器:
t.field('createJob', {
type: 'Job',
args: {
// ...
example: stringArg(),
selections: stringArg(),
// ...
},
resolve: (parent, {
example,
selections
}, ctx) => {
// The resolver where I do a ctx.prisma.createJob and connect/create with example
},
})因此,现在在解析器中,我可以接收json字符串形式的选择,然后对其进行解析并使用作业进行连接/创建。
突变将如下所示:
mutation {
createJob(
example: "bla"
selections: "ESCAPED JSON HERE"
){
id
}
}我想知道有没有更优雅的,我可以这样做的:
mutation {
createJob(
example: "bla"
selections: {
question: "bla"
}
){
id
}
}或
mutation {
createJob(
example: "bla"
selections(data: {
// ...
})
){
id
}
}我注意到,使用nexus-prisma可以执行stringArg({list: true}),但不能真正执行对象。
我的主要问题是什么是做嵌套突变或连接在一起的最优雅的方式。
发布于 2019-04-29 08:09:33
您可以使用文档中所示的inputObjectType:
export const SomeFieldInput = inputObjectType({
name: "SomeFieldInput",
definition(t) {
t.string("name", { required: true });
t.int("priority");
},
});确保将类型作为传递给makeSchema的types的一部分包含在内。然后您可以使用它来定义参数,例如
args: {
input: arg({
type: "SomeFieldInput", // name should match the name you provided
}),
}现在,参数值将作为常规JavaScript对象而不是字符串提供给解析器。如果您需要输入对象的列表,或者希望使参数成为必需的,那么可以使用在使用标量时提供的same options来实现-- list、nullable、description等。
下面是一个完整的示例:
const Query = queryType({
definition(t) {
t.field('someField', {
type: 'String',
nullable: true,
args: {
input: arg({
type: "SomeFieldInput", // name should match the name you provided
}),
},
resolve: (parent, { input }) => {
return `You entered: ${input && input.name}`
},
})
},
})
const SomeFieldInput = inputObjectType({
name: "SomeFieldInput",
definition(t) {
t.string("name", { required: true });
},
});
const schema = makeSchema({
types: {Query, SomeFieldInput},
outputs: {
...
},
});然后按如下方式查询:
query {
someField(
input: {
name: "Foo"
}
)
}或者使用变量:
query($input: SomeFieldInput) {
someField(input: $input)
}https://stackoverflow.com/questions/55824050
复制相似问题