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

GraphQL handler: environment variables not working for endpoint for subscriptions/WebSocket calls #5296

Open
xmlking opened this issue Apr 4, 2023 · 6 comments

Comments

@xmlking
Copy link

xmlking commented Apr 4, 2023

environment variables not working with GraphQL Handler endpoint when invoking subscriptions. I have to give real URL.
I even tried to provide subscriptionsEndpoint via environment variables without any luck.
This is preventing us deploying to different environments without hardcoding endpoints...

Queries and Mutations works fine when setting GraphQL endpoint via environment variables.

  - name: mysub
    handler:
      graphql:
        # environment variables not working with graphql subscriptions endpoint
        # endpoint: '{env.MY_SEB_ENDPOINT}'
        endpoint: http://localhost:8080/graphql
        # subscriptionsEndpoint:  ws://localhost:8080/graphql

Error

/node_modules/.pnpm/ws@8.13.0/node_modules/ws/lib/websocket.js:675
      throw new SyntaxError(`Invalid URL: ${address}`);
            ^
SyntaxError: Invalid URL: {env.MY_SEB_ENDPOINT}
@chrisbrelski
Copy link

@xmlking
It worked for me when using the syntax :
subscriptionsEndpoint: "${MY_SERVICE}/graphql"
Instead of {env.MY_SERVICE}

@xmlking
Copy link
Author

xmlking commented Apr 13, 2023

Thanks for letting me know. Will they this solution.
I recall @ardatan mentioned someway disadvantage of using this way for accessing environment variables.

@jacoatvatfree
Copy link
Contributor

jacoatvatfree commented Feb 9, 2024

I have the same problem with the mongoose handler and the connectionString field. Either syntax just throws the following error:

MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
    at new ConnectionString (/workspaces/api/node_modules/mongodb-connection-string-url/src/index.ts:133:13)
    at parseOptions (/workspaces/api/node_modules/mongodb/src/connection_string.ts:245:15)
    at new MongoClient (/workspaces/api/node_modules/mongodb/src/mongo_client.ts:359:34)
    at NativeConnection.createClient (/workspaces/api/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:293:14)
    at NativeConnection.openUri (/workspaces/api/node_modules/mongoose/lib/connection.js:779:34)
    at connect (/workspaces/api/node_modules/mongoose/lib/mongoose.js:404:15)
    at MongooseHandler.getMeshSource (/workspaces/api/node_modules/@graphql-mesh/mongoose/cjs/index.js:40:36)
    at /workspaces/api/node_modules/@graphql-mesh/runtime/cjs/get-mesh.js:90:52
    at Array.map (<anonymous>)
    at getMesh (/workspaces/api/node_modules/@graphql-mesh/runtime/cjs/get-mesh.js:85:38)

@jacoatvatfree
Copy link
Contributor

Seems that environment variables are super limited in where they can be applied to config. I also tried adding statsd plugin with

plugins:
  - statsd:
      client:
        host: '{env.DD_AGENT_HOST}'

and getting the following error in the logs

Error: Error sending hot-shots message: Error: getaddrinfo ENOTFOUND {env.DD_AGENT_HOST}

@jacoatvatfree
Copy link
Contributor

jacoatvatfree commented Feb 10, 2024

I can hack a fix for the second problem (statsd plugin) in my case by doing a build, then doing a search and replace on the generated .mesh/index.ts file before starting.

package.json

"scripts": {
    "build": "mesh build && npx -y https://github.com/vatfree/env-replace.git .mesh/index.ts",
    "start": "yarn build && mesh start"
  }

but the mongoose host is required before the build and replacing the .meshrc.yaml file will alter the source code in a destructive way.

@jacoatvatfree
Copy link
Contributor

Turns out the above hack was unnecessary, since the underlying hot-shots package will try to use the DD_AGENT_HOST environment variable by default if no host was specified. So best to just not specify it like:

plugins:
  - statsd:
      # client:
      #   host: '{env.DD_AGENT_HOST}'

This was referenced Apr 30, 2024
This was referenced May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants