为什么我的 GraphQL/Apollo 突变失败了?
Why does my GraphQL/Apollo mutation fail?
Apollo 查询是这样定义的:
const createUser = gql`
mutation(
$username: String!,
$email: String!,
$password: String!,
$time_created: String!,
$time_played: Int!,
$verified: Boolean!,
$type_user: Boolean!,
$userLevel: UserLevelInput!,
$ranks: RanksInput!,
$pvp: PvpInput!
){
createUser(
username: $username,
email: $email,
password: $password,
time_created: $time_created,
time_played: $time_played,
verified: $verified,
type_user: $type_user,
userLevel: $userLevel,
ranks: $ranks,
pvp: $pvp
){
username
email
password
}
}
`;
我的架构:
const userSchema = new Schema({
username: String,
email: String,
password: String,
time_created: Date,
time_played: Number,
verified: Boolean,
type_user: Boolean,
userLevel: {
lidUnlocked: Number,
gidUnlocked: Number,
},
ranks: {
level: [
{
level: Number,
avgTime: Number,
rank: Number,
group: [
{
group: Number,
time: Number,
rank: Number,
},
],
},
],
},
pvp: {
points: Number,
rank: Number,
},
});
我是如何提出请求的:
const handleSubmit = (e) => {
e.preventDefault();
addUser({
variables: {
username: input.username,
email: input.email,
password: input.password,
time_created: Date.now(),
time_played: 0,
verified: false,
type_user: false,
userLevel: {
lidUnlocked: 1,
gidUnlocked: 1
},
ranks: {
level: [{
level: 1,
avgTime: 0,
rank: 0,
group: [{
group: 1,
time: 0,
rank: 0
}]
}]
},
pvp: {
points: 0,
rank: 0,
}
}
})
}
UserLevelInput、RanksInput 和 PvpInput:
const UserLevelInputType = new GraphQLInputObjectType({
name: "UserLevelInput",
fields: () => ({
lidUnlocked: { type: GraphQLInt },
gidUnlocked: { type: GraphQLInt },
}),
});
const RanksInputType = new GraphQLInputObjectType({
name: "RanksInput",
fields: () => ({
level: { type: new GraphQLList(LevelInputType) },
}),
});
const LevelInputType = new GraphQLInputObjectType({
name: "LevelInput",
fields: () => ({
level: { type: GraphQLInt },
avgTime: { type: GraphQLInt },
rank: { type: GraphQLInt },
group: { type: new GraphQLList(GroupInputType) },
}),
});
const GroupInputType = new GraphQLInputObjectType({
name: "GroupInput",
fields: () => ({
group: { type: GraphQLInt },
time: { type: GraphQLInt },
rank: { type: GraphQLInt },
}),
});
const PvpInputType = new GraphQLInputObjectType({
name: "PvpInput",
fields: () => ({
points: { type: GraphQLInt },
rank: { type: GraphQLInt },
}),
});
如果我在 localhost:5005/graphql 上进行此更改,它会按预期工作:
mutation{
createUser(
username:"babadany2999",
email:"babadany2999@gmail.com",
password:"Immboold1",
time_created:"1645738406658",
time_played: 0,
verified: false,
type_user: false,
userLevel:{
lidUnlocked: 1,
gidUnlocked: 1
},
ranks: {
level: [{
level: 1,
avgTime: 0,
rank: 0,
group:[{
group: 1,
time: 0,
rank: 0
}]
}]
},
pvp: {
points: 0,
rank: 0
}
), {
username
email
password
}
}
此外,如果我发出请求(代码不在 /graphql 中),然后查看 Apollo Dev 工具以了解该特定突变,我会发现 Int、UserLevelInput、RanksInput 和 PpvInput 类型未知。
Apollo Dev Tools type unknown
对于遇到相同问题的任何人,我设法通过创建复杂对象的常量并简单地将默认值设置为猫鼬 table 中的那些常量而不将其作为输入提供给 apollo 来“修复”它.
username: String,
email: String,
password: String,
time_created: {
type: Date,
default: new Date()
},
time_played: {
type: Number,
default: 0
},
type_user: {
type: Boolean,
default: false
},
verified: {
type: Boolean,
default: false
},
userLevel: {
lidUnlocked: Number,
gidUnlocked: Number
},
ranks: {
type: Object,
default: ranks
},
pvp: {
points: {
type: Number,
default: 0
},
rank: Number
}
})
和常量的一部分(很长但是到最后都是一样的结构):
const ranks= {
level: [
{
level: 1,
group: [
{ group: 1, unlocked: true, time: 0, rank: 0 },
{ group: 2, unlocked: false, time: 0, rank: 0 },
{ group: 3, unlocked: false, time: 0, rank: 0 },
{ group: 4, unlocked: false, time: 0, rank: 0 },
{ group: 5, unlocked: false, time: 0, rank: 0 },
{ group: 6, unlocked: false, time: 0, rank: 0 },
{ group: 7, unlocked: false, time: 0, rank: 0 },
{ group: 8, unlocked: false, time: 0, rank: 0 },
{ group: 9, unlocked: false, time: 0, rank: 0 },
{ group: 10, unlocked: false, time: 0, rank: 0 },
{ group: 11, unlocked: false, time: 0, rank: 0 },
{ group: 12, unlocked: false, time: 0, rank: 0 },
{ group: 13, unlocked: false, time: 0, rank: 0 },
],
unlocked: true,
avgTime: 0,
rank: 0,
},
Apollo 查询是这样定义的:
const createUser = gql`
mutation(
$username: String!,
$email: String!,
$password: String!,
$time_created: String!,
$time_played: Int!,
$verified: Boolean!,
$type_user: Boolean!,
$userLevel: UserLevelInput!,
$ranks: RanksInput!,
$pvp: PvpInput!
){
createUser(
username: $username,
email: $email,
password: $password,
time_created: $time_created,
time_played: $time_played,
verified: $verified,
type_user: $type_user,
userLevel: $userLevel,
ranks: $ranks,
pvp: $pvp
){
username
email
password
}
}
`;
我的架构:
const userSchema = new Schema({
username: String,
email: String,
password: String,
time_created: Date,
time_played: Number,
verified: Boolean,
type_user: Boolean,
userLevel: {
lidUnlocked: Number,
gidUnlocked: Number,
},
ranks: {
level: [
{
level: Number,
avgTime: Number,
rank: Number,
group: [
{
group: Number,
time: Number,
rank: Number,
},
],
},
],
},
pvp: {
points: Number,
rank: Number,
},
});
我是如何提出请求的:
const handleSubmit = (e) => {
e.preventDefault();
addUser({
variables: {
username: input.username,
email: input.email,
password: input.password,
time_created: Date.now(),
time_played: 0,
verified: false,
type_user: false,
userLevel: {
lidUnlocked: 1,
gidUnlocked: 1
},
ranks: {
level: [{
level: 1,
avgTime: 0,
rank: 0,
group: [{
group: 1,
time: 0,
rank: 0
}]
}]
},
pvp: {
points: 0,
rank: 0,
}
}
})
}
UserLevelInput、RanksInput 和 PvpInput:
const UserLevelInputType = new GraphQLInputObjectType({
name: "UserLevelInput",
fields: () => ({
lidUnlocked: { type: GraphQLInt },
gidUnlocked: { type: GraphQLInt },
}),
});
const RanksInputType = new GraphQLInputObjectType({
name: "RanksInput",
fields: () => ({
level: { type: new GraphQLList(LevelInputType) },
}),
});
const LevelInputType = new GraphQLInputObjectType({
name: "LevelInput",
fields: () => ({
level: { type: GraphQLInt },
avgTime: { type: GraphQLInt },
rank: { type: GraphQLInt },
group: { type: new GraphQLList(GroupInputType) },
}),
});
const GroupInputType = new GraphQLInputObjectType({
name: "GroupInput",
fields: () => ({
group: { type: GraphQLInt },
time: { type: GraphQLInt },
rank: { type: GraphQLInt },
}),
});
const PvpInputType = new GraphQLInputObjectType({
name: "PvpInput",
fields: () => ({
points: { type: GraphQLInt },
rank: { type: GraphQLInt },
}),
});
如果我在 localhost:5005/graphql 上进行此更改,它会按预期工作:
mutation{
createUser(
username:"babadany2999",
email:"babadany2999@gmail.com",
password:"Immboold1",
time_created:"1645738406658",
time_played: 0,
verified: false,
type_user: false,
userLevel:{
lidUnlocked: 1,
gidUnlocked: 1
},
ranks: {
level: [{
level: 1,
avgTime: 0,
rank: 0,
group:[{
group: 1,
time: 0,
rank: 0
}]
}]
},
pvp: {
points: 0,
rank: 0
}
), {
username
email
password
}
}
此外,如果我发出请求(代码不在 /graphql 中),然后查看 Apollo Dev 工具以了解该特定突变,我会发现 Int、UserLevelInput、RanksInput 和 PpvInput 类型未知。 Apollo Dev Tools type unknown
对于遇到相同问题的任何人,我设法通过创建复杂对象的常量并简单地将默认值设置为猫鼬 table 中的那些常量而不将其作为输入提供给 apollo 来“修复”它.
username: String,
email: String,
password: String,
time_created: {
type: Date,
default: new Date()
},
time_played: {
type: Number,
default: 0
},
type_user: {
type: Boolean,
default: false
},
verified: {
type: Boolean,
default: false
},
userLevel: {
lidUnlocked: Number,
gidUnlocked: Number
},
ranks: {
type: Object,
default: ranks
},
pvp: {
points: {
type: Number,
default: 0
},
rank: Number
}
})
和常量的一部分(很长但是到最后都是一样的结构):
const ranks= {
level: [
{
level: 1,
group: [
{ group: 1, unlocked: true, time: 0, rank: 0 },
{ group: 2, unlocked: false, time: 0, rank: 0 },
{ group: 3, unlocked: false, time: 0, rank: 0 },
{ group: 4, unlocked: false, time: 0, rank: 0 },
{ group: 5, unlocked: false, time: 0, rank: 0 },
{ group: 6, unlocked: false, time: 0, rank: 0 },
{ group: 7, unlocked: false, time: 0, rank: 0 },
{ group: 8, unlocked: false, time: 0, rank: 0 },
{ group: 9, unlocked: false, time: 0, rank: 0 },
{ group: 10, unlocked: false, time: 0, rank: 0 },
{ group: 11, unlocked: false, time: 0, rank: 0 },
{ group: 12, unlocked: false, time: 0, rank: 0 },
{ group: 13, unlocked: false, time: 0, rank: 0 },
],
unlocked: true,
avgTime: 0,
rank: 0,
},