Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!opts.prefix) {
opts.prefix = ""
}
PREFIX = Object.freeze(opts.prefix)
// TODO optimize schema creation (use a pool instead of a single connection ?)
const mysqlConnection = mysql.createConnection(opts.mysqlConfig)
// Mix-in for Data Access Methods and SQL Autogenerating Methods
mysqlUtilities.upgrade(mysqlConnection)
// Mix-in for Introspection Methods
mysqlUtilities.introspection(mysqlConnection)
// initialize the graphQL schema
const schemaComposer = new SchemaComposer()
const mysqlTablesNames = await _getMysqlTablesNames(mysqlConnection)
return Promise.all(mysqlTablesNames.map(async mysqlTableName => {
// initialize the graphql type built from the mysql table
const gqlTC = schemaComposer.TypeComposer.create({
name: _clearNameForType(mysqlTableName),
})
// add local fields
const fields = await _buildGqlFieldsFromMysqlTable(mysqlConnection, mysqlTableName)
gqlTC.addFields(fields)
// add local resolver
const resolver = _buildResolverForGqlType(opts.mysqlConfig, mysqlTableName, gqlTC)
schemaComposer.Query.addFields({ [resolver.name]: resolver })
/* @flow */
// SINGLE SCHEMA ON SERVER
// import { schemaComposer } from 'graphql-compose';
// MULTI SCHEMA MODE IN ONE SERVER
// import { SchemaComposer } from 'graphql-compose';
// const schemaComposer = new SchemaComposer();
import { SchemaComposer } from 'graphql-compose';
export type TContext = {
ip: string,
};
export const schemaComposer: SchemaComposer = new SchemaComposer();
test('create filter operators for node references', () => {
const schemaComposer = new SchemaComposer()
schemaComposer.createObjectTC({ name: 'Tag', interfaces: ['Node'] })
const typeComposer = schemaComposer.createObjectTC({
name: 'Post',
interfaces: ['Node'],
fields: {
tag: 'Tag',
tags: ['Tag']
}
})
const inputTypeComposer = createFilterInput(schemaComposer, typeComposer)
const inputFields = inputTypeComposer.getFields()
forEach(inputFields.tag.type.getFields(), (field, fieldName) => {
const extensions = inputFields.tag.type.getFieldExtensions(fieldName)
function createTypeComposer (config) {
const schemaComposer = new SchemaComposer()
const typeComposer = schemaComposer.createObjectTC(config)
initMustHaveTypes(schemaComposer)
addObjectTypeExtensions(typeComposer)
createFilterInput(schemaComposer, typeComposer)
return typeComposer
}
])('prefer float when mixed number types - %s', (_, nodes) => {
const schemaComposr = new SchemaComposer()
const fields = createFieldDefinitions(nodes)
const types = createFieldTypes(schemaComposr, fields, 'Test')
expect(types.number.type).toEqual('Float')
})
function createTypeComposer (config) {
const schemaComposer = new SchemaComposer()
const typeComposer = schemaComposer.createObjectTC(config)
initMustHaveTypes(schemaComposer)
addObjectTypeExtensions(typeComposer)
createFilterInput(schemaComposer, typeComposer)
return typeComposer
}
module.exports = function createSchema (store, options = {}) {
const { types = [], schemas = [], resolvers = [], extensions = [] } = options
const schemaComposer = new SchemaComposer()
initMustHaveTypes(schemaComposer).forEach(typeComposer => {
schemaComposer.addSchemaMustHaveType(typeComposer)
})
addDirectives(schemaComposer, extensions)
directives.forEach(directive => {
schemaComposer.addDirective(directive)
})
types.forEach(typeOrSDL => {
addTypes(schemaComposer, typeOrSDL)
})
createNodesSchema(schemaComposer, store)
import elasticsearch from 'elasticsearch';
import { SchemaComposer } from 'graphql-compose';
import { ElasticApiParser } from 'graphql-compose-elasticsearch';
function checkHost(host): void {
if (host === 'http://user:pass@example.com:9200') {
throw new Error(
"✋ 🛑 I don't have public elasticsearch instance for demo purposes. \n" +
'🚀 Demo will work if you provide public elasticsearch instance url \n' +
'🚀 in query argument `host: "http://user:pass@example.com:9200"` \n'
);
}
}
const schemaComposer = new SchemaComposer();
schemaComposer.Query.setField('elastic60', {
description: 'Elastic v6.0',
type: schemaComposer.createObjectTC({
name: 'Elastic60',
fields: new ElasticApiParser({ version: '6_0', prefix: 'Elastic60' }).generateFieldMap(),
}),
args: {
host: {
type: 'String',
defaultValue: 'http://user:pass@example.com:9200',
},
},
resolve: (src, args, context) => {
checkHost(args.host);
context.elasticClient = new elasticsearch.Client({