Apollo Server Middleware
Installation
- Install the
inigo.js
middleware packagenpm install inigo.js
- Install your platform specific library:
npm install inigo-linux-amd64
Available libraries:
- inigo-linux-amd64
- inigo-linux-arm64
- inigo-alpine-amd64
- inigo-alpine-arm64
- inigo-darwin-amd64
- inigo-darwin-arm64
- inigo-windows-amd64
Configuration
- Import
InigoPlugin
&InigoConfig
frominigo.js
moduleimport { InigoPlugin, InigoConfig } from 'inigo.js';
-
For predefined GraphQL schema definitions
- Create an inigo config object
const inigoCfg = new InigoConfig({ Token: "YOUR-INIGO-SERVICE-TOKEN", // Input token generated using inigo cli or web panel Schema: typeDefs // String based SDL format GraphQL Schema });
- Create an inigo config object
-
For runtime-generated GraphQL schema definitions
GraphQL.js utilities can be utilized for conversion.
- Install the
graphql
packagenpm install graphql
- Import
printSchema
fromgraphql
packageimport { printSchema } from 'graphql';
- Create an inigo config object
const inigoCfg = new InigoConfig({ Token: "YOUR-INIGO-SERVICE-TOKEN", // Input token generated using inigo cli or web panel Schema: printSchema(typeDefs) // Convert GraphQLSchema object to SDL format });
- Install the
-
- Plug in the middleware by adding the following to
plugins
withinApolloServer
InigoPlugin(inigoCfg)
Result:
const server = new ApolloServer({ typeDefs, resolvers, introspection: true, plugins: [ InigoPlugin(inigoCfg) // <--- ] });
- Your final configuration should look like the following example
import { ApolloServer } from 'apollo-server'; import { InigoPlugin, InigoConfig } from 'inigo.js'; // <--- const typeDefs = ` type Query { hello: String } `; const resolvers = { Query: { hello: () => 'world', }, }; const inigoCfg = new InigoConfig({ // <--- Token: "eyJhbGc..", // <--- Schema: typeDefs // <--- }); // <--- const server = new ApolloServer({ typeDefs, resolvers, introspection: true, plugins: [ // <--- InigoPlugin(inigoCfg) // <--- ] // <--- }); server.listen().then(({ url }) => { console.log(`🚀 Server ready at ${url}`); });
Passing Authentication using JWT header
- Configure and apply your
service.yml
kind: Service name: starwars spec: path_user_id: jwt.user_name path_user_profile: jwt.user_profile path_user_role: jwt.user_roles
- Configure
ApolloServer
to pass in aninigo
object within context containing thejwt
from the request headers.Note:
jwt
is always prioritized when found withctx
or other.const server = new ApolloServer({ typeDefs, resolvers, introspection: true, plugins: [ InigoPlugin(inigoCfg) ], context: async ({ req }) => { return { inigo: { jwt: req.headers.authorization ?? "" } } } );
Passing Authentication using Context
- Configure and apply your
service.yml
kind: Service name: starwars spec: path_user_id: ctx.user_name path_user_profile: ctx.user_profile path_user_role: ctx.user_roles
- Configure
ApolloServer
to pass in aninigo
object containing context.Note:
jwt
is always prioritized when found withctx
or other.
Note: It’s important to have object names identical to what was referenced in the service.yml
const server = new ApolloServer({ typeDefs, resolvers, introspection: true, plugins: [ InigoPlugin(inigoCfg) ], context: async ({ req }) => { return { inigo: { ctx: { user_name: "yoda", user_profile: "admin", user_roles: [ "producer", "director", "actor", "viewer" ], } } } } );