Graphql 如何使用nexus prisma实现嵌套变异解析器
我有以下数据模型:Graphql 如何使用nexus prisma实现嵌套变异解析器,graphql,prisma,prisma-graphql,nexus-prisma,Graphql,Prisma,Prisma Graphql,Nexus Prisma,我有以下数据模型: type Job { // ... example: String selections: [Selection!] // ... } type Selection { ... question: String ... } 我定义对象类型,以便: export const Job = prismaObjectType({ name: 'Job', definition(t) { t.prismaF
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
}
}
或
我注意到,使用stringArg({list:true})
可以执行,但不能真正执行对象
我的主要问题是,进行嵌套变异或连接一体式变异的最优雅方式是什么。您可以使用,如文档所示:
export const SomeFieldInput = inputObjectType({
name: "SomeFieldInput",
definition(t) {
t.string("name", { required: true });
t.int("priority");
},
});
确保将该类型作为传递给makeSchema
的类型的一部分。然后可以使用它定义参数,如
args: {
input: arg({
type: "SomeFieldInput", // name should match the name you provided
}),
}
现在,参数值将作为常规JavaScript对象(而不是字符串)提供给解析器。如果需要输入对象列表,或希望使参数成为必需参数,可以使用标量--列表
、可空
、说明
,等等
下面是一个完整的示例:
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)
}
这似乎正是我需要的将审查和可能分配赏金感谢一堆!文档不是很清楚,还是我自己??谢谢你!!是的,nexus
非常新,文档也非常稀少。他们接受PRs对文档的任何更正或添加:)FWIW我用更完整的示例schemaHey更新了答案!我从来没有真正检查过,因为我已经以另一种方式实现了它,我得到:错误:预期SomeFieldInput是有效的输出类型,看到GraphQLInputObjectType
有什么想法吗?谢谢很抱歉迟了回复!
query($input: SomeFieldInput) {
someField(input: $input)
}