Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mutations in Relay easier by including related tables #115

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this? Thanks 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I haven't been able to get the test suite to run locally yet :/

FYI This is just using existing functionality from createTableType.js

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tim-field do you need any help in setting up the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ferdinandsalis Yes Please! :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tim-field you can get a hold of me on gitter most of the time. Just oben a direct message with me. Hope I can help.

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