Skip to content

Commit

Permalink
feat: all resolvers working
Browse files Browse the repository at this point in the history
  • Loading branch information
zetlen committed Sep 10, 2018
1 parent bf214e6 commit 66ea456
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/upward-js/lib/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class Context {
return path.getFrom(this._data);
}

set(base, value) {
set(base, value, override) {
const isSet = constants.has(base) || this._data.hasOwnProperty(base);
if (isSet) {
if (isSet && !override) {
throw new Error(
`Attempted to reassign context property '${base}' to '${value}'. Context properties cannot be reassigned.`
);
Expand Down
4 changes: 3 additions & 1 deletion packages/upward-js/lib/ContextPath.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const debug = require('debug')('upward-js:ContextPath');
const illegalPathChars = /(^\.+)|[^\.\w\/]/;
const contextPathCache = new Map();
class ContextPath {
Expand Down Expand Up @@ -45,7 +46,8 @@ class ContextPath {
getFrom(obj) {
let current = obj;
for (const segment of this._segments) {
if (!current.hasOwnProperty(segment)) {
debug('traverse %o for %s', current, segment);
if (!current || !current.hasOwnProperty(segment)) {
return;
}
current = current[segment];
Expand Down
1 change: 1 addition & 0 deletions packages/upward-js/lib/ResolverVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ResolverVisitor {
const definedString = defined.toString();
debug(
'defined: %s is primitive, yielding to context.get("%s")',
definedString,
definedString
);
return this.context.get(definedString);
Expand Down
9 changes: 5 additions & 4 deletions packages/upward-js/lib/compiledResources/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { extname } = require('path');
const { extname, normalize } = require('path');
const CompiledResources = {
GraphQLDocument: require('./GraphQLDocument'),
MustacheTemplate: require('./MustacheTemplate')
Expand All @@ -14,9 +14,10 @@ for (const Resource of Object.values(CompiledResources)) {

module.exports = Object.assign(CompiledResources, {
forFileOfType(filenameOrExtension) {
const extension = filenameOrExtension.startsWith('.')
? filenameOrExtension
: extname(filenameOrExtension);
const normalized = normalize(filenameOrExtension);
const extension = normalized.startsWith('.')
? normalized
: extname(normalized);
return byExtension.get(extension);
}
});
1 change: 1 addition & 0 deletions packages/upward-js/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
IOAdapter: require('./IOAdapter'),
middleware: require('./middleware'),
server: require('./server')
};
5 changes: 4 additions & 1 deletion packages/upward-js/lib/resolvers/ConditionalResolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const debug = require('debug')('upward-js:ConditionalResolver');
const AbstractResolver = require('./AbstractResolver');

class ConditionalResolver extends AbstractResolver {
Expand Down Expand Up @@ -27,16 +28,18 @@ class ConditionalResolver extends AbstractResolver {
return this.tryMatchers(definition.when);
}
async tryMatchers([top, ...rest]) {
debug('matching %o with %d left to go', top, rest.length);
const regex = new RegExp(top.pattern);
const candidate = await this.visitor.context.get(top.matches);
debug('regex is %s, candidate is %s', regex, candidate);
const regexMatch = candidate.match(regex);

if (regexMatch) {
const match = regexMatch.reduce((contextMatch, group, index) => {
contextMatch[`$${index}`] = group;
return contextMatch;
}, {});
this.visitor.context.set('$match', match);
this.visitor.context.set('$match', match, true);
const yielded = await this.visitor.upward(top, 'use');
this.visitor.context.forget('$match');
return yielded;
Expand Down
4 changes: 3 additions & 1 deletion packages/upward-js/lib/resolvers/FileResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class FileResolver extends AbstractResolver {
}
}
debug('parse === %s, found %s to compile', parse, Resource.name);
return new Resource(fileText, this.visitor.io);
const rsrc = new Resource(fileText, this.visitor.io);
await rsrc.compile();
return rsrc;
}
}

Expand Down
28 changes: 21 additions & 7 deletions packages/upward-js/lib/resolvers/ServiceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ const debug = require('debug')('upward-js:ServiceResolver');
const { inspect } = require('util');
const { execute, makePromise } = require('apollo-link');
const { HttpLink } = require('apollo-link-http');
const { isPlainObject } = require('lodash');
const { isPlainObject, fromPairs } = require('lodash');
const AbstractResolver = require('./AbstractResolver');
const GraphQLDocument = require('../compiledResources/GraphQLDocument');
class ServiceResolver extends AbstractResolver {
static get resolverType() {
return 'service';
}
static get telltale() {
return 'endpoint';
return 'url';
}
async resolve(definition) {
const die = msg => {
Expand Down Expand Up @@ -59,12 +59,18 @@ class ServiceResolver extends AbstractResolver {
: {}
];

const [url, query, method, headers, variables] = await Promise.all(
toResolve
);
const [
url,
query,
method,
headers,
variableEntries
] = await Promise.all(toResolve);

const variables = fromPairs(variableEntries);

debug(
'url retrieved: "%s, query resolved: "%s, creating link',
'url retrieved: "%s", query resolved: "%s", creating link',
url,
query
);
Expand All @@ -86,9 +92,17 @@ class ServiceResolver extends AbstractResolver {
throw new Error(`Unknown type passed to 'query'.`);
}

debug('running query with %o', variables);

return makePromise(
execute(link, { query: await parsedQuery.render(), variables })
);
).then(({ data, errors }) => {
if (errors && errors.length > 0) {
throw new Error(errors[0].message);
} else {
return { data };
}
});
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/upward-js/lib/resolvers/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ test('Resolvers exports', () => {
).toBeTruthy();
expect(ResolversByType).toMatchInlineSnapshot(`
Object {
"conditional": [Function],
"file": [Function],
"inline": [Function],
"service": [Function],
"template": [Function],
}
`);
Expand Down
6 changes: 3 additions & 3 deletions packages/upward-js/lib/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
const ResolverList = [
require('./InlineResolver'),
require('./FileResolver'),
require('./TemplateResolver')
// require('./ServiceResolver'),
// require('./ConditionalResolver')
require('./TemplateResolver'),
require('./ServiceResolver'),
require('./ConditionalResolver')
];

const ResolversByType = ResolverList.reduce((out, Resolver) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/upward-spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ The above context lookup behaves at runtime as an instruction to wait until the
```yaml
uxbridges:
resolver: web
parse: json
resolver: service
method: POST
resolver: inline
inline: false
Expand All @@ -290,10 +289,11 @@ uxbridges:
resolver: inline
inline:
content-type: application/x-www-form-urlencoded
body:
query:
resolver: inline
inline:
name: uxbridge
inline: '{
characters @rest(type: "Character", path: )
}'
```
The above definition would assign an [`HttpResponse`](#http-response) to the `uxbridges` basename once it has run. An HTTP response has no `characters` property, so the example context lookup would resolve to the empty string. However, an HTTP response does have a `body` property, so the lookup `uxbridges.body.characters[0].name` would resolve to `Kevin Uxbridge`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
status: 200
headers:
inline:
content-type:
when:
matches:
content-disposition:
when:
matches: headers.content-type
pattern: text/html
use:
inline:
inline
default:
inline:
attachment

0 comments on commit 66ea456

Please sign in to comment.