Skip to content

Commit

Permalink
fix(deprecation): add DgraphClient.clientStubFromCloudEndpoint (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyusinghgaur authored Apr 21, 2021
1 parent 2ab48ce commit 0638ef7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ and understand how to run and work with Dgraph.
- [Intro](#intro)
- [Using the Synchronous Client](#using-the-synchronous-client)
* [Creating a Client](#creating-a-client)
* [Creating a Client for Slash Graphql Endpoint](#creating-a-client-for-slash-graphql-endpoint)
* [Creating a Client for Dgraph Cloud](#creating-a-client-for-dgraph-cloud)
* [Creating a Secure Client Using TLS](#creating-a-secure-client-using-tls)
* [Check Dgraph Version](#check-dgraph-version)
* [Login Using ACL](#login-using-acl)
Expand Down Expand Up @@ -160,12 +160,14 @@ DgraphStub stub3 = DgraphGrpc.newStub(channel3);
DgraphClient dgraphClient = new DgraphClient(stub1, stub2, stub3);
```

### Creating a Client for Slash Graphql Endpoint
### Creating a Client for Dgraph Cloud

If you want to connect to Dgraph running on your [Slash GraphQL](https://slash.dgraph.io) instance, then all you need is the URL of your Slash GraphQL endpoint and the API key. You can get a client using them as follows :
If you want to connect to Dgraph running on a [Dgraph Cloud](https://cloud.dgraph.io) instance,
then all you need is the URL of your Dgraph Cloud instance and the API key. You can get a client
with them as follows :

```java
DgraphStub stub = DgraphClient.clientStubFromSlashEndpoint("https://your-slash-instance.cloud.dgraph.io/graphql", "your-api-key");
DgraphStub stub = DgraphClient.clientStubFromCloudEndpoint("https://your-instance.cloud.dgraph.io/graphql", "your-api-key");
DgraphClient dgraphClient = new DgraphClient(stub);
```

Expand Down
23 changes: 19 additions & 4 deletions src/main/java/io/dgraph/DgraphClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,30 @@ public class DgraphClient {
* https://your-slash-instance.cloud.dgraph.io/graphql
* @param apiKey The API key used to connect to your Slash GraphQL instance.
* @return A new DgraphGrpc.DgraphStub object to be used with DgraphClient/DgraphAsyncClient.
* @deprecated This method will be removed in v21.07 release. For more details, see:
* https://discuss.dgraph.io/t/regarding-slash-cloud-dgraph-endpoints-in-the-clients/13492
* @deprecated This method will be removed in v21.07 release. Please use {@link
* #clientStubFromCloudEndpoint(String, String) clientStubFromCloudEndpoint} instead.
*/
@Deprecated
public static DgraphGrpc.DgraphStub clientStubFromSlashEndpoint(
String slashEndpoint, String apiKey) throws MalformedURLException {
String[] parts = new URL(slashEndpoint).getHost().split("[.]", 2);
return clientStubFromCloudEndpoint(slashEndpoint, apiKey);
}

/**
* Creates a gRPC stub that can be used to construct clients to connect with Dgraph Cloud.
*
* @param cloudEndpoint The url of the Dgraph Cloud instance. Example:
* https://your-instance.cloud.dgraph.io/graphql
* @param apiKey The API key used to connect to your Dgraph Cloud instance.
* @return A new {@link io.dgraph.DgraphGrpc.DgraphStub} object to be used with {@link
* DgraphClient}/{@link DgraphAsyncClient}.
* @since v21.03.1
*/
public static DgraphGrpc.DgraphStub clientStubFromCloudEndpoint(
String cloudEndpoint, String apiKey) throws MalformedURLException {
String[] parts = new URL(cloudEndpoint).getHost().split("[.]", 2);
if (parts.length < 2) {
throw new MalformedURLException("Invalid Slash URL.");
throw new MalformedURLException("Invalid Dgraph Cloud URL.");
}
String gRPCAddress = parts[0] + ".grpc." + parts[1];

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/io/dgraph/DgraphAsyncClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ public void testDelete() throws Exception {

@Test
public void testNewTransactionFromContext() {
DgraphProto.TxnContext ctx = DgraphProto.TxnContext.newBuilder().setStartTs(1234L).build();
DgraphProto.TxnContext ctx = DgraphProto.TxnContext.newBuilder().build();
try (AsyncTransaction txn = dgraphAsyncClient.newTransaction(ctx)) {
Response response = txn.query("{ result(func: uid(0x1)) { } }").join();
assertEquals(response.getTxn().getStartTs(), 1234L);
assertTrue(response.getTxn().getStartTs() > 0L);
}
}

@Test
public void testNewReadOnlyTransactionFromContext() {
DgraphProto.TxnContext ctx = DgraphProto.TxnContext.newBuilder().setStartTs(1234L).build();
DgraphProto.TxnContext ctx = DgraphProto.TxnContext.newBuilder().build();
try (AsyncTransaction txn = dgraphAsyncClient.newReadOnlyTransaction(ctx)) {
Response response = txn.query("{ result(func: uid(0x1)) { } }").join();
assertEquals(response.getTxn().getStartTs(), 1234L);
assertTrue(response.getTxn().getStartTs() > 0L);
}
}

Expand Down
17 changes: 8 additions & 9 deletions src/test/java/io/dgraph/DgraphClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ public void testDelete() {

@Test
public void testNewTransactionFromContext() {
TxnContext ctx = TxnContext.newBuilder().setStartTs(1234L).build();
TxnContext ctx = TxnContext.newBuilder().build();
try (Transaction txn = dgraphClient.newTransaction(ctx)) {
Response response = txn.query("{ result(func: uid(0x1)) { } }");
assertEquals(response.getTxn().getStartTs(), 1234L);
assertTrue(response.getTxn().getStartTs() > 0L);
}
}

@Test
public void testNewReadOnlyTransactionFromContext() {
TxnContext ctx = TxnContext.newBuilder().setStartTs(1234L).build();
TxnContext ctx = TxnContext.newBuilder().build();
try (Transaction txn = dgraphClient.newReadOnlyTransaction(ctx)) {
Response response = txn.query("{ result(func: uid(0x1)) { } }");
assertEquals(response.getTxn().getStartTs(), 1234L);
assertTrue(response.getTxn().getStartTs() > 0L);
}
}

Expand Down Expand Up @@ -181,19 +181,18 @@ public void testCheckVersion() {
}

@Test
public void testFromSlashEndpoint_ValidURL() {
public void testFromCloudEndpoint_ValidURL() {
try {
DgraphClient.clientStubFromSlashEndpoint(
"https://your-slash-instance.cloud.dgraph.io/graphql", "");
DgraphClient.clientStubFromCloudEndpoint("https://your-instance.cloud.dgraph.io/graphql", "");
} catch (MalformedURLException e) {
fail(e.getMessage());
}
}

@Test
public void testFromSlashEndpoint_InValidURL() {
public void testFromCloudEndpoint_InValidURL() {
try {
DgraphClient.clientStubFromSlashEndpoint("https://a-bad-url", "");
DgraphClient.clientStubFromCloudEndpoint("https://a-bad-url", "");
fail("Invalid Slash URL should not be accepted.");
} catch (MalformedURLException e) {
}
Expand Down

0 comments on commit 0638ef7

Please sign in to comment.