Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mercuriusjs/federation

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mercuriusjs/federation

A plugin for mercurius federation

  • 4.0.0
  • next
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
48K
decreased by-1.63%
Maintainers
2
Weekly downloads
 
Created
Source

@mercuriusjs/federation

A module to add Apollo Federation v1 metadata info to a schema.

Quick start

npm i fastify @mercuriusjs/federation
const Fastify = require('fastify')
const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')

const users = {
  1: {
    id: '1',
    name: 'John',
    username: '@john'
  },
  2: {
    id: '2',
    name: 'Jane',
    username: '@jane'
  }
}

const app = Fastify()
const schema = `
  extend type Query {
    me: User
  }

  type User @key(fields: "id") {
    id: ID!
    name: String
    username: String
  }
`

const resolvers = {
  Query: {
    me: () => {
      return users['1']
    }
  },
  User: {
    __resolveReference: (source, args, context, info) => {
      return users[source.id]
    }
  }
}

app.register(mercuriusFederationPlugin, {
  schema,
  resolvers
})

app.get('/', async function (req, reply) {
  const query = '{ _service { sdl } }'
  return app.graphql(query)
})

app.listen({ port: 3000 })

Build a schema and pass it to mercurius

Instead of using the plugin, the federation schema can be built using the buildFederationSchema function and passing the schema generated to mercurius.

const Fastify = require('fastify')
const mercurius = require('mercurius')
const { buildFederationSchema } = require('../')

...

app.register(mercurius, {
  schema: buildFederationSchema(schema),
  resolvers,
  graphiql: true
})

...

API

mercuriusFederationPlugin

A fastify plugin to create a mercurius server that expose the federation directives.

const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')

const schema = ...
const resolvers = ...
const app = Fastify()

app.register(mercuriusFederationPlugin, {
  schema,
  resolvers
})
options

Uses the same options of mercurius but it requires a string, DocumentNode or an Array of DocumentNode for schema attribute.

buildFederationSchema

Create a schema object that can be used in a federated environment

(schema, opts) => GraphQLSchema

  • schema string | DocumentNode | Array: the source schema
  • opts object:
    • isGateway boolean: If enabled create a schema compatible with the gateway, Default 'false'

Keywords

FAQs

Package last updated on 09 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc