SHARE
TWEET

Untitled

a guest Mar 21st, 2019 39 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ConnectionHandler } from 'relay-runtime';
  2. import {
  3.     graphql,
  4.     requestSubscription
  5. } from 'react-relay'
  6. import environment from '../network';
  7.  
  8. const subscription = graphql`
  9.     subscription chatWithSubscription($id: String){
  10.         chatWith(id: $id){
  11.             textMessage
  12.             id
  13.             chatId
  14.             date
  15.             userType
  16.             translatedMessage
  17.         }
  18.     }
  19. `;
  20.  
  21. function chatWith(id) {
  22.     const variables = { id: id };
  23.     requestSubscription(environment, {
  24.         subscription,
  25.         variables,
  26.         onError: (error) => {
  27.             console.log(error, "error");
  28.         },
  29.         updater: (store) => {
  30.             console.log(store, "store");
  31.         }
  32.     });
  33. }
  34.  
  35. module.exports = chatWith;
  36.      
  37. module.exports = createPaginationContainer(
  38.   ChatMessages,
  39.   graphql`
  40.     fragment ChatMessages_query on Query
  41.       @argumentDefinitions(
  42.         count: { type: "Int", defaultValue: 10 }
  43.         cursor: { type: "String" }
  44.         chatId: { type: "ID" }
  45.       ) {
  46.       messages(
  47.         first: $count
  48.         after: $cursor
  49.         chatId: $chatId
  50.       ) @connection(key: "ChatMessages_messages") {
  51.         edges {
  52.           node {
  53.             id
  54.             chatId
  55.             userType
  56.             date
  57.             textMessage
  58.             translatedMessage
  59.           }
  60.           cursor
  61.         }
  62.         totalCount
  63.         pageInfo {
  64.           endCursor
  65.           hasNextPage
  66.         }
  67.       }
  68.     }
  69.   `,
  70.   {
  71.     direction: "forward" | "backward",
  72.     getConnectionFromProps(props) {
  73.       return props.query && props.query.messages;
  74.     },
  75.     // This is also the default implementation of `getFragmentVariables` if it isn't provided.
  76.     getFragmentVariables(prevVars, totalCount) {
  77.       return {
  78.         ...prevVars,
  79.         count: totalCount
  80.       };
  81.     },
  82.     getVariables(props, { count, cursor }, fragmentVariables) {
  83.       return {
  84.         count,
  85.         cursor,
  86.         chatId: fragmentVariables.chatId
  87.       };
  88.     },
  89.     query: graphql`
  90.       query ChatMessagesPaginationQuery(
  91.         $count: Int!
  92.         $cursor: String
  93.         $chatId: ID!
  94.       ) {
  95.         ...ChatMessages_query
  96.           @arguments(
  97.             count: $count
  98.             cursor: $cursor
  99.             chatId: $chatId
  100.           )
  101.       }
  102.     `
  103.   }
  104. );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top