Documentation
API
Using Context

Using Context

Here’s how you provide a context to a GraphQL Schema.

Using a Function

Sofa allows to build a context object through a factory function. You get an access to a request object through the req field.

api.use(
  '/api',
  useSofa({
    schema,
    async context({ req }) {
      return {
        req,
        ...yourContext,
      };
    },
  })
);

Using an Object

You can also pass any data, directly, without using a function.

api.use(
  '/api',
  useSofa({
    schema,
    context: yourContext,
  })
);