Skip to content

Lua exception when json encoding graphql request body #470

@GusJelly

Description

@GusJelly

Prerequisites

  • I am using the latest stable release of Neovim
  • I am using the latest version of the plugin

Neovim Version

v0.10.1

Operating system/version

Fedora 40

Actual behavior

When trying to run a graphql request body that does not have variables, I get a
lua error that happens in the parser.parse_body method. This happens because
variables_text is null since there are no variables to be found by
treesitter.

To fix the issue I did this in my fork:

body.__TYPE = "graphql"
local query_text = vim.treesitter.get_node_text(assert(body_node:named_child(0)), source)
query_text = expand_variables(query_text, context)
local variables_text
local variables_node = body_node:named_child(1)
-- if variables_node does not exist, encode only the query_text to prevent null
-- exceptions
if variables_node then
    variables_text = vim.treesitter.get_node_text(variables_node, source)
    variables_text = expand_variables(variables_text, context)
    body.data = vim.json.encode({
        query = query_text,
        variables = vim.json.decode(variables_text),
    })
else
    body.data = vim.json.encode({
        query = query_text
    })
end
logger.debug(body.data)

After this dirty fix, I have issues where the json encoding does not work
properly as far as I can tell.

When I run a request like this:

GRAPHQL http://localhost:5005/graphql

query {
  slabs {
    nodes {
      code
    }
  }
}

The query_text looks like this:

"query {\n  slabs {\n    nodes {\n      code\n    }\n  }\n}"

and the body.data looks like this:

'{"query":"query {\\n  slabs {\\n    nodes {\\n      code\\n    }\\n  }\\n}"}'

I wanted to ask for some help on fixing this issue if possible. Should I make a
pr and work on it there?

My code is on this fork: [github.com/GusJelly/rest.nvim]

Expected behavior

expected the query to be turned into json that a graphql server can understand, something like this:

{"query": {<query>}}

Steps to reproduce

  1. Create a simple graphql request file:
GRAPHQL http://localhost:5005/graphql

query {
  slabs {
    nodes {
      code
    }
  }
}
  1. Try to run the request with Rest run.

Other information

No response

Repro (lazy.nvim)

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
    spec = {
        "rest-nvim/rest.nvim",
        {
            "nvim-treesitter/nvim-treesitter",
            build = ":TSUpdate",
            main = "nvim-treesitter.configs",
            opts = {
                ensure_installed = { "http" },
                sync_install = false,
                highlight = { enable = true },
                indent = { enable = true },
            },
        },
    },
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions