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

@graphql-tools/stitch

Package Overview
Dependencies
Maintainers
4
Versions
1959
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-tools/stitch

A set of utils for faster development of GraphQL tools

  • 9.4.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created

What is @graphql-tools/stitch?

@graphql-tools/stitch is a part of the GraphQL Tools ecosystem that allows you to combine multiple GraphQL schemas into a single unified schema. This is particularly useful for microservices architectures where each service has its own GraphQL schema, and you want to expose a single GraphQL endpoint to clients.

What are @graphql-tools/stitch's main functionalities?

Schema Stitching

This feature allows you to combine multiple GraphQL schemas into a single unified schema. The code sample demonstrates how to stitch together a user schema and a product schema.

const { stitchSchemas } = require('@graphql-tools/stitch');
const { makeExecutableSchema } = require('@graphql-tools/schema');

const userSchema = makeExecutableSchema({
  typeDefs: `
    type User {
      id: ID!
      name: String!
    }
    type Query {
      user(id: ID!): User
    }
  `,
  resolvers: {
    Query: {
      user: () => ({ id: '1', name: 'John Doe' })
    }
  }
});

const productSchema = makeExecutableSchema({
  typeDefs: `
    type Product {
      id: ID!
      name: String!
    }
    type Query {
      product(id: ID!): Product
    }
  `,
  resolvers: {
    Query: {
      product: () => ({ id: '1', name: 'Laptop' })
    }
  }
});

const stitchedSchema = stitchSchemas({
  subschemas: [
    { schema: userSchema },
    { schema: productSchema }
  ]
});

console.log(stitchedSchema.getTypeMap());

Schema Delegation

Schema delegation allows you to forward a query from one schema to another. The code sample demonstrates how to delegate a query to the stitched schema to fetch user data.

const { delegateToSchema } = require('@graphql-tools/stitch');
const { makeExecutableSchema } = require('@graphql-tools/schema');

const userSchema = makeExecutableSchema({
  typeDefs: `
    type User {
      id: ID!
      name: String!
    }
    type Query {
      user(id: ID!): User
    }
  `,
  resolvers: {
    Query: {
      user: () => ({ id: '1', name: 'John Doe' })
    }
  }
});

const productSchema = makeExecutableSchema({
  typeDefs: `
    type Product {
      id: ID!
      name: String!
    }
    type Query {
      product(id: ID!): Product
    }
  `,
  resolvers: {
    Query: {
      product: () => ({ id: '1', name: 'Laptop' })
    }
  }
});

const stitchedSchema = stitchSchemas({
  subschemas: [
    { schema: userSchema },
    { schema: productSchema }
  ]
});

const result = delegateToSchema({
  schema: stitchedSchema,
  operation: 'query',
  fieldName: 'user',
  args: { id: '1' }
});

result.then(data => console.log(data));

Schema Merging

Schema merging allows you to combine multiple schemas into one. The code sample demonstrates how to merge a user schema and a product schema into a single schema.

const { mergeSchemas } = require('@graphql-tools/merge');
const { makeExecutableSchema } = require('@graphql-tools/schema');

const userSchema = makeExecutableSchema({
  typeDefs: `
    type User {
      id: ID!
      name: String!
    }
    type Query {
      user(id: ID!): User
    }
  `,
  resolvers: {
    Query: {
      user: () => ({ id: '1', name: 'John Doe' })
    }
  }
});

const productSchema = makeExecutableSchema({
  typeDefs: `
    type Product {
      id: ID!
      name: String!
    }
    type Query {
      product(id: ID!): Product
    }
  `,
  resolvers: {
    Query: {
      product: () => ({ id: '1', name: 'Laptop' })
    }
  }
});

const mergedSchema = mergeSchemas({
  schemas: [userSchema, productSchema]
});

console.log(mergedSchema.getTypeMap());

Other packages similar to @graphql-tools/stitch

FAQs

Package last updated on 13 Dec 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