Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection variable not interpolated in GraphQL vars #2960

Closed
2 tasks done
rbideau opened this issue Aug 29, 2024 · 11 comments
Closed
2 tasks done

Collection variable not interpolated in GraphQL vars #2960

rbideau opened this issue Aug 29, 2024 · 11 comments
Labels
bug Something isn't working

Comments

@rbideau
Copy link
Contributor

rbideau commented Aug 29, 2024

I have checked the following:

  • I use the newest version of bruno.
  • I've searched existing issues and found nothing related to my issue.

Describe the bug

On version 1.27.0, collection variable are not interpolated in GraphQL Vars when executing the request via the app.

Might be related to

.bru file to reproduce the bug

meta {
  name: spacex
  type: graphql
  seq: 1
}

post {
  url: https://spacex-production.up.railway.app/
  body: graphql
  auth: none
}

body:graphql {
  {
    capsules {
      id
    }
  }
  
}

vars:post-response {
  id: res.body.data.capsules[0].id
}

assert {
  res.status: eq 200
}
meta {
  name: spacex var
  type: graphql
  seq: 2
}

post {
  url: https://spacex-production.up.railway.app/
  body: graphql
  auth: none
}

body:graphql {
  query ($id: ID!) {
    capsule(id: $id) {
      id
      landings
    }
  }
  
}

body:graphql:vars {
  {"id": "{{id}}"}
}

assert {
  res.status: eq 200
}

Screenshots/Live demo link

Screencast.from.29-08-2024.08.35.56.webm
@rbideau rbideau added the bug Something isn't working label Aug 29, 2024
@mkurapov
Copy link

mkurapov commented Aug 29, 2024

Not sure if this is something the Bruno team will be looking to add in the future, but in the meantime @rbideau, we've been using this in pre-request scripts:

  resolveTemplateVariables: function (string) {
    const VARIABLE_NAME_REGEX = /{{([A-Za-z]\w+)}}/g

    return string.replace(
      VARIABLE_NAME_REGEX,
      (_, key) => bru.getVar(key) || bru.getEnvVar(key)
    )
  }

@mkurapov
Copy link

Ah, never mind, I see this is a net-new/separate issue. I think we needed to manually interpolate the string in the query, but never in the input variables

@That-1-QA
Copy link

That-1-QA commented Sep 10, 2024

I also have this issue. No matter what I try, I cannot use Bruno environment variables, or any other variables within the graphql variables. They just are not replaced before bruno sends the request.

As a workaround, I'm able to use bru variables within the graphql variables by using a standard HTTP request, JSON body, with something like:

{
  "query": "mutation ($data: [JSON!]!) {\n dataUpload(input: { data: $data  }) { saved { type subscriptions } errors {   response {   id   }    error  }}}",
  "variables": "{ \"data\": \n    [\n{\n    \"id\": \"{{dataID}}\",\n \"version\": \"1-0-0\",\n    \"type\": \"text\",\n    \"source\": \"{{source}}\",\n    \"createdTimeStamp\": 1718641627,\n    \"modifiedTimeStamp\": 1718641627,\n    \"text\": \"This is my testing text!\"\n}]}"
}

In the above example, the {{source}} and {{dataID}} variables are set by Bruno within a pre-request script

const { v4: uuidv4 } = require('uuid'); 
const dataID = uuidv4();
const source = uuidv4();

bru.setVar("dataID", dataID);
bru.setVar("source", source);

@Nightbr
Copy link

Nightbr commented Sep 11, 2024

Still on v1.28.0

Also it was working before since we have many request that uses it.

Happy to help debugging or giving some tests example if necessary.

@bstupid
Copy link

bstupid commented Sep 12, 2024

This is a regression and worked fine in 1.24 I had to go back to earlier build

@That-1-QA That-1-QA mentioned this issue Sep 17, 2024
2 tasks
@cmuench
Copy link

cmuench commented Sep 18, 2024

I was able to get it working by using the script of @mkurapov in a collection pre-script a bit changed:

function resolveTemplateVariables(string) {
  const VARIABLE_NAME_REGEX = /{{([A-Za-z_]\w+)}}/g;
  
  return string.replace(
    VARIABLE_NAME_REGEX,
    (_, key) => bru.getVar(key) || bru.getEnvVar(key)
  );
}

let body = req.getBody();

if (!body.variables) {
  return;
}

let resolvedVariables = resolveTemplateVariables(body.variables);
body.variables = resolvedVariables;
//console.log(body);

Works well with my Magento 2 project.

@V-ed
Copy link

V-ed commented Sep 19, 2024

I did something similar but kept copying it in every request, thank you @cmuench for making me realize that indeed the collections scripts exists!

Although your script is fairly complete, there's two things I changed in my version :

// Bruno apparently does not support variables substitution in GQL queries yet :)
// Doing this temporarily until this is fixed.
// https://github.com/usebruno/bruno/issues/2960

let body = req.getBody();

if (!body?.variables) {
  return;
}

function resolveTemplateVariables(string) {
  const VARIABLE_NAME_REGEX = /{{(\w+)}}/g;
  
  return string.replace(
    VARIABLE_NAME_REGEX,
    (_, key) => bru.getVar(key) || bru.getEnvVar(key)
  );
}

const resolvedVariables = resolveTemplateVariables(body.variables);

body.variables = resolvedVariables;
  1. The variable body could be undefined in regular queries / when there is no Body, so adding a shortcircuit helps prevent weird TypeError: Cannot read properties of undefined (reading 'variables') errors in non-graphql requests
  2. Tweaked the VARIABLE_NAME_REGEX, as the only important bits of the regex is the \w. I know this was copied from @mkurapov's comment, no diss to either of you, it just makes the intent of this regex clearer (unless you REALLY wanted your variables to start by a letter)!

Hopefully this can get fixed so we don't need this workaround (especially since it seems like it was a regression, having hover info of the variable value in the GQL variables view would be super helpful!).

@mjhcorporate
Copy link
Contributor

mjhcorporate commented Sep 20, 2024

The interpolation is only broken in the UI -- it works on the command line. However, the pre-request script breaks the CLI usage, because on the CLI, body.variables is an object!

Revised pre-request script checks for this:

// Bruno UI has a regression w.r.t. environment variables in GraphQL variables.
// Doing a manual replacement until the issue is fixed.
// https://github.com/usebruno/bruno/issues/2960

let body = req.getBody();

// skip if the variables are not defined or if the CLI runner is used (-> typeof variables is "object" there)
if (!body?.variables || (typeof body.variables === 'object')) {
  return;
}

function resolveTemplateVariables(string) {
  const VARIABLE_NAME_REGEX = /{{(\w+)}}/g;
  return string.replace(
    VARIABLE_NAME_REGEX,
    (_, key) => bru.getVar(key) || bru.getEnvVar(key)
  );
}

const resolvedVariables = resolveTemplateVariables(body.variables);

body.variables = resolvedVariables;

@srpalwaidynamite
Copy link

Hopefully they fix this issue in new version

@reggermont
Copy link
Contributor

reggermont commented Sep 25, 2024

Confirm this is the same bug as #352

This could fix the issue, merged on September 23: #3142, so should be good on 1.30.2 / 1.31.0

Reverting to 1.26.2 until fixed

EDIT: Tried to build current version and still doesn't work
@helloanoop FYI

@rbideau
Copy link
Contributor Author

rbideau commented Oct 2, 2024

I can no longer reproduce it on 1.32.0

Looks like it was fixed by #3212

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants