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

logger access #59

Open
Falven opened this issue Oct 5, 2023 · 1 comment
Open

logger access #59

Falven opened this issue Oct 5, 2023 · 1 comment

Comments

@Falven
Copy link

Falven commented Oct 5, 2023

Apollo server has a logger option that you can set to determine the logger it should use.
Azure Functions provide the logger via the context object such that your logger logs to App Insights.
However, this integration needs you to initialize and pass in the server object before you can access the context.
Is there any way to set Apollo's logger to the logger from Azure context?

const server = new ApolloServer<MyApiContext>({
  typeDefs,
  resolvers,
  formatError: (formattedError: GraphQLFormattedError): GraphQLFormattedError => {
    context.log.error({
      error: formattedError.message,
      locations: formattedError.locations,
      path: formattedError.path,
      extensions: formattedError.extensions,
    });
    return formattedError;
  },
  includeStacktraceInErrorResponses: NODE_ENV === 'development',
  logger: ???
});

const azureFunction = startServerAndCreateHandler(server, {
  context: async ({ req, context: { log } }): Promise<MyApiContext> => {
  ...
  },
});
@Falven
Copy link
Author

Falven commented Oct 5, 2023

It's ugly, but this is what I am currently doing.

import { GraphQLFormattedError } from 'graphql';
import { ApolloServer } from '@apollo/server';
import type { Logger } from '@azure/functions';
import { startServerAndCreateHandler } from '@as-integrations/azure-functions';

type IntegratedLogger = Console & Logger;

let integratedLogger: IntegratedLogger = console as IntegratedLogger;

const createIntegratedLogger = (logger: Logger): IntegratedLogger =>
  Object.assign({}, console, logger) as IntegratedLogger;

const server = new ApolloServer<MyContext>({
  typeDefs,
  resolvers,
  formatError: (formattedError: GraphQLFormattedError): GraphQLFormattedError => {
    integratedLogger.error({
      error: formattedError.message,
      locations: formattedError.locations,
      path: formattedError.path,
      extensions: formattedError.extensions,
    });
    return formattedError;
  },
  includeStacktraceInErrorResponses: NODE_ENV === 'development',
  logger: integratedLogger,
});

const azureFunction = startServerAndCreateHandler(server, {
  context: async ({ req, context }): Promise<MyContext> => {
    integratedLogger = createIntegratedLogger(context.log);

    return {
     ...
    };
  },
});

export default azureFunction;

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

1 participant