当前位置: 首页 > 知识库问答 >
问题:

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

经国安
2023-03-14

阿波罗查询是这样定义的:

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开发工具的特定突变,我得到的Int,UserLevelIn的,RanksIn的和PpvIn的类型是未知的。Apollo开发工具类型未知

共有1个答案

穆博简
2023-03-14

对于任何遇到相同问题的人,我都设法通过创建复杂对象的常量并简单地将默认值设置为mongose表中的常量而不将其作为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,
        },

 类似资料:
  • 我的根组件已经包装了ApolloProvider标签,但错误消息告诉我不是。 错误信息 问题是我的根组件已经包装了一个ApolloProvider标签 进口报表 与GraphQL的连接 测试查询 默认应用 这就是出错的地方 const { 数据, 加载 } = useQuery(USER_QUERY) 是回溯显示的行

  • 我刚刚开始在Apollo和Vue中使用GraphQL,所以这可能是一个“愚蠢”的问题,但我不知道怎么做。 如何在同一个视图中执行查询以获取一个对象,并使用变异更新它 假设我有一个简单的模式 和vue组件 现在我应该如何编写模板部分?我可以将产品对象链接到输入中的v模型吗? 谢谢你的帮助。

  • 所有和任何建议将非常感谢!谢谢你! 发布http://localhost:3000/GraphQL 400(错误请求)[GraphQL错误]:消息:变量“$email”未定义。,位置:[object object],[object object],路径:未定义 [GraphQL错误]:消息:变量“$LocationCity”未定义。,位置:[object object],[object objec

  • 我有一个关于GraphQL模式拼接的问题。我有两个Graphql模式: 和 我现在尝试使用graphql工具的函数合并模式: 但不是我想要实现的(扩展用户类型): 结果是:type Name{firstname:String!lastname:String!} 在最终架构中只显示一个UserType。我尝试使用中的API来扩展Type,但没有得到任何结果。 有没有办法通过扩展冲突类型来合并模式?

  • 问题内容: 我正在尝试使我的元素保持原状(过渡之后)。现在,翻译后的位置是我想要的位置,但随后我的名字又回到了报价单上。我是否缺少一段代码,或者是否有一段代码使这种回弹发生? 问题答案: 原因和解决方法: CSS转换通常不能应用于具有设置的元素。虽然奇怪的是,这种情况似乎最初发生在回弹之前,但解决方案是将设置更改为下面的代码段所示。 如果元素不可变形,为什么最初会对其进行翻译? 浏览器(至少是Ch

  • 在后端,我进行了调试,并收到了请求,它根据客户机上设置的令牌查找用户,但随后什么也不做,只在我试图从应用程序发送时返回给我400,在graphiql浏览器上。 我错过了什么?非常感谢。