[英]Passing input value to my graphql mutation - react/react-apollo
[英]Object as input variable in mutation: GraphQL - Apollo - React
我在两个单独的存储库中有一个 React 客户端项目和一个 Node.js/GraphQL api。
在我的 React 应用程序中,我想将一个对象作为变量类型传递到我的突变中。 这是我的突变的样子:
export const CREATE_SPEAKER = gql`
input Expertise {
title: String!
domain: String!
}
mutation CreateSpeaker(
$name: String!
$age: String!
$nationality: String!
$avatar: String!
$expertise: Expertise!
) {
createSpeaker(
speakerInput: {
name: $name
age: $age
nationality: $nationality
avatar: $avatar
expertise: $expertise
}
) {
name
age
nationality
avatar
expertise {
title
domain
}
}
}
`;
在我的 Node.js 项目中,我有以下架构:
input SpeakerInput {
name: String!
age: String!
expertise: ExpertiseInput!
nationality: String!
avatar: String
}
input ExpertiseInput {
title: String!
domain: String!
}
还有我的解析器:
createSpeaker: async args => {
const { name, age, nationality, avatar, expertise } = args.speakerInput;
const newSpeaker = new Speaker({
name,
age,
nationality,
avatar,
expertise: {
title: expertise.title,
domain: expertise.domain
}
});
try {
return await newSpeaker.save();
} catch (error) {
throw ("Failed to create speaker:: ", error);
}
}
但是在尝试创建扬声器时出现以下错误:
Uncaught (in promise) Invariant Violation:查询中不允许架构类型定义。 找到:“InputObjectTypeDefinition”
任何建议/想法如何做到这一点?
在向 GraphQL 服务发送请求时,您不能定义其他类型,也不需要——只需使用您已经在服务器上定义的类型(在本例中为ExpertiseInput
:
$expertise: ExpertiseInput!
但是,一开始就没有必要使用这么多变量:
mutation CreateSpeaker($input: SpeakerInput!) {
createSpeaker(speakerInput: $input) {
name
age
nationality
avatar
expertise {
title
domain
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.