Skip to content

Commit

Permalink
Merge pull request #1 from michaelcacciatore/task/client_compiler
Browse files Browse the repository at this point in the history
add basic client compilation - ALPHA 5
  • Loading branch information
michaelcacciatore authored Jul 17, 2019
2 parents 07627a6 + 667e92d commit ff1c0e0
Show file tree
Hide file tree
Showing 11 changed files with 614 additions and 447 deletions.
66 changes: 66 additions & 0 deletions compile/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { sep } = require('path');
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;
if (args) {
const argumentConfig = Object.keys(args).reduce((query, arg) => {
return `${query}${query.length !== 1 ? ' ' : ''}${arg}: $${arg},`;
}, '(');

return `${argumentConfig.substr(0, argumentConfig.length - 1)})`;
}

return '';
};

const compileSchema = schema => {
const start = Object.keys(schema).reduce((client, key) => {
const currentField = Array.isArray(schema[key]) ? schema[key][0] : schema[key];
const { type } = getOutputType(currentField);
const args = createArgumentQuery(currentField);
if (type) {
return `${client} ${key}${args}`;
}
if (isSource(currentField)) {
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 && key !== ROOT_OPTIONS_KEY) {
console.warn(`Could not determine the GraphQL type of ${key}`);
}

return client;
}, '{');

return `${start} }`;
};

const compileClient = schemaFiles => {
return schemaFiles.reduce((client, file) => {
const schemaContents = require(`${process.cwd()}${sep}${file}`); // eslint-disable-line global-require
const { name, schema } = schemaContents;

return {
...client,
[name]: `${name} ${compileSchema(schema)}`,
};
}, {});
};

module.exports = compileClient;
77 changes: 70 additions & 7 deletions compile/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ const { GRAPHQL_OPTIONS_KEY, GRAPHQL_PATH, resolve } = require('../constants');
const { GraphQLNonNull, GraphQLList, GraphQLSchema, isOutputType } = require(GRAPHQL_PATH);

const createArgumentConfig = require('./arguments');
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 @@ -140,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 All @@ -156,7 +187,13 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {

typeNames.add(typeName);

const outputType = createOutputType({ ...graphql, ...schemaToCompile }, fields);
const outputType = createOutputType(
{
...graphql,
...schemaToCompile,
},
fields,
);

nestedTypes.set(schemaToCompile, outputType);

Expand All @@ -166,12 +203,17 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
return {
...final,
[key]: {
args: createArgumentConfig({ ...sourceArgs, ...args }, typeName),
args: createArgumentConfig(
{
...sourceArgs,
...args,
},
typeName,
),
type: isArray || isSourceAnArray ? GraphQLList(type) : type,
resolve: resolver
? async (...arg) => {
const resolved = await resolver(...arg);
console.log(resolved, requestedSchema);
return consolidate(resolved, requestedSchema);
}
: () => key,
Expand All @@ -182,7 +224,11 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
return final;
}, {});

const graphql = { description, deprecationReason, name };
const graphql = {
description,
deprecationReason,
name,
};

return {
...fullSchema,
Expand All @@ -191,7 +237,12 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
[name]: {
resolve,
args: createArgumentConfig(parentArgs, name),
type: createOutputType({ graphql }, compile(schemaContent)),
type: createOutputType(
{
graphql,
},
compile(schemaContent),
),
},
},
mutation: {
Expand All @@ -202,7 +253,12 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
[key]: {
resolve,
args: createArgumentConfig(args),
type: createOutputType({ graphql: graphqlSettings }, compile(schema)),
type: createOutputType(
{
graphql: graphqlSettings,
},
compile(schema),
),
},
}),
{},
Expand All @@ -211,7 +267,7 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
};
}, {});

return new GraphQLSchema({
const server = new GraphQLSchema({
...(Object.keys(query).length && {
query: createOutputType(
{
Expand All @@ -233,6 +289,13 @@ const compileSchema = async (filePattern = '!(node_modules)/**/schema.js') => {
),
}),
});

const client = compileClient(schemaFiles);

return {
client,
server,
};
} catch (e) {
console.error(e);
throw new Error('An unexpected error during schema compilation');
Expand Down
14 changes: 14 additions & 0 deletions compile/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@ const isSource = obj =>
typeof obj.source === 'object' &&
typeof obj.source.resolver === 'function';

const getOutputType = obj => {
const type = Array.isArray(obj) ? obj[0] : obj;
const isGraphQLOutput = isGraphQLOutputType(type);
const isOutput = isOutputType(type);
if (isGraphQLOutput || isOutput) {
return {
type,
};
}

return {};
};

module.exports = {
isNestedObject,
isGraphQLOutputType,
isGraphQLInputType,
isSource,
getOutputType,
};
Loading

0 comments on commit ff1c0e0

Please sign in to comment.