Skip to content

Commit

Permalink
Make mutations in Relay easier by including related tables #114
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-field committed Sep 18, 2016
1 parent b443473 commit d40b134
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/graphql/createTableType.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ const createColumnField = column => ({
resolve: source => source[column.name],
})

const createForeignKeyField = ({ nativeTable, nativeColumns, foreignTable, foreignColumns }) => ({
// TODO maybe move these ForeignKey methods to own file ?

export const createForeignKeyField = ({ nativeTable, nativeColumns, foreignTable, foreignColumns }) => ({
type: createTableType(foreignTable),
description:
`Queries a single ${foreignTable.getMarkdownTypeName()} node related to ` +
Expand All @@ -115,7 +117,7 @@ const createForeignKeyField = ({ nativeTable, nativeColumns, foreignTable, forei
),
})

const createForeignKeyReverseField = ({ nativeTable, nativeColumns, foreignTable, foreignColumns }) => ({
export const createForeignKeyReverseField = ({ nativeTable, nativeColumns, foreignTable, foreignColumns }) => ({
type: createConnectionType(nativeTable),
description:
`Queries and returns a set of ${nativeTable.getMarkdownTypeName()} ` +
Expand Down
13 changes: 10 additions & 3 deletions src/graphql/mutation/createInsertMutationField.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
GraphQLInputObjectType,
} from 'graphql'

import { fromPairs, identity, constant } from 'lodash'
import { fromPairs, identity, constant, camelCase } from 'lodash'
import { $$rowTable } from '../../symbols.js'
import SQLBuilder from '../../SQLBuilder.js'
import getColumnType from '../getColumnType.js'
import createTableType from '../createTableType.js'
import createTableType, { createForeignKeyField, createForeignKeyReverseField } from '../createTableType.js'
import { createTableEdgeType } from '../createConnectionType.js'
import { createTableOrderingEnum } from '../createConnectionArgs.js'
import getPayloadInterface from './getPayloadInterface.js'
Expand Down Expand Up @@ -87,7 +87,14 @@ const createPayloadType = table =>
node: output,
}),
},

// Add foreign key field references.
...fromPairs(
table.getForeignKeys().map(foreignKey => {
const columnNames = foreignKey.nativeColumns.map(({ name }) => name)
const name = `${foreignKey.foreignTable.name}_by_${columnNames.join('_and_')}`
return [camelCase(name), createForeignKeyField(foreignKey)]
})
),
...getPayloadFields(table.schema),
},
})
Expand Down

0 comments on commit d40b134

Please sign in to comment.