Java Client - error while calling setSchema

I am using java client to set a schema
I am getting the below error

java.lang.RuntimeException: The alter encountered an execution exception:
Caused by: java.util.concurrent.ExecutionException: io.grpc.StatusRuntimeException: UNKNOWN: line 2 column 24: Expected new line after field declaration. Got @

code

ManagedChannel channel =
                ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext().build();
        DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
        DgraphClient client = new DgraphClient(stub);
        client.checkVersion();
        DgraphProto.Operation operation = DgraphProto.Operation
                .newBuilder()
                .setSchema("""
                type Account {
                  accountNumber: string @index(exact)
                }
                """)
                .build();

        client.alter(operation);
        channel.shutdownNow().awaitTermination(30, TimeUnit.SECONDS);

dependencies

implementation 'io.dgraph:dgraph4j:21.12.0'
	implementation 'io.netty:netty-tcnative-boringssl-static:2.0.31.Final'
	implementation 'io.grpc:grpc-netty:1.34.1'
	implementation 'com.google.code.gson:gson:2.11.0'
//	implementation 'io.grpc:grpc-protobuf:1.34.1'
//	implementation 'io.grpc:grpc-stub:1.34.1'
//	implementation 'io.grpc:grpc-netty:1.34.1'
	implementation 'com.google.protobuf:protobuf-java:3.5.1'

Dgraph version - 24.0.1

server logs doesn’t show any error

  31 server.go:383] Received ALTER op: schema:"type Account {\n  accountNumber: string @index(exact)\n}\n" 
2024-08-05 16:00:09 I0805 10:30:09.845840      31 server.go:1887] Got Alter request from: "192.168.65.1:30985"
2024-08-05 16:00:09 I0805 10:30:09.848505      31 server.go:529] ALTER op: schema:"type Account {\n  accountNumber: string @index(exact)\n}\n"  done

this is not a java error, if you read the error message you will find out that your schema is invalid. You are mixing type and predicate definitions.

1 Like

thank you very much. @vnium
Got it. the below one worked

String schema = """
        accountNumber: string @index(exact) .
        type Account {
          accountNumber
        }
        """;

slightly unrelated question :
so looks like Java client Dgraph4J supports only DQL ? does it support graphql ?

From what I know @antony, Java client just supports DQL. However, for GraphQL you could really use any GraphQL standard library.

1 Like

Thank you, got it. What I wanted was to run graphql queries from java app to mutate/query Dgraph data.
Since I am familiar with GraphQL, I wanted to avoid the DQL learning curve.