GraphQL mutation permission error: 'Error: Mutation failed due to permission errors:'

Viewed 355

Problem

I am working on a next.js app with graphql, and I am trying to create content from my app.

When I run my mutation query I get this:

Error: Mutation failed due to permission errors: {"response":{"errors":[{"message":"Mutation failed due to permission errors","extensions":{"code":"403","failedActions":[{"action":"create","model":"NousContacter","stage":"DRAFT"}]}}],"data":null,"extensions":{"requestId":"cl47gsed70qm30bt92jr1df04"},"status":403,"headers":{"map":{"cache-control":"private, no-store","content-type":"application/json"}}},"request":{"query":"\n\tmutation CreateNousContacter($nom: String!, $email: String!, $message: String! ) {\n\t\tcreateNousContacter(data: {nom: $nom, email: $email, message: $message}){id}}","variables":{"nom":"TESTname","email":"TESTmail","message":"TESTmessage"}}}

My get queries are all working without any problem, but it's the first mutation I write and I don't know how to get it to work.

My request

import { request, gql } from 'graphql-request';
const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINT;

export const nousContacter = async ({ nom, email, message }) => {
    const query = gql`
        mutation CreateNousContacter($nom: String!, $email: String!, $message: String!){
            createNousContacter(data: {nom: $nom, email: $email, message: $message}) {
              id
            }
          }`;
        await request(graphqlAPI, query, {nom, email, message});
};

On the graphics side, all the fields of my model are read/write

BONUS QUESTIONS

1- Whenever I get my mutation to work, how can I create my content and put it in directly in the published stage? I haven't managed to do it in the API playground of graphics

2- Can I do what I want to do, with my field on read only?

3 Answers

This message "Mutation failed due to permission errors" It means that you gave permission to write content, but right after writing, you are trying to read the return, but the return you are trying to read is likely to be read as PUBLISHED.

Give your Model another permission to read as DRAFT

You need to generate a permenant token from graphcms which you will add to .env and call it in the header like below:

import { GraphQLClient, gql } from 'graphql-request';
const graphqlAPI = process.env.NEXT_PUBLIC_GRAPHCMS_ENDPOINT;
const graphcmsToken = process.env.GRAPHCMS_TOKEN

export const nousContacter = async ({ nom, email, message }) => {

  const graphQLClient = new GraphQLClient(graphqlAPI, {
    headers: {
      authorization: `Bearer ${graphcmsToken}`
    }
  })

    const query = gql`
        mutation CreateNousContacter($nom: String!, $email: String!, $message: String!){
            createNousContacter(data: {nom: $nom, email: $email, message: $message}) {
              id
            }
          }`;
        await GraphQLClient.request(query, {nom, email, message});
};

You have to give all permission to your specific Token:

setting -> Permanent Auth Tokens -> edit the Token and give it all permission.

Related