简体   繁体   中英

Why does my GraphQL/Apollo mutation fail?

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:"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
  }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM