Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@graphql-tools/stitch
Advanced tools
@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.
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());
Apollo Server is a community-maintained open-source GraphQL server that works with any GraphQL schema. It provides a straightforward setup for creating a GraphQL server and supports schema stitching through its federation capabilities. Compared to @graphql-tools/stitch, Apollo Server is more focused on providing a complete server solution with additional features like caching, monitoring, and more.
GraphQL Yoga is a fully-featured GraphQL server with focus on easy setup, performance, and great developer experience. It supports schema stitching and merging, similar to @graphql-tools/stitch, but also includes built-in support for subscriptions and other advanced features. GraphQL Yoga is designed to be a more opinionated and feature-rich alternative to setting up a GraphQL server from scratch.
merge-graphql-schemas is a utility library for merging GraphQL schemas. It provides a simple API for combining multiple schema files into a single executable schema. While it offers similar functionality to @graphql-tools/stitch in terms of schema merging, it is more lightweight and focused solely on the merging aspect without additional features like schema delegation.
Check API Reference for more information about this package; https://www.graphql-tools.com/docs/api/modules/stitch_src
You can also learn more about Schema Stitching in this chapter; https://www.graphql-tools.com/docs/stitch-combining-schemas
FAQs
A set of utils for faster development of GraphQL tools
We found that @graphql-tools/stitch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.