Skip to content

Commit

Permalink
fix: correct typos and reorganize code
Browse files Browse the repository at this point in the history
Introduces style, grammar and comments corrections to make
code easier to understand.
  • Loading branch information
Fabs committed May 11, 2018
1 parent 326af7d commit 5e4ba2b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 67 deletions.
2 changes: 1 addition & 1 deletion packages/data-service/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "data-service",
"module": "./src/index.js",
"version": "0.0.1",
"version": "0.1.0",
"license": "Apache-2.0",
"peerDependencies": {
"graphql": "0.13.2",
Expand Down
9 changes: 6 additions & 3 deletions packages/data-service/src/__tests__/graphqlObservable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ const typeDefs = `
}
`;

const resolvers = {
const mockResolvers = {
Query: {
launched: (parent, args, ctx) => {
const { name } = args;

// act according with the type of filter
if (name === undefined) {
// When no filter is passed
return ctx.query;
} else if (typeof name == "string") {
} else if (typeof name === "string") {
// When the filter is a value
return ctx.query.map(els => els.filter(el => el.name === name));
} else {
// when the filter is an observable
return ctx.query
.combineLatest(name, (res, name) => [res, name])
.map(els => els[0].filter(el => el.name === els[1]));
Expand All @@ -43,7 +46,7 @@ const resolvers = {

const schema = makeExecutableSchema({
typeDefs,
resolvers
resolvers: mockResolvers
});

// jest helper who binds the marbles for you
Expand Down
125 changes: 62 additions & 63 deletions packages/data-service/src/graphqlObservable.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
import { Observable } from "rxjs/Observable";

// TODO: refactor this once it has a more compreehensive implementation
// TODO: refactor this once it has a more comprehensive implementation
// of the graphql api

// WARNING: This is NOT a spec complete graphql implementation
// https://facebook.github.io/graphql/October2016/

// eslint-disable-next-line import/prefer-default-export
export const graphqlObservable = (doc, schema, context) => {
const translateOperation = {
query: "Query"
};

const resolveStep = (typeMap, definition, context, parent) => {
if (definition.kind === "OperationDefinition") {
const nextTypeMap = typeMap[
translateOperation[definition.operation]
].getFields();

return definition.selectionSet.selections.reduce((acc, sel) => {
const resolvedObservable = resolveStep(nextTypeMap, sel, context);

const merger = (acc, emitted) => {
const propertyName = (definition.name || sel.name).value;

return { ...acc, [propertyName]: emitted };
};

return acc.combineLatest(resolvedObservable, merger);
}, Observable.of({}));
}

// Node Field
if (definition.kind === "Field" && definition.selectionSet !== undefined) {
const args = definition.arguments
.map(arg => {
if (arg.value.kind === "Variable") {
return { [arg.name.value]: context[arg.value.name.value] };
} else {
return { [arg.name.value]: arg.value.value };
}
})
.reduce(Object.assign, {});

const resolvedObservable = typeMap[definition.name.value].resolve(
parent,
args,
context,
null // that would be the info
);

return resolvedObservable.map(emittedResults => {
return emittedResults.map(result => {
return definition.selectionSet.selections.reduce((acc, sel) => {
acc[sel.name.value] = resolveStep(typeMap, sel, context, result);

return acc;
}, {});
});
});
}
const translateOperation = {
query: "Query"
};

function resolveStep(typeMap, definition, context, parent) {
if (definition.kind === "OperationDefinition") {
const nextTypeMap = typeMap[
translateOperation[definition.operation]
].getFields();

return definition.selectionSet.selections.reduce((acc, sel) => {
const resolvedObservable = resolveStep(nextTypeMap, sel, context);

// LeafField
if (definition.kind === "Field") {
return parent[definition.name.value];
}
const merger = (acc, emitted) => {
const propertyName = (definition.name || sel.name).value;

return Observable.throw(
new Error("graphqlObservable does not recognise ${definition.kind}")
return { ...acc, [propertyName]: emitted };
};

return acc.combineLatest(resolvedObservable, merger);
}, Observable.of({}));
}

// Node Field
if (definition.kind === "Field" && definition.selectionSet !== undefined) {
const args = definition.arguments
.map(
arg =>
(arg.value.kind === "Variable"
? { [arg.name.value]: context[arg.value.name.value] }
: { [arg.name.value]: arg.value.value })
)
.reduce(Object.assign, {});

const resolvedObservable = typeMap[definition.name.value].resolve(
parent,
args,
context,
null // that would be the info
);
};

return resolvedObservable.map(emittedResults => {
return emittedResults.map(result => {
return definition.selectionSet.selections.reduce((acc, sel) => {
acc[sel.name.value] = resolveStep(typeMap, sel, context, result);

return acc;
}, {});
});
});
}

// LeafField
if (definition.kind === "Field") {
return parent[definition.name.value];
}

return Observable.throw(
new Error("graphqlObservable does not recognise ${definition.kind}")
);
}

// eslint-disable-next-line import/prefer-default-export
export function graphqlObservable(doc, schema, context) {
if (doc.definitions.length === 1) {
return resolveStep(schema._typeMap, doc.definitions[0], context, null);
}
Expand All @@ -89,4 +88,4 @@ export const graphqlObservable = (doc, schema, context) => {

return acc.combineLatest(resolvedObservable, merger);
}, Observable.of({}));
};
}

0 comments on commit 5e4ba2b

Please sign in to comment.