为什么我的 GraphQL/Apollo 突变失败?

发布于 2025-01-09 18:35:48 字数 4183 浏览 0 评论 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:"[email protected]",
    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 开发工具类型未知

This is how the Apollo query is defined:

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
        }
    }
`;

My schema:

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,
  },
});

How I'm making the request:

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 and 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 },
  }),
});

If i make this mutation on localhost:5005/graphql it works as intended:

mutation{
  createUser(
    username:"babadany2999",
    email:"[email protected]",
    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
  }
}

Also if I make the request(with the code not in /graphql) and then check out Apollo Dev tools for that particular mutation, I get that the Int, UserLevelInput, RanksInput and PpvInput types are not known.
Apollo Dev Tools type unknown

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2025-01-16 18:35:48

对于遇到同样问题的任何人,我设法通过创建复杂对象的常量并简单地将默认值设置为猫鼬表中的这些常量而不将其作为阿波罗的输入来“修复”它。

    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,
        },

For anyone encountering the same problem, I managed to "fix" it by creating constants of the complex objects and simply setting the default to those constants in the mongoose table and not giving that as input to 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
    }

})

And part of the constant(it's very long but it has the same structure until the end):


    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,
        },

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文