Skip to content

Commit b7dce2a

Browse files
feat: Support for Slash GraphqQL endpoint (#158)
This PR adds a method on DgraphClient to easily construct a gRPC stub for Slash GraphQL endpoints. Co-authored-by: Abhimanyu Singh Gaur <[email protected]>
1 parent f1ca9ac commit b7dce2a

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and understand how to run and work with Dgraph.
2525
- [Intro](#intro)
2626
- [Using the Synchronous Client](#using-the-synchronous-client)
2727
* [Creating a Client](#creating-a-client)
28+
* [Creating a Client for Slash Graphql Endpoint](#creating-a-client-for-slash-graphql-endpoint)
2829
* [Creating a Secure Client Using TLS](#creating-a-secure-client-using-tls)
2930
* [Check Dgraph Version](#check-dgraph-version)
3031
* [Login Using ACL](#login-using-acl)
@@ -154,6 +155,15 @@ DgraphStub stub3 = DgraphGrpc.newStub(channel3);
154155
DgraphClient dgraphClient = new DgraphClient(stub1, stub2, stub3);
155156
```
156157

158+
### Creating a Client for Slash Graphql Endpoint
159+
160+
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 :
161+
162+
```java
163+
DgraphStub stub = DgraphClient.clientStubFromSlashEndpoint("https://your-slash-instance.cloud.dgraph.io/graphql", "your-api-key");
164+
DgraphClient dgraphClient = new DgraphClient(stub);
165+
```
166+
157167
### Creating a Secure Client using TLS
158168

159169
To setup a client using TLS, you could use the following code snippet. The server needs to be

src/main/java/io/dgraph/DgraphClient.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
import io.dgraph.DgraphProto.TxnContext;
2020
import io.dgraph.DgraphProto.Version;
2121

22+
import io.grpc.ManagedChannelBuilder;
23+
import io.grpc.Metadata;
24+
import io.grpc.stub.MetadataUtils;
25+
26+
import java.net.MalformedURLException;
27+
import java.net.URL;
28+
2229
/**
2330
* Implementation of a DgraphClient using grpc.
2431
*
@@ -27,18 +34,46 @@
2734
* @author Edgar Rodriguez-Diaz
2835
* @author Deepak Jois
2936
* @author Michail Klimenkov
37+
* @author Neeraj Battan
38+
* @author Abhimanyu Singh Gaur
3039
*/
3140
public class DgraphClient {
41+
private static final String gRPC_AUTHORIZATION_HEADER_NAME = "authorization";
3242

3343
private final DgraphAsyncClient asyncClient;
3444

45+
/**
46+
* Creates a gRPC stub that can be used to construct clients to connect with Slash GraphQL.
47+
*
48+
* @param slashEndpoint The url of the Slash GraphQL endpoint. Example:
49+
* https://your-slash-instance.cloud.dgraph.io/graphql
50+
* @param apiKey The API key used to connect to your Slash GraphQL instance.
51+
* @return A new DgraphGrpc.DgraphStub object to be used with DgraphClient/DgraphAsyncClient.
52+
*/
53+
public static DgraphGrpc.DgraphStub clientStubFromSlashEndpoint(
54+
String slashEndpoint, String apiKey) throws MalformedURLException {
55+
String[] parts = new URL(slashEndpoint).getHost().split("[.]", 2);
56+
if (parts.length < 2) {
57+
throw new MalformedURLException("Invalid Slash URL.");
58+
}
59+
String gRPCAddress = parts[0] + ".grpc." + parts[1];
60+
61+
Metadata metadata = new Metadata();
62+
metadata.put(Metadata.Key.of(gRPC_AUTHORIZATION_HEADER_NAME,
63+
Metadata.ASCII_STRING_MARSHALLER), apiKey);
64+
return MetadataUtils.attachHeaders(
65+
DgraphGrpc.newStub(
66+
ManagedChannelBuilder.forAddress(gRPCAddress, 443).useTransportSecurity().build()),
67+
metadata);
68+
}
69+
3570
/**
3671
* Creates a new client for interacting with a Dgraph store.
3772
*
3873
* <p>A single client is thread safe.
3974
*
4075
* @param stubs - an array of grpc stubs to be used by this client. The stubs to be used are
41-
* chosen at random per transaction.
76+
* chosen at random per transaction.
4277
*/
4378
public DgraphClient(DgraphGrpc.DgraphStub... stubs) {
4479
this.asyncClient = new DgraphAsyncClient(stubs);
@@ -142,7 +177,7 @@ public Version checkVersion() {
142177
* access JWT and a refresh JWT, which will be stored in the jwt field of this class, and used for
143178
* authorizing all subsequent requests sent to the server.
144179
*
145-
* @param userid the id of the user who is trying to login, e.g. Alice
180+
* @param userid the id of the user who is trying to login, e.g. Alice
146181
* @param password the password of the user
147182
*/
148183
public void login(String userid, String password) {

src/test/java/io/dgraph/DgraphClientTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@
1515
*/
1616
package io.dgraph;
1717

18-
import static org.testng.Assert.assertEquals;
19-
import static org.testng.Assert.assertTrue;
20-
2118
import com.google.gson.JsonObject;
2219
import com.google.gson.JsonParser;
2320
import com.google.protobuf.ByteString;
2421
import io.dgraph.DgraphProto.Mutation;
2522
import io.dgraph.DgraphProto.Operation;
2623
import io.dgraph.DgraphProto.Response;
2724
import io.dgraph.DgraphProto.TxnContext;
25+
26+
import java.net.MalformedURLException;
2827
import java.util.Collections;
2928
import java.util.Map;
29+
3030
import org.testng.annotations.BeforeMethod;
3131
import org.testng.annotations.Test;
3232

33+
import static org.testng.Assert.*;
34+
import static org.testng.Assert.fail;
35+
3336
/**
3437
* @author Edgar Rodriguez-Diaz
3538
* @author Deepak Jois
@@ -150,4 +153,23 @@ public void testCheckVersion() {
150153
assertTrue(v.getTag().length() > 0);
151154
assertEquals(v.getTag().charAt(0), 'v');
152155
}
156+
157+
@Test
158+
public void testFromSlashEndpoint_ValidURL() {
159+
try {
160+
DgraphClient.clientStubFromSlashEndpoint("https://your-slash-instance.cloud.dgraph" +
161+
".io/graphql", "");
162+
} catch (MalformedURLException e) {
163+
fail(e.getMessage());
164+
}
165+
}
166+
167+
@Test
168+
public void testFromSlashEndpoint_InValidURL() {
169+
try {
170+
DgraphClient.clientStubFromSlashEndpoint("https://a-bad-url", "");
171+
fail("Invalid Slash URL should not be accepted.");
172+
} catch (MalformedURLException e) {
173+
}
174+
}
153175
}

0 commit comments

Comments
 (0)