Skip to content

Commit

Permalink
enable root on source queries
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcacciatore committed Jul 17, 2019
1 parent 689f574 commit 667e92d
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 50 deletions.
18 changes: 13 additions & 5 deletions compile/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { sep } = require('path');
const { GRAPHQL_OPTIONS_KEY } = require('../constants');
const { GRAPHQL_OPTIONS_KEY, ROOT_OPTIONS_KEY } = require('../constants');
const { isNestedObject, isSource, getOutputType } = require('./helpers');
const getNumberOfQueries = require('../utils/getNumberOfQueries');

const createArgumentQuery = schema => {
const args = isSource(schema) ? schema.source.args : schema.args;
Expand All @@ -24,16 +25,23 @@ const compileSchema = schema => {
return `${client} ${key}${args}`;
}
if (isSource(currentField)) {
return `${client} ${key}${args} ${compileSchema(
Array.isArray(currentField.schema) ? currentField.schema[0] : currentField.schema,
)}`;
const schemaValue = Array.isArray(currentField.schema)
? currentField.schema[0]
: currentField.schema;
const numberOfQueries = getNumberOfQueries(schemaValue);

if (numberOfQueries === 1) {
return `${client} ${key}${args}`;
}

return `${client} ${key}${args} ${compileSchema(schemaValue)}`;
}
if (isNestedObject(currentField) && key !== GRAPHQL_OPTIONS_KEY) {
return `${client} ${key}${args} ${compileSchema(
Array.isArray(currentField) ? currentField[0] : currentField,
)}`;
}
if (key !== GRAPHQL_OPTIONS_KEY) {
if (key !== GRAPHQL_OPTIONS_KEY && key !== ROOT_OPTIONS_KEY) {
console.warn(`Could not determine the GraphQL type of ${key}`);
}

Expand Down
31 changes: 30 additions & 1 deletion compile/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const compileClient = require('./client');
const { isGraphQLOutputType, isNestedObject, isSource } = require('./helpers');
const { createOutputType } = require('./types');
const consolidate = require('../utils/consolidate');
const hasRoot = require('../utils/hasRoot');
const getNumberOfQueries = require('../utils/getNumberOfQueries');

const findFiles = promisify(glob);

Expand Down Expand Up @@ -141,6 +143,34 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
return schemaValue;
})();

if (isSchemaASource && !schemaToCompile.type) {
const numberOfQueries = getNumberOfQueries(requestedSchema, true);
const sourceHasRoot = hasRoot(schema);
if (numberOfQueries.length === 1 && !sourceHasRoot) {
return {
...final,
[key]: {
args: createArgumentConfig(
{
...sourceArgs,
...args,
},
typeName,
),
type: Array.isArray(numberOfQueries[0])
? GraphQLList(numberOfQueries[0])
: numberOfQueries[0],
resolve: resolver
? async (...arg) => {
const resolved = await resolver(...arg);
return consolidate(resolved, requestedSchema);
}
: () => key,
},
};
}
}

// recursively create the fields for this object
const fields = compile(schemaToCompile, typeName, isSchemaASource);
// Create the type or find it from the types already created
Expand Down Expand Up @@ -184,7 +214,6 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
resolve: resolver
? async (...arg) => {
const resolved = await resolver(...arg);
console.log(resolved, requestedSchema);
return consolidate(resolved, requestedSchema);
}
: () => key,
Expand Down
47 changes: 7 additions & 40 deletions utils/consolidate.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,8 @@
const { GRAPHQL_PATH, ROOT_OPTIONS_KEY } = require('../constants');

const { isOutputType } = require(GRAPHQL_PATH);
const findPredicates = require('./findPredicates');
const { ROOT_OPTIONS_KEY } = require('../constants');
const getAll = require('./getAll');

const isRoot = (key, value) => key === ROOT_OPTIONS_KEY && value === true;

const getRoot = obj => {
const findRoot = value => {
const keys = Object.keys(value);

for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
const property = value[key];
if (typeof property === 'object') {
if (Array.isArray(property)) {
return property.map(field => findRoot(field));
}

return findRoot(property);
}

if (isRoot(key, property)) {
const { root, ...values } = value;

return values;
}
}

return value;
};

return findRoot(obj);
};
const getRoot = require('./getRoot');
const getNumberOfQueries = require('./getNumberOfQueries');
const hasRoot = require('./hasRoot');

const getCurrentSchema = (potentialSchema = {}) =>
Array.isArray(potentialSchema) ? potentialSchema[0] : potentialSchema;
Expand Down Expand Up @@ -103,12 +73,9 @@ const consolidate = (response, schema, returnSingleValue = true) => {
return result;
}

const numofResults = findPredicates(
schema,
value => isOutputType(value) || (Array.isArray(value) && isOutputType(value[0])),
);
const schemaHasRoot = findPredicates(schema, (value, key) => isRoot(key, value)).length === 1;

const numofResults = getNumberOfQueries(schema);
const schemaHasRoot = hasRoot(schema);
console.log(response, result, schema);
if (schemaHasRoot) {
return getRoot(result);
}
Expand Down
10 changes: 6 additions & 4 deletions utils/findPredicates.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const findPredicates = (obj, predicate, existingPath = '') => {
const findPredicates = (obj, predicate, returnValue = false, existingPath = '') => {
let levels = 0;
const find = (object, key) => {
levels += 1;
const newPath = existingPath ? `${existingPath}.${key}` : key;
const value = object[key];
if (predicate(value, key)) {
return newPath;
return returnValue ? value : newPath;
}
if (typeof value === 'object') {
if (Array.isArray(value)) {
if (typeof value[0] === 'object') {
return value.map(field => findPredicates(field, predicate, newPath)).flat(levels);
return value
.map(field => findPredicates(field, predicate, returnValue, newPath))
.flat(levels);
}

return value.map(field => find(field));
}
return findPredicates(value, predicate, newPath);
return findPredicates(value, predicate, returnValue, newPath);
}
return undefined;
};
Expand Down
13 changes: 13 additions & 0 deletions utils/getNumberOfQueries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { GRAPHQL_PATH } = require('../constants');

const { isOutputType } = require(GRAPHQL_PATH);
const findPredicates = require('./findPredicates');

const getNumberOfQueries = (schema, returnValue = false) =>
findPredicates(
schema,
value => isOutputType(value) || (Array.isArray(value) && isOutputType(value[0])),
returnValue,
);

module.exports = getNumberOfQueries;
31 changes: 31 additions & 0 deletions utils/getRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const isRoot = require('./isRoot');

const getRoot = obj => {
const findRoot = value => {
const keys = Object.keys(value);

for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
const property = value[key];
if (typeof property === 'object') {
if (Array.isArray(property)) {
return property.map(field => findRoot(field));
}

return findRoot(property);
}

if (isRoot(key, property)) {
const { root, ...values } = value;

return values;
}
}

return value;
};

return findRoot(obj);
};

module.exports = getRoot;
6 changes: 6 additions & 0 deletions utils/hasRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const findPredicates = require('./findPredicates');
const isRoot = require('./isRoot');

const hasRoot = schema => findPredicates(schema, (value, key) => isRoot(key, value)).length === 1;

module.exports = hasRoot;
5 changes: 5 additions & 0 deletions utils/isRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { ROOT_OPTIONS_KEY } = require('../constants');

const isRoot = (key, value) => key === ROOT_OPTIONS_KEY && value === true;

module.exports = isRoot;

0 comments on commit 667e92d

Please sign in to comment.