2

I am trying to save a user id to a new biz. I keep getting a 400 error and can not figure out why. I am using django for the backend with graphql and apollo client for the front with vue js. On my request the user ID is sent but for some reason throws a 400 bad request error.

Create Biz Mutation Apollo

export const CREATE_BIZ_MUTATION = gql`

    mutation CreateBizMutation($name: String!, $owner: ID!) {
      createBiz(name: $name, ownerId: $owner) {
        name

      }
    }`

Create Biz mutation Django

class CreateBiz(graphene.Mutation):
    id = graphene.Int()
    name = graphene.String()
    code = graphene.String()
    owner = graphene.Field(UserType)


    class Arguments:
        name = graphene.String()


    def mutate(self, info, name):
        user = get_user(info) or None
        code = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(6))
        biz = Biz(
            code = code,
            name = name,
            owner = user
        )

        biz.save()

        return CreateBiz(
            id= biz.id,
            name = biz.name,
            code = biz.code,
            owner = biz.owner
        )

Create Biz Component

createBiz () {
        const owner = localStorage.getItem(DJANGO_USER_ID)
        if (!owner) {
            console.error('No user logged in')
            return
        }
        const { name } = this.$data
        this.$apollo.mutate({
            mutation: CREATE_BIZ_MUTATION,
            variables: {
                name,
                owner
            }
        }).catch((error) => {
                    console.log(error)
                    })
        }
    }
Taylor
  • 1,223
  • 1
  • 15
  • 30
  • This sounds like a csrf issue. Let me know if you still have this error and I'll provide more detail. – morinx Mar 02 '18 at 17:31

0 Answers0