Home > OS >  GraphiQL Mutation: How can I ignore other objects in query variables
GraphiQL Mutation: How can I ignore other objects in query variables

Time:11-14

Currently having hard time setting up my end points clerk to hasura.

I am absolute new to this platform specially at GraphiQL and just following documentations and youtube video

What I am trying to do is import/insert specific data i neeed only from clerk. Here's the sample query variables:

{
  "data": {
    "birthday": "",
    "created_at": 1654012591514,
    "email_addresses": [
      {
        "email_address": "[email protected]",
        "id": "idn_29w83yL7CwVlJXylYLxcslromF1",
        "linked_to": [],
        "object": "email_address",
        "verification": {
          "status": "verified",
          "strategy": "ticket"
        }
      }
    ],
    "external_accounts": [],
    "external_id": "567772",
    "first_name": "Example",
    "gender": "",
    "id": "user_29w83sxmDNGwOuEthce5gg56FcC",
    "last_name": "Example",
    "last_sign_in_at": 1654012591514,
    "object": "user",
    "password_enabled": true,
    "phone_numbers": [],
    "primary_email_address_id": "idn_29w83yL7CwVlJXylYLxcslromF1",
    "primary_phone_number_id": null,
    "primary_web3_wallet_id": null,
    "private_metadata": {},
    "profile_image_url": "https://www.gravatar.com/avatar?d=mp",
    "public_metadata": {},
    "two_factor_enabled": false,
    "unsafe_metadata": {},
    "updated_at": 1654012591835,
    "username": null,
    "web3_wallets": []
  },
  "object": "event",
  "type": "user.created"
}

What I only need to this object is content inside of the "data" is: created_at, first_name, user_id, updated_at, profile_image_url

The GraphiQL Query I did is:

mutation CreateUser(
    $created_at: String,
    $first_name: String,
    $user_id: String,
        $updated_at: String,
    $profile_image_url: String
    )
    {
  insert_users_one(object: 
    {
      created_at: $created_at, 
      first_name: $first_name, 
      user_id: $user_id, 
      updated_at: $updated_at, 
      profile_image_url: $profile_image_url, 
    }) {
    created_at
    first_name
    user_id
    updated_at
    profile_image_url
  }
}

Which throwing error of:

{
  "errors": [
    {
      "extensions": {
        "code": "validation-failed",
        "path": "$"
      },
      "message": "unexpected variables in variableValues: object, type, data"
    }
  ]
}

I tried using other method like this:

mutation CreateUser($data: users_insert_input!) {
  insert_users_one(object: $data) {
    created_at
    first_name
    user_id
    updated_at
    profile_image_url
  }
}

But it is still having error because of object and type fields

{
  "errors": [
    {
      "extensions": {
        "code": "validation-failed",
        "path": "$"
      },
      "message": "unexpected variables in variableValues: object, type"
    }
  ]
}

Here's a sample of GraphQL type:

//is this how you break things down?
type Mutation {
  data(
    created_at: Int
    first_name: String
    id: String
    updated_at: Int
    profile_image_url: String
  ): Data
}
//this is what i will send in the database, things that I only need

type Verification {
  status: String
  strategy: String
}

type EmailAddresses {
  email_address: String
  id: String
  object: String
  verification: Verification
  linked_to: [String]
}

type Data {
  birthday: String
  created_at: Int
  external_id: String
  first_name: String
  gender: String
  id: String
  last_name: String
  last_sign_in_at: Int
  object: String
  password_enabled: Boolean
  primary_email_address_id: String
  primary_phone_number_id: String
  primary_web3_wallet_id: String
  profile_image_url: String
  two_factor_enabled: Boolean
  updated_at: Int
  username: String
  web3_wallets: [String]
  phone_numbers: [String]
  external_accounts: [String]
  email_addresses: [EmailAddresses]
}

type AutogeneratedMainType {
  object: String
  type: String
  data: Data
}

I was expecting based on documents, It will ignore aren't included data.

Visit Github Discussions here

CodePudding user response:

Context about the error

This is error you are receiving is based on this graphql spec - https://spec.graphql.org/June2018/#sec-Input-Objects . More over there is also a different spec for validation against variables here - https://spec.graphql.org/June2018/#sec-All-Variables-Used

TLDR; Using variable which isn’t defined in operation, will result into “unexpected variableValues” error. In your case apart from data , you have type and object as variables in your query variables object which is not defined in operation itself. Remember that query variables is an “object” expecting the variable key-values in it.

Workaround

Cleanest way to do this is to sanitize your object (which you will pass in query variables) by either creating a new object from it and passing data to it or either you remove the unnecessary fields from it which are not defined in operation. You could just delete the properties of that object. Consider yourObject containing data,type and object fields. Then you can do delete yourObject.type and delete yourObject.object. And then pass it.

This workaround is intended for client side code. But there's no exception for graphiQL explorer as that error would be thrown upon undefined variables in operation. If trying via graphiQL explorer, you would manually need to not pass those variables in query variables scope.

Conclusion

This behavior is in compliant with this graphql spec and not with Hasura directly, so we would suggest you to go through those graphql spec links and understand the aspect of it.

CodePudding user response:

Based on the error message I suggest removing the object: label from your mutation:

insert_users_one(
  created_at: $created_at, 
  first_name: $first_name, 
  user_id: $user_id, 
  updated_at: $updated_at, 
  profile_image_url: $profile_image_url, 
) {
  insert_users_one(
    created_at: $created_at, 
    first_name: $first_name, 
    user_id: $user_id, 
    updated_at: $updated_at, 
    profile_image_url: $profile_image_url, 
   ) {
    created_at
    first_name
    user_id
    updated_at
    profile_image_url
  }
}

However it's not clear what the insert_users_one mutation is expecting in terms of input structure. I recommend introspecting your schema and looking at that mutation to verify what it wants.

  • Related