How to parse enum in JavaScript - Expo React Native - @apollo/client

Hi community :slight_smile:
I am having an issue where I need to send an enum in a mutation request, but I am not being successful in finding the right way to do so. Can someone help me with it? Thank you!

schema.graphql

enum OnboardingStage {
    TRACKING_SETTING
}

mutations.js

import { gql } from "@apollo/client";

export const resolvers = {
  OnboardingStage: {
    TRACKING_SETTING: "TRACKING_SETTING"  },
};

export const SIGNUP_USER_TRACKING = gql`
  mutation SignupPatientSubmitAllowedTrackingSetting(
    $signupTrackingSetting: PatientSignupTrackingSetting!
  ) {
    signupPatientSubmitAllowedTrackingSetting(
      signupTrackingSetting: $signupTrackingSetting
    ) {
      allowTracking
      onboardingStage
    }
  }
`;

App.js


import { useMutation } from "@apollo/client";

const [alert, setAlert] = useState(true);

  const [signupPatientSubmitAllowedTrackingSetting, { data, loading, error }] =
    useMutation(SIGNUP_USER_TRACKING, {
      errorPolicy: "all",
      onCompleted(data) {
        console.log(data);
      },
    });

const handleTracking = async (answer) => {
    try {
      await signupPatientSubmitAllowedTrackingSetting({
        variables: {
          signupTrackingSetting: {
            trackingSetting: {
              allowTracking: answer,
            },
//here is supposed to be an enum not a string
            onboardingStage: "TRACKING_SETTING",
          },
        },
      });
      setAlert(false);
    } catch (e) {
      console.log(e);
    }
  };```


log:

Submission error! ApolloError: Response not successful: Received status code 400 

network response:


{"errors":[{"message":"Unsupported feature (@line:column 3:5):The argument '$signupTrackingSetting' is a variable value. Variables are not currently supported here.\nGraphQL operation that failed: SignupPatientSubmitAllowedTrackingSetting","locations":[{"line":0,"column":0}]}]}

This seems to be a server-specific issue here - what server are you using?

hi Lenz,

we are working on the server issue at the moment. Do you have any idea about the enum parsing problem? I am kind stuck on that and I can’t send a string to the backend :confused:
Thank you for your help

You haven’t really shown anything to indicate that you cannot just pass in the string - are you getting any kind of error message that you aren’t sharing?

hi,

Yes, I definitely need to send an enum, not a string. I’ve already tested it with Postman and passing a string returns an error(see log bellow) and when I pass a enum the request is successful. I already know, that is currently passing a string at the payload, so I would like to fix it.

Apollo client request payload:

signupTrackingSetting: {trackingSetting: {allowTracking: true}, onboardingStage: "TRACKING_SETTING"}

request with Postman

mutation SignupPatientSubmitAllowedTrackingSetting {
    signupPatientSubmitAllowedTrackingSetting(
        signupTrackingSetting: {trackingSetting: {allowTracking: true}, onboardingStage: "TRACKING_SETTING"}
    ) {
        id
    }
}

, the response is:
"message": "Error: error serializing parameter 1: cannot convert between the Rust typealloc::string::Stringand the Postgres typeOnboardingStage",

That still seems like a problem on your server side - between the Rust server and the Postgres server.
If this was incorrect on the client side, the Rust server would (assuming it validates incoming requests against the schema) already reject the query. (Try this by just returning something like “ASDqweasd”)

On the JavaScript side, the concept of an “enum” doesn’t exist - also not on the transport level of the JSON format that is sent to the server. It is sent to the server as a string, and the server’s job to identify it as an enum value.

1 Like