Commit 95ebaf2a authored by Caner Candan's avatar Caner Candan

feat(assembler): remove the self-relation parent/child and replace by prop-occurrence relation, fix

parent db77fb80
Pipeline #18759660 skipped with stages
......@@ -18,7 +18,7 @@ import { FormControl } from 'material-ui/Form'
import Input, { InputLabel, InputAdornment } from 'material-ui/Input'
import {
SessionQuery, FlatOccurrencesQuery, OccurrenceQuery,
SessionQuery, OccurrencesQuery, OccurrenceQuery,
DeleteCheckedOccurrencesMutation, UpdateSessionMutation, UpdatePropMutation,
PropSubscription,
} from '../../graphql'
......@@ -335,7 +335,7 @@ NodeProp.propTypes = {
}))
@withWidth()
@SessionQuery
@FlatOccurrencesQuery
@OccurrencesQuery
@DeleteCheckedOccurrencesMutation
@UpdateSessionMutation
export default class RightDrawerComponent extends Component {
......
export { default as OccurrenceChildrenFieldsFragment } from './occurrence-children-fields.fragment'
export { default as OccurrenceChildrenRecusiveFragment } from './occurrence-children-recursive.fragment'
export { default as OccurrenceFieldsFragment } from './occurrence-fields.fragment'
export { default as OccurrenceRecusiveFragment } from './occurrence-recursive.fragment'
export { default as PropFieldsFrgament } from './prop-fields.fragment'
export { default as SessionFieldsFragment } from './session-fields.fragment'
import { gql as AssemblerGql } from 'apollo-boost'
import PropFieldsFragment from './prop-fields.fragment'
export default AssemblerGql`
fragment OccurrenceChildrenFieldsFragment on Occurrence {
id
__typename
component
parent {
id
__typename
component
}
session {
id
__typename
}
isChecked
props {
...PropFieldsFragment
}
focusedProp {
...PropFieldsFragment
}
propParent {
...PropFieldsFragment
}
}
${PropFieldsFragment}
`
import { gql as AssemblerGql } from 'apollo-boost'
import OccurrenceChildrenFieldsFragment from './occurrence-children-fields.fragment'
export default AssemblerGql`
fragment OccurrenceChildrenRecursiveFragment on Occurrence {
children {
...OccurrenceChildrenFieldsFragment
children {
...OccurrenceChildrenFieldsFragment
children {
...OccurrenceChildrenFieldsFragment
children {
...OccurrenceChildrenFieldsFragment
}
}
}
}
}
${OccurrenceChildrenFieldsFragment}
`
import { gql as AssemblerGql } from 'apollo-boost'
import OccurrenceChildrenFieldsFragment from './occurrence-children-fields.fragment'
import PropFieldsFragment from './prop-fields.fragment'
export default AssemblerGql`
fragment OccurrenceFieldsFragment on Occurrence {
...OccurrenceChildrenFieldsFragment
children {
id
__typename
component
parent {
...PropFieldsFragment
}
session {
id
__typename
}
isChecked
props {
...PropFieldsFragment
}
focusedProp {
...PropFieldsFragment
}
}
${OccurrenceChildrenFieldsFragment}
${PropFieldsFragment}
`
import { gql as AssemblerGql } from 'apollo-boost'
import OccurrenceFieldsFragment from './occurrence-fields.fragment'
import PropFieldsFragment from './prop-fields.fragment'
export default AssemblerGql`
fragment OccurrenceRecursiveFragment on Occurrence {
...OccurrenceFieldsFragment
props {
...PropFieldsFragment
children {
...OccurrenceFieldsFragment
props {
...PropFieldsFragment
children {
...OccurrenceFieldsFragment
props {
...PropFieldsFragment
children {
...OccurrenceFieldsFragment
props {
...PropFieldsFragment
children {
...OccurrenceFieldsFragment
props {
...PropFieldsFragment
}
}
}
}
}
}
}
}
}
}
${OccurrenceFieldsFragment}
${PropFieldsFragment}
`
......@@ -8,7 +8,7 @@ export default AssemblerGql`
type
oneOf
value
child {
children {
id
__typename
}
......
......@@ -4,7 +4,6 @@ import { gql as AssemblerGql } from 'apollo-boost'
import OccurrenceFieldsFragment from '../fragments/occurrence-fields.fragment'
import { query as OccurrencesQuery } from '../queries/occurrences.query'
import { query as FlatOccurrencesQuery } from '../queries/flat-occurrences.query'
const debug = buildDebug('react-packages:packages:rct-assembler-component:create-occurrence-mutation')
......@@ -13,14 +12,12 @@ export const mutation = AssemblerGql`
$sessionId: ID!
$component: String!
$parentId: ID
$propParentId: ID
$props: [PropCreateWithoutOccurrenceInput!]
) {
createOccurrence(
sessionId: $sessionId
component: $component
parentId: $parentId
propParentId: $propParentId
props: $props
) {
...OccurrenceFieldsFragment
......@@ -38,7 +35,6 @@ export const config = {
const variablesFields = {}
const optimisticFields = {
parent: null,
propParent: null,
}
// parent parent id to pass to mutation variables
if (focusedOccurrence && focusedOccurrence.id && focusedOccurrence.focusedProp) {
......@@ -46,31 +42,22 @@ export const config = {
const {
id, name, type, oneOf, value,
} = focusedProp
if (name === 'children') {
// if prop children selected, the new occurrence is assigned as a
// direct child of the parent occurrence
variablesFields.parentId = focusedOccurrence.id
optimisticFields.parent = {
id: focusedOccurrence.id,
__typename: 'Occurrence',
component: focusedOccurrence.component,
}
} else if (type === 'node') {
if (type === 'node' || type === 'any') {
// if a non-children prop is selected and it's a node,
// the new occurrence is assigned to it
variablesFields.propParentId = focusedOccurrence.id
optimisticFields.propParent = {
variablesFields.parentId = focusedOccurrence.focusedProp.id
optimisticFields.parent = {
id,
__typename: 'Prop',
name,
type,
oneOf,
value,
child: null,
children: [],
}
}
}
// prepare props object to pass to mutatiton variables
// prepare props object to pass to mutation variables
if (Object.keys(componentProps).length) {
variablesFields.props = Object.keys(componentProps).map((name) => {
const { type: { name: type, value: typeValue }, defaultValue } = componentProps[name]
......@@ -108,7 +95,6 @@ export const config = {
__typename: 'Session',
},
isChecked: false,
children: [],
props: [],
focusedProp: focusedOccurrence && focusedOccurrence.focusedProp,
...optimisticFields,
......@@ -117,28 +103,16 @@ export const config = {
update: (proxy, { data: { createOccurrence } }) => {
debug(createOccurrence)
// add new item to flat occurrences
const foData = proxy.readQuery({
query: FlatOccurrencesQuery,
variables: { sessionId },
})
// TODO: if parentId non-null add the item as a child of its parent
foData.occurrences.push(createOccurrence)
proxy.writeQuery({
query: FlatOccurrencesQuery,
variables: { sessionId },
data: foData,
})
// add new item to occurrences
const oData = proxy.readQuery({
const data = proxy.readQuery({
query: OccurrencesQuery,
variables: { sessionId },
})
// TODO: only add item if `parentId` is null
oData.occurrences.push(createOccurrence)
// TODO: if parentId non-null add the item as a child of its parent
data.occurrences.push(createOccurrence)
proxy.writeQuery({
query: OccurrencesQuery,
variables: { sessionId },
data: oData,
data,
})
},
})
......
......@@ -5,7 +5,6 @@ import { gql as AssemblerGql } from 'apollo-boost'
import OccurrenceFieldsFragment from '../fragments/occurrence-fields.fragment'
import { query as SessionQuery } from '../queries/session.query'
import { query as OccurrencesQuery } from '../queries/occurrences.query'
import { query as FlatOccurrencesQuery } from '../queries/flat-occurrences.query'
const debug = buildDebug('react-packages:packages:rct-assembler-component:delete-checked-occurrences-mutation')
......@@ -29,12 +28,12 @@ export const config = {
update: (proxy) => {
// remove isChecked item
let { occurrences } = proxy.readQuery({
query: FlatOccurrencesQuery,
query: OccurrencesQuery,
variables: { sessionId },
})
occurrences = occurrences.filter(({ isChecked }) => !isChecked)
proxy.writeQuery({
query: FlatOccurrencesQuery,
query: OccurrencesQuery,
variables: { sessionId },
data: { occurrences },
})
......@@ -50,9 +49,11 @@ export const config = {
data: { session },
})
},
refetchQueries: [
{ query: OccurrencesQuery, variables: { sessionId } },
],
// TODO: disabling this part for now and need to check if the canvas is
// updated accordingly
// refetchQueries: [
// { query: OccurrencesQuery, variables: { sessionId } },
// ],
}),
props: ({ mutate }) => ({
deleteCheckedOccurrences: async () => {
......
......@@ -38,7 +38,7 @@ export const config = {
type,
oneOf,
value,
child: null,
children: [],
},
},
})
......
import buildDebug from 'debug'
import { graphql } from 'react-apollo'
import { gql as AssemblerGql } from 'apollo-boost'
import OccurrenceChildrenFieldsFragment from '../fragments/occurrence-children-fields.fragment'
const debug = buildDebug('react-packages:packages:rct-assembler-component:flat-occurrences-query')
export const query = AssemblerGql`
query($sessionId: ID!) {
occurrences(
sessionId: $sessionId
includeChildren: true
) {
...OccurrenceChildrenFieldsFragment
}
}
${OccurrenceChildrenFieldsFragment}
`
export const config = {
options: ({ sessionId }) => ({ variables: { sessionId } }),
props: ({
data: {
loading, error, occurrences, ...flatOccurrencesDataFields
},
}) => {
debug('query:flatOccurrences()')
if (loading) { return { loading, flatOccurrencesDataFields } }
if (error) { return { error } }
return { loading: false, occurrences, flatOccurrencesDataFields }
},
}
export default graphql(query, config)
export { default as FlatOccurrencesQuery } from './flat-occurrences.query'
export { default as OccurrencesQuery } from './occurrences.query'
export { default as SessionQuery } from './session.query'
export { default as OccurrenceQuery } from './occurrence.query'
......@@ -2,20 +2,23 @@ import buildDebug from 'debug'
import { graphql } from 'react-apollo'
import { gql as AssemblerGql } from 'apollo-boost'
import OccurrenceChildrenFieldsFragment from '../fragments/occurrence-children-fields.fragment'
import OccurrenceChildrenRecursiveFragment from '../fragments/occurrence-children-recursive.fragment'
import OccurrenceFieldsFragment from '../fragments/occurrence-fields.fragment'
import OccurrenceRecursiveFragment from '../fragments/occurrence-recursive.fragment'
const debug = buildDebug('react-packages:packages:rct-assembler-component:occurrences-query')
export const query = AssemblerGql`
query($sessionId: ID!) {
occurrences(sessionId: $sessionId) {
...OccurrenceChildrenFieldsFragment
...OccurrenceChildrenRecursiveFragment
occurrences(
sessionId: $sessionId
includeChildren: true
) {
...OccurrenceFieldsFragment
...OccurrenceRecursiveFragment
}
}
${OccurrenceChildrenFieldsFragment}
${OccurrenceChildrenRecursiveFragment}
${OccurrenceFieldsFragment}
${OccurrenceRecursiveFragment}
`
export const config = {
options: ({ sessionId }) => ({ variables: { sessionId } }),
......
......@@ -18,14 +18,6 @@ export const document = AssemblerGql`
__typename
component
isChecked
parent {
id
__typename
}
children {
id
__typename
}
session {
id
__typename
......@@ -37,6 +29,10 @@ export const document = AssemblerGql`
type
oneOf
value
children {
id
__typename
}
}
focusedProp {
id
......@@ -45,19 +41,26 @@ export const document = AssemblerGql`
type
oneOf
value
children {
id
__typename
}
}
propParent {
parent {
id
__typename
name
type
oneOf
value
children {
id
__typename
}
}
}
}
}
`
}`
export const variables = {}
// eslint-disable-next-line no-shadow
export const updateQuery = (previousResult, { subscriptionData, variables }) => {
......
......@@ -20,6 +20,10 @@ export const document = AssemblerGql`
type
oneOf
value
children {
id
__typename
}
}
}
}
......
......@@ -6,7 +6,7 @@ import withWidth from 'material-ui/utils/withWidth'
import classNames from 'classnames'
import {
SessionQuery, OccurrencesQuery, FlatOccurrencesQuery,
SessionQuery, OccurrencesQuery,
CreateOccurrenceMutation,
OccurrenceSubscription,
} from './graphql'
......@@ -71,7 +71,6 @@ const drawerWidth = 240
@withWidth()
@SessionQuery
@OccurrencesQuery
@FlatOccurrencesQuery
@CreateOccurrenceMutation
export default class RctAssemblerComponent extends Component {
static propTypes = {
......@@ -90,8 +89,6 @@ export default class RctAssemblerComponent extends Component {
// eslint-disable-next-line react/forbid-prop-types,
occurrencesDataFields: PropTypes.object,
// eslint-disable-next-line react/forbid-prop-types,
flatOccurrencesDataFields: PropTypes.object,
// eslint-disable-next-line react/forbid-prop-types,
subscriptionParam: PropTypes.any,
// TBD
}
......@@ -101,7 +98,6 @@ export default class RctAssemblerComponent extends Component {
occurrences: [],
loading: true,
occurrencesDataFields: {},
flatOccurrencesDataFields: {},
subscriptionParam: undefined,
}
......@@ -109,25 +105,20 @@ export default class RctAssemblerComponent extends Component {
debug('componentWillReceiveProps')
if (!nextProps.loading) {
// check for existing subscription
if (this.occurrencesUnsubscribe || this.flatOccurrencesUnsubscribe) {
if (this.occurrencesUnsubscribe) {
// check if props have changed and, if necessary,
// stop the subscription
if (this.props.subscriptionParam === nextProps.subscriptionParam) {
return
}
this.occurrencesUnsubscribe()
this.flatOccurrencesUnsubscribe()
}
const { occurrencesDataFields, flatOccurrencesDataFields, sessionId } = nextProps
const { occurrencesDataFields, sessionId } = nextProps
// subscribe
this.occurrencesUnsubscribe = occurrencesDataFields.subscribeToMore({
...OccurrenceSubscription,
variables: { sessionId },
})
this.flatOccurrencesUnsubscribe = flatOccurrencesDataFields.subscribeToMore({
...OccurrenceSubscription,
variables: { sessionId },
})
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment