Javascript 对象作为突变中的输入变量:GraphQL-Apollo-React
我在两个独立的repo中有一个React客户端项目和一个Node.js/graphqlapi 在我的React应用程序中,我想将一个对象作为变量类型传递到我的应用程序中。以下是我的突变情况:Javascript 对象作为突变中的输入变量:GraphQL-Apollo-React,javascript,node.js,reactjs,graphql,react-apollo,Javascript,Node.js,Reactjs,Graphql,React Apollo,我在两个独立的repo中有一个React客户端项目和一个Node.js/graphqlapi 在我的React应用程序中,我想将一个对象作为变量类型传递到我的应用程序中。以下是我的突变情况: export const CREATE_SPEAKER = gql` input Expertise { title: String! domain: String! } mutation CreateSpeaker( $name: String! $age:
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);
}
}
但我在尝试创建演讲者时遇到以下错误:
未捕获(承诺中)不变冲突:架构类型定义不可用
在查询中允许。找到:“InputObjectTypeDefinition”
有什么建议/想法吗?在向GraphQL服务发送请求时,您不能定义其他类型,您不需要——只需使用服务器上已定义的类型(在本例中,
ExpertiseInput
:
$expertise: ExpertiseInput!
但是,首先不需要使用这么多变量:
mutation CreateSpeaker($input: SpeakerInput!) {
createSpeaker(speakerInput: $input) {
name
age
nationality
avatar
expertise {
title
domain
}
}
}