@basementscripts/graphql-schema-builder
v1.1.6
Published
GraphQL Schema Builder for Node.js
Downloads
22
Keywords
Readme
@basementscripts/graphql-schema-builder
Implement Schema in ApolloServer instance
import { ApolloServer, ApolloServerExpressConfig } from 'apollo-server-express'
import { SchemaBuilder } from '@basementscripts/graphql-schema-builder'
import { base, input, filter } from '@basementscripts/graphql-schema-builder/typeDefs'
import { UserTypeDefs, UserResolver } from '@user-service/graphql'
const builder = new SchemaBuilder({
typeDefs: [base, input, filter, UserTypeDefs],
resolvers: [UserResolver]
})
const server = new ApolloServer({
schema: builder.toSchema()
//...config
})Resolver
import {
field,
fields,
mutation,
query,
resolver
} from '@basementscripts/graphql-schema-builder/decorators'
@resolver()
export default class AuthRoleResolver extends Resolver {
@fields('account')
User
@query()
getUser(root, input): GraphQLResponse<any> {
return input
}
@mutation()
createUser(root, { input }: any): GraphQLResponse<any> {
return input
}
@field('user')
sideEffectUser({ user }) {
return user
}
@field('users')
sideEffectUsers({ users }) {
return users
}
}Plugins
import { ApolloServer } from '@apollo/server'
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
import { expressMiddleware } from '@as-integrations/express5'
import express from 'express'
import http from 'http'
import cors from 'cors'
import { typeDefs, resolvers } from './schema'
interface MyContext {
token?: String
}
const app = express()
// Our httpServer handles incoming requests to our Express app.
// Below, we tell Apollo Server to "drain" this httpServer,
// enabling our servers to shut down gracefully.
const httpServer = http.createServer(app)
const server = new ApolloServer<MyContext>({
typeDefs,
resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })]
})
await server.start()
app.use(
'/graphql',
cors<cors.CorsRequest>(),
express.json(),
expressMiddleware(server, {
context: async ({ req }) => ({ token: req.headers.token })
})
)
await new Promise<void>((resolve) => httpServer.listen({ port: 4000 }, resolve))
console.log(`🚀 Server ready at http://localhost:4000/graphql`)Cache Control
import { ApolloServer } from '@apollo/server'
import { ApolloServerPluginCacheControl } from '@apollo/server/plugin/cacheControl'
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [
ApolloServerPluginCacheControl({
// Cache everything for 1 second by default.
defaultMaxAge: 1,
// Don't send the `cache-control` response header.
calculateHttpHeaders: false
})
]
})Disable Cache
import { ApolloServer } from '@apollo/server'
import { ApolloServerPluginCacheControlDisabled } from '@apollo/server/plugin/disabled'
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [ApolloServerPluginCacheControlDisabled()]
})Development
Changelog
This project uses an automated changelog system with Git hooks. The CHANGELOG.md file is automatically updated on each commit based on the commit message.
Commit Message Format
The changelog system automatically categorizes changes based on keywords in your commit messages:
- Added:
feat,add,new - Fixed:
fix,bug - Changed:
change,update,modify - Removed:
remove,delete - Deprecated:
deprecate - Security:
security
Git Hooks
The project includes a commit-msg hook that automatically:
- Reads your commit message
- Categorizes the change based on keywords
- Updates the
CHANGELOG.mdfile in the[Unreleased]section - Adds the updated changelog to your commit
Manual Changelog Updates
If you need to manually update the changelog, edit CHANGELOG.md directly. The file follows the Keep a Changelog format.
Version Releases
When releasing a new version:
- Update the version in
package.json - Move entries from
[Unreleased]to a new version section (e.g.,[1.1.0] - 2024-01-15) - Add the release date
- Commit the changes
