Skip to content

Commit

Permalink
feat(gql): fetch entities when event received
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Dec 11, 2018
1 parent 2de1b3f commit 09e0a5d
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 567 deletions.
28 changes: 15 additions & 13 deletions client/react-native/common/components/Screens/Chats/Add.js
Expand Up @@ -151,19 +151,21 @@ export default class ListScreen extends Component {
variables={queries.ContactList.defaultVariables}
fragment={fragments.ContactList}
alias='ContactList'
renderItem={props => (
<Item
{...props}
onPress={() => {
console.log(props)
const index = contactsID.lastIndexOf(props.data.id)
index < 0
? contactsID.push(props.data.id)
: contactsID.splice(index, 1)
this.setState({ contactsID })
}}
/>
)}
renderItem={props =>
props.data.status !== 42 ? (
<Item
{...props}
onPress={() => {
console.log(props)
const index = contactsID.lastIndexOf(props.data.id)
index < 0
? contactsID.push(props.data.id)
: contactsID.splice(index, 1)
this.setState({ contactsID })
}}
/>
) : null
}
/>
</Screen>
)
Expand Down
Expand Up @@ -34,7 +34,7 @@ const Item = fragments.Conversation(({ data, navigation }) => {
{isRead
? 'No new message'
: isInvite
? 'You have been invited'
? 'New conversation'
: 'You have a new message'}
</Text>
</Flex.Rows>
Expand Down
10 changes: 4 additions & 6 deletions client/react-native/common/graphql/mutations/ContactRequest.js
@@ -1,6 +1,6 @@
import { graphql } from 'react-relay'

import { commit, genericUpdater } from '../../relay'
import { commit } from '../../relay'
import { contact } from '../../utils'

const ContactRequestMutation = graphql`
Expand Down Expand Up @@ -42,11 +42,9 @@ export default context => (input, configs) =>
},
{
updater: (store, data) =>
genericUpdater(context.fragments.ContactList, 'ContactList', {
...context.queries.ContactList.defaultVariables,
count: undefined,
cursor: undefined,
})(store, data.ContactRequest),
context.updaters.contactList.forEach(updater =>
updater(store, data.ContactRequest)
),
...configs,
}
)
19 changes: 8 additions & 11 deletions client/react-native/common/graphql/mutations/ConversationCreate.js
Expand Up @@ -53,15 +53,12 @@ export default context => (input, configs) =>
ConversationCreateMutation,
'ConversationCreate',
input,
// {
// updater: (store, data) => {
// updaters.conversationList.forEach(updater =>
// updater(store)
// .add('ConversationEdge', data.ConversationCreate.id)
// .after()
// )
// },
// ...configs,
// }
configs
{
updater: (store, data) =>
console.log(data) ||
context.updaters.conversationList.forEach(updater =>
updater(store, data.ConversationCreate)
),
...configs,
}
)
19 changes: 7 additions & 12 deletions client/react-native/common/graphql/mutations/ConversationInvite.js
@@ -1,7 +1,6 @@
import { graphql } from 'react-relay'

import { commit } from '../../relay'
// import { updaters } from '..'

const ConversationInviteMutation = graphql`
mutation ConversationInviteMutation(
Expand Down Expand Up @@ -52,15 +51,11 @@ export default context => (input, configs) =>
ConversationInviteMutation,
'ConversationInvite',
input,
// {
// updater: (store, data) => {
// updaters.conversationList.forEach(updater =>
// updater(store)
// .add('ConversationEdge', data.ConversationInvite.id)
// .after()
// )
// },
// ...configs,
// }
configs,
{
updater: (store, data) =>
context.updaters.conversationList.forEach(updater =>
updater(store, data.ConversationInvite)
),
...configs,
}
)
27 changes: 27 additions & 0 deletions client/react-native/common/graphql/queries/Conversation.js
@@ -0,0 +1,27 @@
import { fetchQuery, graphql } from 'react-relay'

import { merge } from '../../helpers'

const query = graphql`
query ConversationQuery($id: ID!) {
GetConversation(id: $id) {
id
...Conversation
}
}
`

const defaultVariables = {
id: '',
}

export default context => ({
graphql: query,
defaultVariables,
fetch: variables =>
fetchQuery(
context.environment,
query,
merge([defaultVariables, variables])
),
})
Expand Up @@ -24,7 +24,7 @@ const query = graphql`
const defaultVariables = {
filter: null,
orderBy: 'updated_at',
orderDesc: false,
orderDesc: true,
count: 50,
cursor: '',
}
Expand Down
@@ -1,21 +1,27 @@
import { btoa } from 'b64-lite'

import EventStream from './EventStream'
import { parseEmbedded } from '../../helpers/json'
import { queries } from '..'
import EventStream from './EventStream'

export default context => ({
...EventStream(context),
subscribe: ({ iterator, updater }) =>
EventStream(context).subscribe({
updater:
updater &&
((store, data) => {
(async (store, data) => {
if (data.EventStream.kind === 301) {
const attributes = parseEmbedded(data.EventStream.attributes)
attributes.conversation.id = btoa(
'conversation:' + attributes.conversation.id
)
return updater && updater(store, attributes.conversation)
const conversation = await queries(context).Conversation.fetch({
id: attributes.conversation.id,
})

console.log('ConversationInvite: conversation: ', conversation)
return updater && updater(store, conversation)
}
}),
}),
Expand Down
@@ -1,14 +1,19 @@
import EventStream from './EventStream'
import { queries } from '..'

export default context => ({
...EventStream(context),
subscribe: ({ updater }) =>
EventStream(context).subscribe({
updater:
updater &&
((store, data) => {
(async (store, data) => {
if (data.EventStream.kind === 302) {
return updater(store, data.EventStream)
const conversation = await queries(context).Conversation.fetch({
id: data.EventStream.conversationId,
})
console.log('ConversationNewMessage: conversation:', conversation)
return updater(store, conversation)
}
}),
}),
Expand Down
10 changes: 9 additions & 1 deletion client/react-native/common/graphql/updaters/conversationList.js
@@ -1 +1,9 @@
export default context => []
import { genericUpdater } from '../../relay'

export default context => [
genericUpdater(context.fragments.ConversationList, 'ConversationList', {
...context.queries.ConversationList.defaultVariables,
count: undefined,
cursor: undefined,
}),
]
3 changes: 2 additions & 1 deletion client/react-native/common/relay/genericUpdater.js
Expand Up @@ -47,7 +47,8 @@ export default (fragment, alias, args) => {
ConnectionHandler.deleteNode(connection, data.id)
return
}

console.log(args)
// get all edges
const edges = connection.getLinkedRecords('edges')
const field = Case.camel(args.orderBy || args.sortBy || 'id')
const cursor =
Expand Down
6 changes: 3 additions & 3 deletions core/Makefile
Expand Up @@ -15,10 +15,10 @@ GENERATED_FILES = \
$(call rwildcard $(CODE_PATHS), *.gen.js) \
$(call rwildcard $(CODE_PATHS), *.gen.graphql)
GENERATION_TARGETS = \
.kind.generated \
.client.generated \
.pb.generated \
.gql.generated \
.kind.generated \
.pb.generated

CGO_LDFLAGS ?= -L/usr/local/opt/openssl/lib
CGO_CPPFLAGS ?= -I/usr/local/opt/openssl/include
Expand Down Expand Up @@ -166,7 +166,7 @@ dev-deps:

.gql.generated: $(PROTOS) .pb.generated
mkdir -p ./api/node/graphql
$(PROTOC) $(PROTOC_OPTS) --gotemplate_out=debug=false,all=true,template_dir=./api/node:./api/node ./api/node/*.proto
$(PROTOC) $(PROTOC_OPTS) --gotemplate_out=debug=false,all=true,template_dir=./api/node:./api/node ./api/node/service.proto
goimports -w ./api/node/*.gen.go ./api/node/graphql/*.gen.go
cd ./api/node/graphql && $(GQLGEN) -c gqlgen.gen.yml -v gen
cp ./api/node/graphql/service.gen.graphql ../client/react-native/common/schema.graphql
Expand Down
8 changes: 4 additions & 4 deletions core/api/client/jsonclient/berty.node.service.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09e0a5d

Please sign in to comment.