1
votes

I am currently using apollo to make a transfer (adding a user) on my project and when the response my object data is empty. Do you know why ?

this is my schema type

  type UserKyrios {
    _id: ObjectId!
    firstname: String!
    lastname: String!
    email: String!
    password: String!
    creationDate: Date
  }
`;

and this is my input type

  input UserKyriosInput {
    firstname: String!
    lastname: String!
    email: String!
    password: String!
  }
`;

Definition type Mutation

  type Mutation {
    upsertUserKyrios(user: UserKyriosInput!): UserKyrios
  }

My querie mutation

mutation UpsertUserKyrios($user: UserKyriosInput!) {
    upsertUserKyrios(user: $user) {
        ...userKyriosFields
        creationDate
    }
  }

My output

data: {upsertUserKyrios: null}
upsertUserKyrios: null

My resolver

upsertUserKyrios: async (_, __, { server }) => server.plugins.mongodb.UserKyrios.insertOne(),

My script component

<script>
import { UPSERT_USER_KYRIOS } from '../../../../graphql/KyriosMutations';

export default {
  name: 'addUserModalComponent',
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
  },
  data: () => ({
    user: {
      firstname: '',
      lastname: '',
      email: '',
      password: '',
    },
  }),
  methods: {
    closeDialog() {
      this.$emit('closeModal');
    },
    async submitForm() {
      const userCreated = await this.$apollo.mutate({
        mutation: UPSERT_USER_KYRIOS,
        variables: {
          user: this.user,
        },
      });
      console.log(userCreated);
    },
  },
};
</script>
3

3 Answers

0
votes

Your resolver seems to be missing info. Your second argument in the resolver doesn’t seem to be handling any incoming data from the fields. Also, once those arguments come in, maybe I don’t know enough about Mongo, but shouldn’t you be sending the data to be inserted as an object in the insertOne function?

We also should see what your gql definition looks like.

0
votes

Resolvers gives you some information about the reqoust:

1st param: Parent

2nd param: args => contains the request payload

3d param: context

4th param: info

Mutation data exist in the second param that you dont use (args). args will contain an object. In your case user with the values from the client request. ($variables).

upsertUserKyrios: async (_, args, { server }) =>

In the client side:

when you submit the form send all the necessary info in the veriables object (UserKyriosInput)

variables: {
  user: {
    firstname: this.user.firstName
    lastname: this.user.lastName
    email: this.user.email
    password: this.user.password
  }
}

or

variables: {
  user: {
    ...this.user
  }
}

Hope make sence.

0
votes

When an error happens, the data returns empty. You need to look for the complete response. And error is probably present there.

Not sure where is the this.$apollo.mutate comming from, but the mutate usually returns a promise.

One good way to use them is with hooks: https://www.apollographql.com/docs/react/essentials/mutations/