![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
apollo-helpers
Advanced tools
**DEPRECATION NOTICE** This module is deprecated and no longer supported. Apollo 2.0 implemented tools that make it easy to merge schemas.
DEPRECATION NOTICE This module is deprecated and no longer supported. Apollo 2.0 implemented tools that make it easy to merge schemas.
A collection of functions I would rewrite over and over again the more I dealt with Apollo graphql-tools, and other graphql setups.
npm install apollo-helpers
apolloHelpers.compileGraphqlResources(modules)
This is used to combine an array of "graphql resources", each with their own schema, resolvers, and context definitions, into a single set of properties you can use to pass into { makeExecutableSchema } from 'graphql-tools'.
const graphqlModules = [
{
schema: `type Query { health }`,
resolvers: { Query: { health: () => 'ok' } }
},
{
schema: `extend type Query { health2 }`,
resolvers: {
Query: {
health2: (parent, args, context) => context.newHealth
}
},
context: req => ({ newHealth: 'A-O-K' })
}
]
const graphqlResources = compileGraphqlResources(graphqlModules)
const schema = makeExecutableSchema({
typeDefs: graphqlResources.typeDefs,
resolvers: graphqlResources.resolvers
})
app.use(
'/graphql',
graphqlExpress(req => ({
schema,
context: graphqlResources.getContext(req)
}))
)
The only argument is the array of graphql modules. Each of the graphql modules can have the following properties:
module.schema
- A string, or a function that returns an array of strings
which represent the schema (and dependent schemas) of your graphql module. See
the apollo docs
for how modularizing schemas works. Note that one of your modules will need to
expose a type Query {}
with something in it. An example graphql module will
show you below.module.resolvers
- An object, who's shape lines up with your schema. This
object will be deeply merged into the rest of the resolvers for each of your
modules. Each resolver function takes in three arguments: parent
which is
the object or value returned from the "parent" type (or the root value of your
schema as might be the case), args
which are the arguments that you've
defined in your GraphQL Schema, and context
which is the context of the
query, which we'll look at in a minute.module.context
- A function which takes in the request
from whatever
server framework you're using and should return an object, which will be
merged together on every graphql request and passed as the context
variable
in your resolvers, allowing each graphql module to define it's own context.
Note that every one of your "graphql module" contexts will be merged together
regardless of which resolvers are being called, and merged in a shallow way
(top level keys only, unlike resolvers).The result of this function gives you an object which you can give to the
graphql-tools
makeExecutableSchema
and middleware function. The object will
be referred to as graphqlResources
graphqlResources.typeDefs
- The array of graphql type definiions needed to
pass into makeExecutableSchema
.graphqlResources.resolvers
- An object containing the resolvers for your
queries.graphqlResources.getContext
- A function that you call with your server
framework "request context" variable, and will return you the merged context
object from all of your graphql modules.// resources/system/system.graphql.js
const systemController = {
health: () => 'ok',
echo: msg => msg
}
exports.schema = `
type Query {
health: String!
}
type Mutation {
echo(msg: String!): String!
}
`
exports.context = req => ({
user: req.user || { id: null },
system: systemController
})
exports.resolvers = {
Query: {
health(parent, args, context) {
return context.system.health()
}
},
Mutation: {
echo(parent, args, context) {
return context.system.echo(args.msg)
}
}
}
// resources/todos/todos.graphql.js
// NOTE that in the examples, the business logic is happening in the resolvers.
// My personal opinion is that the logic should happen in the controller, and
// the resolvers should just call functions straight into the controller and
// just act as a translation layer.
const todoController = require('./todos.controller')
exports.schema = `
type Todo {
id: ID!
label: String!
completed: Boolean!
}
extend type Query {
todos(completed: Boolean): [Todo]!
todo(id: ID!): Todo
}
extend type Mutation {
createTodo(label: String!): Todo
updateTodo(
id: ID!
label: String
completed: Boolean
): Todo
deleteTodo(id: ID!): Todo
}
`
exports.context = req => ({
todos: todoController
})
exports.resolvers = {
Todo: {
id(parent) {
return String(parent._id)
}
},
Query: {
todos(parent, args, context) {
const query = Object.assign({}, args, { owner: context.user.id })
return context.todos.find(query)
},
todo(parent, { id }, context) {
return context.todos.findOne({
_id: id,
owner: context.user.id
})
}
},
Mutation: {
createTodo(parent, { label }, context) {
return context.todos.create({
label,
completed: false,
owner: context.user.id
})
},
updateTodo(parent, { id, ...update }, context) {
return context.todos.findOneAndUpdate(
{
_id: id,
owner: context.user.id
},
update,
{ new: true }
)
},
deleteTodo(parent, { id }, context) {
return context.todos.findOneAndRemove({
_id: id,
owner: context.user.id
})
}
}
}
// app.js
const express = require('express')
const bodyParser = require('body-parser')
const { graphqlExpress } = require('apollo-server-express')
const { makeExecutableSchema } = require('graphq-tools')
const { compileGraphqlResources } = require('apollo-helpers')
// Resources
const system = require('./resources/system/system.graphql')
const todos = require('./resources/todos/todos.graphql')
const graphqlResources = compileGraphqlResources([system, todos])
const schema = makeExecutableSchema({
typeDefs: graphqlResources.typeDefs,
resolvers: graphqlResources.resolvers
})
app.use(bodyParser.json())
app.use(
'/graphql',
graphqlExpress(req => ({
schema,
context: graphqlResources.getContext(req)
}))
)
apolloHelpers.createExecutor(graphqlModule)
Creates a function that you can call which will execute a graphql query without needing to setup apollo dev server or anything like that. Useful for testing your graphql modules separate from each other.
graphqlModule
- This module follows the same shape/symantics of the graphql
modules used in compileGraphqlResources
, with a schema
, context
, and
resolvers
property. You can also pass in an array of modules if your module
depends on another to work property.Returns an object with a method you can call to run the query.
const { createExecutor } = require('apollo-helpers')
const pubsub = new require('graphql-subscriptions').PubSub()
const graphqlModule = {
schema: `
Query { health: String! }
Mutation { echo(msg: String!): String! }
Subscription { tick: Int! }
`,
resolvers: {
Query: {
health: () => 'ok'
},
Mutation: {
echo: (parent, args) => args.msg
},
Subscription: {
tick: {
subscribe: () => pubsub.asyncIterator('tick')
}
}
}
}
setInterval(() => {
pubsub.publish('tick', { tick: Date.now() })
}, 1000)
const executor = createExecutor(graphqlModule)
executor.run(`query { health }`).then(result => console.log(result)) // { data: { health: 'ok' } }
executor
.run(
`
mutation Echo($message: String!) {
echo(msg: $message)
}
`,
{
variableValues: { message: 'Hello world!' }
}
)
.then(result => console.log(result)) // { data: { echo: 'Hello world!' } }
executor.subscribe(`subscription { tick }`, {}, result => {
console.log(result) // { data: { tick: 1510239888797 }}
})
executor.run(query, graphqlOptions)
Runs the query and returns a promise which resolves with the data.
query
- The graphql query to rungraphqlOptions
- An object of object which help give more context to the
query.
graphqlOptions.contextArg
- The argument to pass to each of the
.context()
functions on each of the graphqlModulesgraphqlOptions.rootValue
- The parent object (first argument) to all of
the top level query and mutation resolversgraphqlOptions.operationName
- If you have multiple operations defined
in your query
, you need to specify an operationName to tell the executor
which query to rungraphqlOptions.contextValue
- Additional context to pass into the
context
argumentgraphqlOptions.variableValues
- Variables to pass into the graphqlQueryexecutor.subscribe(query, graphqlOptions, callback)
Subscribes to a given subscription and calls the callback when that query
publishes an event.
FAQs
**DEPRECATION NOTICE** This module is deprecated and no longer supported. Apollo 2.0 implemented tools that make it easy to merge schemas.
The npm package apollo-helpers receives a total of 0 weekly downloads. As such, apollo-helpers popularity was classified as not popular.
We found that apollo-helpers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.