Saturday, May 25, 2024

Java Microprofile GraphQL server using Open Liberty

 HOWTO

 

GitHub

 


 

Build models 

$ mvn -pl models install
[INFO] Scanning for projects...
[INFO] 
[INFO] ------< io.openliberty.guides:guide-microprofile-graphql-models >-------
[INFO] Building guide-microprofile-graphql-models 1.0-SNAPSHOT

[INFO] Installing /git/guide-microprofile-graphql/finish/models/target/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar to /home/dave/.m2/repository/io/openliberty/guides/guide-microprofile-graphql-models/1.0-SNAPSHOT/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar
[INFO] Installing /git/guide-microprofile-graphql/finish/models/pom.xml to /home/dave/.m2/repository/io/openliberty/guides/guide-microprofile-graphql-models/1.0-SNAPSHOT/guide-microprofile-graphql-models-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

Package WAR


$ mvn package


[INFO] Reactor Summary for guide-graphql-intro 1.0-SNAPSHOT:
[INFO] 
[INFO] guide-microprofile-graphql-models .................. SUCCESS [  0.794 s]
[INFO] guide-microprofile-graphql-system .................. SUCCESS [ 10.233 s]
[INFO] guide-microprofile-graphql-graphql ................. SUCCESS [  0.254 s]
[INFO] guide-graphql-intro ................................ SUCCESS [  0.001 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

Enable GraphQL UI  in server.xml

<server description="GraphQL service">
    <featureManager>
        <feature>restfulWS-3.1</feature>
        <feature>jsonb-3.0</feature>
        <feature>jsonp-2.1</feature>
        <feature>cdi-4.0</feature>
        <feature>mpConfig-3.1</feature>
        <feature>mpRestClient-3.0</feature>
        <feature>mpGraphQL-2.0</feature>
    </featureManager>

    <variable name="http.port" defaultValue="9082"/>
    <variable name="https.port" defaultValue="9445"/>

    <variable name="io.openliberty.enableGraphQLUI" value="true" />

    <webApplication location="guide-microprofile-graphql-graphql.war" contextRoot="/" />
    <httpEndpoint host="*" httpPort="${http.port}" 
        httpsPort="${https.port}" id="defaultHttpEndpoint"/>
</server>

 

GraphQL WAR

$ jar tvf ./graphql/target/guide-microprofile-graphql-graphql.war
     0 Sat May 25 07:38:34 CEST 2024 META-INF/
    81 Sat May 25 07:38:34 CEST 2024 META-INF/MANIFEST.MF
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/lib/
   433 Sat May 25 07:27:24 CEST 2024 WEB-INF/web.xml
  6366 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/GraphQLService.class
  1415 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/SystemClient.class
   568 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/UnknownUriException.class
  2268 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/UnknownUriExceptionMapper.class
  6693 Sat May 25 07:37:56 CEST 2024 WEB-INF/lib/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar
 15086 Sat May 25 07:27:24 CEST 2024 favicon.ico
  3150 Sat May 25 07:27:24 CEST 2024 index.html
  4503 Sat May 25 07:27:24 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-graphql/pom.xml
    97 Sat May 25 07:38:34 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-graphql/pom.properties

 

SystemWAR

dave@fedora:/git/guide-microprofile-graphql/finish$ jar tvf ./system/target/guide-microprofile-graphql-system.war
     0 Sat May 25 07:38:34 CEST 2024 META-INF/
    81 Sat May 25 07:38:34 CEST 2024 META-INF/MANIFEST.MF
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/lib/
   432 Sat May 25 07:27:24 CEST 2024 WEB-INF/web.xml
  1942 Sat May 25 07:38:30 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/SystemPropertiesResource.class
   444 Sat May 25 07:38:30 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/SystemApplication.class
  2398 Sat May 25 07:38:30 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/SystemMetricsResource.class
  6693 Sat May 25 07:37:56 CEST 2024 WEB-INF/lib/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar
  2461 Sat May 25 07:27:24 CEST 2024 index.html
  4007 Sat May 25 07:27:24 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-system/pom.xml
    96 Sat May 25 07:38:34 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-system/pom.properties

 

Build Docker images

dave@fedora:/git/guide-microprofile-graphql/finish$ docker images
REPOSITORY                    TAG                   IMAGE ID       CREATED          SIZE
graphql                       1.0-SNAPSHOT          727f633aa833   19 seconds ago   753MB
system                        1.0-java17-SNAPSHOT   6812f8c9c7c0   45 seconds ago   765MB
system                        1.0-java11-SNAPSHOT   915f228505ad   2 minutes ago    747MB

 Start containers 

$ ./scripts/startContainers.sh
fec411c8ccc1e46a593b48399907c93284a2b4b2db82b027d876fd53b1bbf10d
53de0a3383d827a43f6bafe185143f92f885a8a99d36e9c60bb167730399b44e
d175e2f72bfd024c4179659893e92dd566b1120948de122fd7d714d668e403cc
1b7b58a43e02e84c750137e416e55b800b36acac4d6f0c481ccde5b184ef6d8f
dave@fedora:/git/guide-microprofile-graphql/finish$ docker ps 
CONTAINER ID   IMAGE                        COMMAND                  CREATED         STATUS         PORTS                                                           NAMES
d175e2f72bfd   graphql:1.0-SNAPSHOT         "/opt/ol/helpers/run…"   7 seconds ago   Up 6 seconds   9080/tcp, 9443/tcp, 0.0.0.0:9082->9082/tcp, :::9082->9082/tcp   graphql
53de0a3383d8   system:1.0-java17-SNAPSHOT   "/opt/ol/helpers/run…"   7 seconds ago   Up 6 seconds   9080/tcp, 9443/tcp                                              system-java17
1b7b58a43e02   system:1.0-java11-SNAPSHOT   "/opt/ol/helpers/run…"   7 seconds ago   Up 6 seconds   9080/tcp, 9443/tcp                                              system-java11

Call GraphQL endpoint http://localhost:9082/graphql/schema.graphql

"Mutation root"
type Mutation {
  "Changes the note set for the system"
  editNote(hostname: String, note: String): Boolean!
}

"Query root"
type Query {
  "Gets information about the system"
  system(hostname: String): system!
  "Gets system load data from the systems"
  systemLoad(hostnames: [String]): [systemLoad]
}

"Information about a Java installation"
type java {
  vendorName: String
  version: String!
}

"System usage data"
type loadData {
  heapUsed: BigInteger!
  loadAverage: Float!
  nonHeapUsed: BigInteger!
}

"Information about a single system"
type system {
  hostname: String!
  java: java!
  note: String
  osArch: String
  osName: String
  osVersion: String
  systemMetrics: systemMetrics!
  username: String!
}

"Information of system usage"
type systemLoad {
  hostname: String!
  loadData: loadData
}

"System metrics"
type systemMetrics {
  heapSize: BigInteger!
  nonHeapSize: BigInteger!
  processors: Int!
}

Access GraphiQL at the http://localhost:9082/graphql-ui 

 

Query structure

# An example GraphQL query might look like:
#
#     {
#       field(arg: "value") {
#         subField
#       }
#     }

 

Query

query {
  system(hostname: "system-java11") {
    hostname
    username
    osArch
    osName
    osVersion
    systemMetrics {
      processors
      heapSize
      nonHeapSize
    }
    java {
      vendorName
      version
    }
  }
}


Response

{
  "data": {
    "system": {
      "hostname": "system-java11",
      "username": "default",
      "osArch": "amd64",
      "osName": "Linux",
      "osVersion": "6.8.10-200.fc39.x86_64",
      "systemMetrics": {
        "processors": 12,
        "heapSize": 4110352384,
        "nonHeapSize": -1
      },
      "java": {
        "vendorName": "IBM Corporation",
        "version": "11.0.22"
      }
    }
  }
}


No comments:

Post a Comment