graphql-mesh icon indicating copy to clipboard operation
graphql-mesh copied to clipboard

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

Open xmlking opened this issue 1 year ago • 6 comments

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/[email protected]/node_modules/ws/lib/websocket.js:675
      throw new SyntaxError(`Invalid URL: ${address}`);
            ^
SyntaxError: Invalid URL: {env.MY_SEB_ENDPOINT}

xmlking avatar Apr 04 '23 22:04 xmlking

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

chrisbrelski avatar Apr 12 '23 15:04 chrisbrelski

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

xmlking avatar Apr 13 '23 01:04 xmlking

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 avatar Feb 09 '24 06:02 jacoatvatfree

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 avatar Feb 10 '24 06:02 jacoatvatfree

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 avatar Feb 10 '24 07:02 jacoatvatfree

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}'

jacoatvatfree avatar Feb 12 '24 10:02 jacoatvatfree