Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
merge-graphql-schemas
Advanced tools
When using graphql-tools, a package from the ApolloStack Team, to combine our
types and resolvers, we call the function makeExecutableSchema()
, passing the full schema as a string, and a resolvers object.
For the schema file, we can create it as a single string containing all types. But as the app grows, so does the size/complexity of this file. On the other hand, if we put every type in its own file, we need a way of merging all that information back into one string. Apollo lets us pass multiple strings, which lets us separate types, but the root queries for those types still need to be merged with the root query and mutation objects. We would like to be able to specify a types and queries that belong to the same domain together:
ClientType
type Client {
id: ID!
name: String
age: Int
products: [Product]
}
type Query {
clients: [Client]
client(id: ID!): Client
}
ProductType
type Product {
id: ID!
description: String
price: Int
}
type Query {
products: [Product]
product(id: ID!): Product
}
Merged Result
type Client {
id: ID!
name: String
age: Int
products: [Product]
}
type Product {
id: ID!
description: String
price: Int
}
type Query {
clients: [Client]
client(id: ID!): Client
products: [Product]
product(id: ID!): Product
}
It's the same for our resolvers: Create everything in one big/complex file/object or merge multiple files/objects into one.
This package will allow you to just specify a folder or a set of imports to merge. mergeGraphqlSchemas()
will merge not only your types but also root queries in the correct format to be passed to your GraphQL server.
There are two ways you can use this package:
types
and resolvers
into separate index
filesThis is the easiest way of getting started.
import path from 'path';
import { mergeGraphqlSchemas } from 'merge-graphql-schemas';
import { graphqlExpress } from 'graphql-server-express';
const schema = mergeGraphqlSchemas(path.join(__dirname, './graphql'));
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
For this to work, the package expects that your /graphql
folder contains /types
and /resolvers
folders. Any files you add to those folders will be used to produce the final schema object.
types
and resolvers
into separate index
filesIf you prefer to have more control over what gets merged, the other way you can use this package is by only calling the mergeTypes()
and mergeResolvers()
functions.
Take a look at the example below:
import { mergeTypes } from 'merge-graphql-schemas';
import clientType from './client_type';
import productType from './product_type';
// Passing an array with all types you want merged
export default mergeTypes([clientType, productType]);
And the same idea for your resolvers:
import { mergeResolvers } from 'merge-graphql-schemas';
import clientResolver from './client_resolver';
import productResolver from './product_resolver';
// Passing an array with all resolvers you want merged
export default mergeResolvers([clientResolver, productResolver]);
Here's an example of how you would implement your server:
import express from 'express';
import { makeExecutableSchema } from 'graphql-tools';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
// Import your types and resolvers
import resolvers from './graphql/resolvers/index';
import typeDefs from './graphql/types/index';
// In this case, you need to call makeExecutableSchema()
const schema = makeExecutableSchema({ typeDefs, resolvers });
const app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
Here's an example of how you should implement your types:
export default `
type Client {
id: ID!
name: String
age: Int
products: [Product]
}
type Query {
clients: [Client]
}
type Mutation {
create_client(name: String!, age: Int!): Client
}
`;
At this moment, you need to use the Query
and Mutation
types.
Here's an example of how you should implement your resolvers:
export default {
Query: {
clients: () => {}
},
Mutation: {
create_client: (_, args) => {}
},
Client: {
products: () => {},
},
}
MIT licensed
Copyright (C) 2017 OK GROW!, http://www.okgrow.com
FAQs
A utility library to facilitate merging of modularized GraphQL schemas and resolver objects.
We found that merge-graphql-schemas demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.