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

How to invoke javascript code from handler/odata/schemaHeaders?

Open cmcconomyfwig opened this issue 2 years ago • 3 comments

Following advice I saw on another issue here, I attemped to use the following configuration:

sources:
  - name: Microsoft Graph
    handler:
      odata:
        endpoint: https://MYURL.crm.dynamics.com/api/data/v9.2/
        batch: multipart
        expandNavProps: true
        operationHeaders: ./introspection-headers.js
        schemaHeaders: ./introspection-headers.js

with ./introspection-headers.js having,

const msal = require('@azure/msal-node');

// async function get_dynamics_token() {
exports.default = async () =>  {
    const authConfig = {
      auth: {
        clientId: 'CLIENT',
        authority: 'https://login.windows.net/MYGUID',
        clientSecret: 'CLIENTSECRET',
      }
    };
  
    const cca = new msal.ConfidentialClientApplication(authConfig);
    const ccRequest = {scopes: ['https://MYURL.api.crm.dynamics.com/.default']};
    const res = await cca.acquireTokenByClientCredential(ccRequest);
    return { "Authorization": `Bearer ${res.accessToken}` }
   }

However, this config yields the following issue:

⚠️ 🕸️  Mesh - config Configuration file is not valid!
⚠️ 🕸️  Mesh - config This is just a warning! It doesn't have any effects on runtime.
⚠️ 🕸️  Mesh - config Error: must be object

This error goes away if I provide a static Authorization directly (but of course I need to update this dynamically!)

So it appears we cannot yet invoke javascript to return dynamic output for the operation and schema headers, unless I missed something.

How can I set myself up to dynamically invoke javascript when I pull the schema and when I query?

(Added context - here is my package.json)

{
  "name": "graphql-mesh",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@azure/msal-node": "^1.15.0",
    "@graphql-mesh/cli": "^0.82.23",
    "@graphql-mesh/odata": "^0.22.13",
    "graphql": "^16.6.0"
  }
}

cmcconomyfwig avatar Feb 21 '23 19:02 cmcconomyfwig