Skip to content

Commit

Permalink
Pass selections to fetchImmediates
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Oct 2, 2016
1 parent 8d6980f commit fc52d39
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
28 changes: 19 additions & 9 deletions README.md
Expand Up @@ -90,9 +90,9 @@ const Root = new RootJoinType({
}
});

function fetchImmediatesFromQuery(request, {query}) {
const requestedColumns = request.selections.map(selection => selection.field.columnName);
const columnsToFields = fromPairs(zip(requestedColumns, request.selections.map(selection => selection.key)));
function fetchImmediatesFromQuery(selections, {query}) {
const requestedColumns = selections.map(selection => selection.field.columnName);
const columnsToFields = fromPairs(zip(requestedColumns, selections.map(selection => selection.key)));
return query.clone().select(requestedColumns).then(records =>
records.map(record =>
mapKeys(record, (value, name) => columnsToFields[name])
Expand Down Expand Up @@ -272,15 +272,15 @@ this is fine for relationships defined on the root.)

The remaining fields define a mapping from the GraphQL field to the database
column. This mapping is handled by `fetchImmediatesFromQuery()`.
The value of `request.requestedFields` in `fetchImmediates()`
is the fields that aren't defined as relationships
The value of `selections` in `fetchImmediates()`
is the requested fields that aren't defined as relationships
(using `single` or `many`) that were either explicitly requested in the
original GraphQL query, or are required as part of the join.

```javascript
function fetchImmediatesFromQuery(request, {query}) {
const requestedColumns = request.selections.map(selection => selection.field.columnName);
const columnsToFields = fromPairs(zip(requestedColumns, request.selections.map(selection => selection.key)));
function fetchImmediatesFromQuery(selections, {query}) {
const requestedColumns = selections.map(selection => selection.field.columnName);
const columnsToFields = fromPairs(zip(requestedColumns, selections.map(selection => selection.key)));
return query.clone().select(requestedColumns).then(records =>
records.map(record =>
mapKeys(record, (value, name) => columnsToFields[name])
Expand Down Expand Up @@ -342,7 +342,17 @@ Create a new `JoinType`.
Each field should either be an immediate field created by `field()`,
or a relationship to another type.

* `fetchImmediates(request, select)`
* `fetchImmediates(selections, select)`: a function to fetch the immediates for this type.
`selections` is a list of objects with the properties:
* `key`: the key of the selection.
This is the alias specified in the GraphQL request,
or the name of the field if no alias was specified.
* `field`: the field of the selection.
This will be one of the values passed in the `fields` property when
constructing the `JoinType`.

`fetchImmediates` should return a list of objects,
where each object has a property named after the key of each selection.

### Fields

Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Expand Up @@ -208,8 +208,7 @@ export class JoinType {
requestedImmediateSelections.concat(request.joinSelections).concat(joinToChildrenSelections),
"key"
);
const immediatesRequest = {...request, selections: immediateSelections};
return Promise.resolve(this.fetchImmediates(immediatesRequest, select)).then(results => {
return Promise.resolve(this.fetchImmediates(immediateSelections, select)).then(results => {
return Promise.all(map(relationshipSelections, fieldRequest => {
return fieldRequest.field.fetch(fieldRequest, select).then(children => {
results.forEach(result => {
Expand Down
4 changes: 2 additions & 2 deletions test/graphjoiner-sql.test.js
Expand Up @@ -38,8 +38,8 @@ exports.beforeEach = () => {
]).into("book"));
};

const fetchImmediatesFromQuery = tableName => (request, {query}) => {
const requestedColumns = request.selections.map(selection => tableName + "." + selection.field.columnName + " as " + selection.key);
const fetchImmediatesFromQuery = tableName => (selections, {query}) => {
const requestedColumns = selections.map(selection => tableName + "." + selection.field.columnName + " as " + selection.key);
return query.clone().select(requestedColumns);
};

Expand Down
4 changes: 2 additions & 2 deletions test/graphjoiner.test.js
Expand Up @@ -15,9 +15,9 @@ const allBooks = [
{id: 3, title: "Catch-22", authorId: 2}
];

function fetchImmediatesFromObj(request, objs) {
function fetchImmediatesFromObj(selections, objs) {
function readObj(obj) {
return fromPairs(request.selections.map(selection => [
return fromPairs(selections.map(selection => [
selection.key,
obj[selection.field.name]
]));
Expand Down
4 changes: 2 additions & 2 deletions test/graphql.test.js
Expand Up @@ -17,9 +17,9 @@ const allBooks = [
{id: 3, title: "Catch-22", authorId: 2}
];

function fetchImmediatesFromObj(request, objs) {
function fetchImmediatesFromObj(selections, objs) {
function readObj(obj) {
return fromPairs(request.selections.map(selection => [
return fromPairs(selections.map(selection => [
selection.key,
obj[selection.field.name]
]));
Expand Down

0 comments on commit fc52d39

Please sign in to comment.