Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding schema injection #448

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"pg-minify": "^0.4.1",
"pluralize": "^3.0.0",
"postgres-interval": "^1.0.2",
"require-glob": "^3.2.0",
"send": "^0.14.1",
"tslib": "^1.5.0"
},
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/schema/BuildToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ interface BuildToken {
// If true then the default mutations for tables (e.g. createMyTable) will
// not be created
readonly disableDefaultMutations: boolean,
// Path to read shcema injections from
readonly schemaInjection: string,
},
// Hooks for adding custom fields/types into our schema.
readonly _hooks: _BuildTokenHooks,
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/schema/createGqlSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type SchemaOptions = {
// GraphQL types that override the default type generation. Currently this
// API is private. Use at your own risk.
_typeOverrides?: _BuildTokenTypeOverrides,
// The path to read our injections from
schemaInjection?: string,
}

/**
Expand All @@ -36,6 +38,7 @@ export default function createGqlSchema (inventory: Inventory, options: SchemaOp
nodeIdFieldName: options.nodeIdFieldName || 'nodeId',
dynamicJson: options.dynamicJson || false,
disableDefaultMutations: options.disableDefaultMutations || false,
schemaInjection: options.schemaInjection || '',
},
_hooks: options._hooks || {},
_typeOverrides: options._typeOverrides || new Map(),
Expand Down
5 changes: 4 additions & 1 deletion src/graphql/schema/getMutationGqlType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLObjectType, GraphQLFieldConfig } from 'graphql'
import { buildObject, memoize1 } from '../utils'
import { buildObject, memoize1, loadInjections } from '../utils'
import BuildToken from './BuildToken'
import createCollectionMutationFieldEntries from './collection/createCollectionMutationFieldEntries'

Expand Down Expand Up @@ -38,6 +38,9 @@ function createMutationGqlType (buildToken: BuildToken): GraphQLObjectType | und
.map(collection => createCollectionMutationFieldEntries(buildToken, collection))
.reduce((a, b) => a.concat(b), [])
),
...(loadInjections(options.schemaInjection, 'mutation')

),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird whitespace?

]

// If there are no mutation fields, just return to avoid errors.
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/schema/getQueryGqlType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLObjectType, GraphQLFieldConfig, GraphQLNonNull, GraphQLID } from 'graphql'
import { buildObject, memoize1 } from '../utils'
import { buildObject, memoize1, loadInjections } from '../utils'
import createNodeFieldEntry from './node/createNodeFieldEntry'
import getNodeInterfaceType from './node/getNodeInterfaceType'
import createCollectionQueryFieldEntries from './collection/createCollectionQueryFieldEntries'
Expand Down Expand Up @@ -36,6 +36,7 @@ function createGqlQueryType (buildToken: BuildToken): GraphQLObjectType {
.getCollections()
.map(collection => createCollectionQueryFieldEntries(buildToken, collection))
.reduce((a, b) => a.concat(b), []),
loadInjections(options.schemaInjection, 'query'),
[
// The root query type is useful for Relay 1 as it limits what fields
// can be queried at the top level.
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import formatName from './formatName'
import idSerde from './idSerde'
import scrib from './scrib'
import parseGqlLiteralToValue from './parseGqlLiteralToValue'
import loadInjections from './loadInjections'

export { buildObject, formatName, idSerde, scrib, parseGqlLiteralToValue }
export { buildObject, formatName, idSerde, scrib, parseGqlLiteralToValue, loadInjections }

export * from './memoize'
23 changes: 23 additions & 0 deletions src/graphql/utils/loadInjections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* A utility function for requiring all files in a provided directory
* Returns an array of double dimensions, as expected by mutationEntries
*/

import { GraphQLFieldConfig } from 'graphql'
import * as requireGlob from 'require-glob'

export default function loadInjections(dirToInject: string, type: string): Array<[string, GraphQLFieldConfig<never, mixed>]> {

if (dirToInject === '' ) return []

const injections = requireGlob.sync(dirToInject, {
cwd: process.cwd(),
reducer: function (options, tree, file) {
if (!Array.isArray(tree)) tree = []
if (file.exports.type === type) tree.push([file.exports.name, file.exports.schema])
return tree
},
})

return injections
}
3 changes: 3 additions & 0 deletions src/postgraphql/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ program
.option('--export-schema-json [path]', 'enables exporting the detected schema, in JSON format, to the given location. The directories must exist already, if the file exists it will be overwritten.')
.option('--export-schema-graphql [path]', 'enables exporting the detected schema, in GraphQL schema format, to the given location. The directories must exist already, if the file exists it will be overwritten.')
.option('--show-error-stack [setting]', 'show JavaScript error stacks in the GraphQL result errors')
.option('--schema-injection [path]', 'requires the files in the specified path and injects them into the GraphQL schema (supports glob\'s)')

program.on('--help', () => console.log(`
Get Started:
Expand Down Expand Up @@ -81,6 +82,7 @@ const {
exportSchemaGraphql: exportGqlSchemaPath,
showErrorStack,
bodySizeLimit,
schemaInjection,
// tslint:disable-next-line no-any
} = program as any

Expand Down Expand Up @@ -125,6 +127,7 @@ const server = createServer(postgraphql(pgConfig, schemas, {
exportJsonSchemaPath,
exportGqlSchemaPath,
bodySizeLimit,
schemaInjection,
}))

// Start our server by listening to a specific port and host name. Also log
Expand Down
1 change: 1 addition & 0 deletions src/postgraphql/postgraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type PostGraphQLOptions = {
exportGqlSchemaPath?: string,
bodySizeLimit?: string,
pgSettings?: { [key: string]: mixed },
schemaInjection?: string,
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/postgraphql/schema/createPostGraphQLSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default async function createPostGraphQLSchema (
jwtSecret?: string,
jwtPgTypeIdentifier?: string,
disableDefaultMutations?: boolean,
schemaInjection?: string,
} = {},
): Promise<GraphQLSchema> {
// Create our inventory.
Expand Down Expand Up @@ -109,6 +110,7 @@ export default async function createPostGraphQLSchema (
nodeIdFieldName: options.classicIds ? 'id' : 'nodeId',
dynamicJson: options.dynamicJson,
disableDefaultMutations: options.disableDefaultMutations,
schemaInjection: options.schemaInjection,

// If we have a JWT Postgres type, let us override the GraphQL output type
// with our own.
Expand Down