ardatan / graphql-mesh

🕸️ GraphQL Federation Framework for any API services such as REST, OpenAPI, Swagger, SOAP, gRPC and more...
https://the-guild.dev/graphql/mesh
MIT License
3.28k stars 342 forks source link

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

Open xmlking opened 1 year ago

xmlking commented 1 year ago

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 commented 1 year ago

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

xmlking commented 1 year ago

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

jacoatvatfree commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 8 months ago

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