graphql / vscode-graphql

MIGRATED: VSCode GraphQL extension (autocompletion, go-to definition, syntax highlighting)
https://marketplace.visualstudio.com/items?itemName=Prisma.vscode-graphql
MIT License
557 stars 71 forks source link

env vars don't not work properly #257

Closed mdreizin closed 2 years ago

mdreizin commented 3 years ago

Actual Behavior

Env vars does not work properly even if they are valid.

Expected Behavior

Should be working fine.

Steps to Reproduce the Problem Or Description

  1. Create .env file
  2. Set GRAPHQL_TOKEN=xxx with a valid value
  3. Create graphql.config.js with the content:
console.log('GRAPHQL_TOKEN', process.env.GRAPHQL_TOKEN) // `GRAPHQL_TOKEN` is set and value is `xxx`

module.exports = {
  projects: {
    'default': {
      name: "GraphQL API",
      schema: [
        "./graphql/schema.graphql",
        "./graphql/**/*.graphql"
      ],
      extensions: {
        endpoints: {
          dev: {
            url: "http://localhost:8080/graphql",
            headers: {
              Authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`
            },
            introspect: false
          }
        }
      }
    }
  }
}
  1. Call any queries/mutations:
ServerParseError: Unexpected token U in JSON at position 0

If I set the token value manually then it will work:

-Authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`
+Authorization: `Bearer xxx`

The following config doesn't work as well:

-Authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`
+Authorization: "Bearer ${GRAPHQL_TOKEN}"

Specifications

Logs Of TS Server || GraphQL Language Service

GRAPHQL_TOKEN xxx
11/20/2020, 4:27:41 PM [4] (pid: 63777) graphql-language-service-usage-logs: {"type":"usage","messageType":"textDocument/didChange","projectName":"default","fileName":"file:///<redacted>/graphql.config.js"}
11/20/2020, 4:28:37 PM [4] (pid: 63777) graphql-language-service-usage-logs: {"type":"usage","messageType":"textDocument/didOpen","projectName":"default","fileName":"file:///<redacted>/graphql/scratches/scratch.graphql"}

undefined
NetworkHelper: operation: query
NetworkHelper: endpoint: http://localhost:8080/graphql
aylton-almeida commented 3 years ago

Is there any update on this? I've been trying to do this with a project and it keeps throwing me a similar Error message

jkcorrea commented 3 years ago

@mdreizin @aylton-almeida this was bugging me too, submitted a fix in #267 but if you want a temp fix you can patch it:

open the installed extension's extension.js file (somewhere like ~/.vscode/extensions/graphql.vscode-graphql-0.3.13/out/extension.js and add this patch around line 30 (don't think it really matters where, as long as it's in that fn):

        const config = getConfig();
        const { debug } = config;
        if (debug) {
            console.log('Extension "vscode-graphql" is now active!');
        }
+       if (vscode_1.workspace.workspaceFolders) {
+         process.chdir(vscode_1.workspace.workspaceFolders[0].uri.fsPath);
+       }
mdreizin commented 3 years ago

@jkcorrea Cool! Thanks!

acao commented 2 years ago

the solution is to import dotenv in your .ts or .js config file!