| 93 | } |
| 94 | |
| 95 | func Example_subscriptionWithCursor() { |
| 96 | // func TestSubCursor(t *testing.T) { |
| 97 | // query to fetch existing chat messages |
| 98 | // gql1 := `query { |
| 99 | // chats(first: 3, after: $cursor) { |
| 100 | // id |
| 101 | // body |
| 102 | // } |
| 103 | // chats_cursor |
| 104 | // }` |
| 105 | |
| 106 | // query to subscribe to new chat messages |
| 107 | gql2 := `subscription { |
| 108 | chats(first: 1, after: $cursor) { |
| 109 | id |
| 110 | body |
| 111 | } |
| 112 | }` |
| 113 | |
| 114 | conf := newConfig(&core.Config{ |
| 115 | DBType: dbType, |
| 116 | DisableAllowList: true, |
| 117 | SubsPollDuration: 1, |
| 118 | SecretKey: "not_a_real_secret", |
| 119 | }) |
| 120 | gj, err := core.NewGraphJin(conf, db) |
| 121 | if err != nil { |
| 122 | panic(err) |
| 123 | } |
| 124 | defer gj.Close() |
| 125 | |
| 126 | // struct to hold the cursor value from fetching the existing |
| 127 | // chat messages |
| 128 | // res := struct { |
| 129 | // Cursor string `json:"chats_cursor"` |
| 130 | // }{} |
| 131 | |
| 132 | // execute query for existing chat messages |
| 133 | // m1, err := gj.GraphQL(context.Background(), gql1, nil, nil) |
| 134 | // if err != nil { |
| 135 | // fmt.Println(err) |
| 136 | // return |
| 137 | // } |
| 138 | |
| 139 | // extract out the cursor `chats_cursor` to use in the subscription |
| 140 | // if err := json.Unmarshal(m1.Data, &res); err != nil { |
| 141 | // fmt.Println(err) |
| 142 | // return |
| 143 | // } |
| 144 | |
| 145 | // replace cursor value to make test work since it's encrypted |
| 146 | // v1 := cursorRegex.ReplaceAllString(string(m1.Data), "cursor_was_here") |
| 147 | // fmt.Println(v1) |
| 148 | |
| 149 | // create variables with the previously extracted cursor value to |
| 150 | // pass to the new chat messages subscription |
| 151 | // vars := json.RawMessage(`{ "cursor": "` + res.Cursor + `" }`) |
| 152 | vars := json.RawMessage(`{ "cursor": null }`) |