Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* @flow */
/* eslint-disable arrow-body-style */
import { upperFirst, ObjectTypeComposer, type SchemaComposer } from 'graphql-compose';
// PaginationInfo should be global
const PaginationInfoTC = ObjectTypeComposer.createTemp(`
# Information about pagination.
type PaginationInfo {
# Current page number
currentPage: Int!
# Number of items per page
perPage: Int!
# Total number of pages
pageCount: Int
# Total number of items
itemCount: Int
# When paginating forwards, are there more items?
hasNextPage: Boolean
Object.keys(fields).forEach(k => {
const names = k.split('.');
if (names.length === 1) {
result[names[0]] = fields[k];
} else {
if (!result[names[0]]) {
result[names[0]] = {
type: ObjectTypeComposer.createTemp({
name: `${this.prefix}_${upperFirst(names[0])}`,
fields: (() => {}: any),
}),
resolve: () => {
return {};
},
};
}
result[names[0]].type.setField(names[1], fields[k]);
}
});
function createType (schemaComposer, type, options) {
switch (type) {
case CreatedGraphQLType.Object: {
convertExtensionsToDirectives(options)
return ObjectTypeComposer.createTemp(options, schemaComposer)
}
case CreatedGraphQLType.Union:
return UnionTypeComposer.createTemp(options, schemaComposer)
case CreatedGraphQLType.Input:
return InputTypeComposer.createTemp(options, schemaComposer)
case CreatedGraphQLType.Scalar:
return ScalarTypeComposer.createTemp(options, schemaComposer)
case CreatedGraphQLType.Interface:
return InterfaceTypeComposer.createTemp(options, schemaComposer)
case CreatedGraphQLType.Enum:
return EnumTypeComposer.createTemp(options, schemaComposer)
}
function createObjectType (schemaComposer, value, fieldName, prefix) {
const name = createTypeName(prefix, fieldName)
const fields = {}
for (const key in value) {
const options = value[key]
const type = createFieldType(schemaComposer, options.value, key, name)
if (type) {
fields[options.fieldName] = type
}
}
return !isEmpty(fields)
? { type: ObjectTypeComposer.createTemp({ name, fields }, schemaComposer) }
: null
}
function createGlobalPageInfoType(name: string) {
if (!globalPageInfoTypes[name]) {
globalPageInfoTypes[name] = ObjectTypeComposer.createTemp(`
"""Information about pagination in a connection."""
type ${name} {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!
"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!
"""When paginating backwards, the cursor to continue."""
startCursor: String
"""When paginating forwards, the cursor to continue."""
endCursor: String
}
`);
}
function instanceElasticClient(elasticClient: Object): GraphQLFieldConfig {
const apiVersion = elasticClient.transport._config.apiVersion || DEFAULT_ELASTIC_API_VERSION;
const prefix = `ElasticAPI${apiVersion.replace('.', '')}`;
const apiParser = new ElasticApiParser({
elasticClient,
prefix,
});
return {
description: `Elastic API v${apiVersion}`,
type: ObjectTypeComposer.createTemp({
name: prefix,
fields: apiParser.generateFieldMap(),
}).getType(),
resolve: () => ({}),
};
}
static createTC(name: string, json: Object): ObjectTypeComposer {
if (!json || typeof json !== 'object') {
throw new Error('You provide empty object in second arg for `createTC` method.');
}
const tc = ObjectTypeComposer.createTemp(name);
Object.keys(json).forEach(fieldName => {
const fieldConfig = this.getFieldConfig(json[fieldName], { typeName: name, fieldName });
tc.setField(fieldName, fieldConfig);
});
return tc;
}