我有一个插件,可以在用户创建标签时使用{id:“ XXX”,text:“ XXX”}创建对象。我正在尝试让我的阿波罗graphql服务器接受此对象作为我的架构中的类型。我尝试了很多事情,但是对我来说最有意义的事情是创建一个具有该对象形状的类型并使用它。这似乎适用于原始类型,但不适用于输入(更新和新输入)。我收到一个错误:错误:架构必须包含唯一的命名类型,但包含多个名为“ CustomQuestionOption”的类型。
const customQuestionTypeDefs = `
type CustomQuestionOption {
id: ID,
text: String
}
type CustomQuestion {
id: ID!
question: String
questionType: String
options: [CustomQuestionOption]
required: Boolean
}
input UpdatedCustomQuestion {
id: ID!
question: String
questionType: String
options: [CustomQuestionOption]
required: Boolean
}
input NewCustomQuestion {
question: String
questionType: String
options: [CustomQuestionOption]
required: Boolean
}
extend type Query {
CustomQuestion(id: ID!): CustomQuestion!
allCustomQuestions: [CustomQuestion]!
}
extend type Mutation {
newCustomQuestion(input: NewCustomQuestion!): CustomQuestion!
updateCustomQuestion(input: UpdatedCustomQuestion!): CustomQuestion!
}
`;
export default customQuestionTypeDefs;
然后合并在一起
const baseSchema = `
schema {
query: Query
mutation: Mutation
}
`;
export const schema = makeExecutableSchema({
typeDefs: [baseSchema, eventTypeDefs, sponsorshipItemTypeDefs, customQuestionTypeDefs, userTypeDefs],
resolvers: merge({}, eventResolvers, sponsorshipItemResolvers, customQuestionResolvers, userResolvers)
});
是否有办法让我的服务器在输入中接受该对象,还是必须另存为字符串并在保存和加载时将其转换? (即“ [[{id:'S',text:'S'}]]”)
答案 0 :(得分:1)
我想我要创建的是自定义标量(与诸如字符串,int等内置标量相反),我只需要将customQuestionOption更改为标量即可。 编辑-见下文
const customQuestionTypeDefs = `
scalar CustomQuestionOption {
id: String,
text: String
}
type CustomQuestion {
id: ID!
question: String
questionType: String
options: [CustomQuestionOption]
required: Boolean
}
input UpdatedCustomQuestion {
id: ID!
question: String
questionType: String
options: [CustomQuestionOption]
required: Boolean
}
input NewCustomQuestion {
question: String
questionType: String
options: [CustomQuestionOption]
required: Boolean
}
extend type Query {
CustomQuestion(id: ID!): CustomQuestion!
allCustomQuestions: [CustomQuestion]!
}
extend type Mutation {
newCustomQuestion(input: NewCustomQuestion!): CustomQuestion!
updateCustomQuestion(input: UpdatedCustomQuestion!): CustomQuestion!
}
`;
export default customQuestionTypeDefs;
除了无法查询对象的各个属性外,此方法都工作正常。真正的解决方案是将CustomQuestionOption改回要在查询中使用的类型,并创建另一个输入(我称为InputCustomQuestionOption)并将其用于最终输入:
const customQuestionTypeDefs = `
type CustomQuestionOption {
id: String,
text: String
}
input InputCustomQuestionOption {
id: String,
text: String
}
type CustomQuestion {
id: ID!
question: String
questionType: String
options: [CustomQuestionOption]
required: Boolean
}
input UpdatedCustomQuestion {
id: ID!
question: String
questionType: String
options: [InputCustomQuestionOption]
required: Boolean
}
input NewCustomQuestion {
question: String
questionType: String
options: [InputCustomQuestionOption]
required: Boolean
}