├── .gitignore ├── README.md ├── pom.xml └── src └── main ├── java └── io │ └── openliberty │ └── graphql │ └── sample │ ├── Conditions.java │ ├── PrecipType.java │ ├── UnknownLocationException.java │ ├── WeatherService.java │ ├── Wind.java │ └── client │ ├── JaxrsResource.java │ ├── TempAndPrecip.java │ └── WeatherApi.java ├── liberty └── config │ └── server.xml └── webapp ├── WEB-INF └── web.xml ├── graphiql.html ├── index.html ├── login.html └── logout.jsp /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .classpath 3 | .project 4 | .settings -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sample of MicroProfile GraphQL on OpenLiberty 2 | 3 | This is a simple demo of the MP GraphQL capabilities in OpenLiberty. 4 | 5 | To run, clone this repository, and then run: 6 | `mvn clean package liberty:run` 7 | 8 | Then browse to: 9 | http://localhost:9080/mpGraphQLSample/graphiql.html 10 | 11 | Then try a query like: 12 | ``` 13 | { 14 | currentConditions(location: "Paris") { 15 | dayTime 16 | hasPrecipitation 17 | temperatureF 18 | weatherText 19 | precipitationType 20 | } 21 | } 22 | ``` 23 | 24 | You should see results similar to (but with random values): 25 | ``` 26 | { 27 | "data": { 28 | "currentConditions": { 29 | "dayTime": true, 30 | "hasPrecipitation": true, 31 | "temperatureF": 8.796591509601903, 32 | "weatherText": "Overcast", 33 | "precipitationType": "SNOW" 34 | } 35 | } 36 | } 37 | ``` 38 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | io.openliberty 6 | mpGraphQLSample 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 3.1 11 | false 12 | RELEASE 13 | 1.8 14 | 1.8 15 | 16 | 17 | 18 | org.eclipse.microprofile 19 | microprofile 20 | 3.0 21 | pom 22 | provided 23 | 24 | 25 | org.eclipse.microprofile.graphql 26 | microprofile-graphql-api 27 | 1.0-M5 28 | provided 29 | 30 | 31 | io.smallrye 32 | smallrye-graphql-client 33 | 1.0.2 34 | 35 | 36 | io.smallrye 37 | smallrye-graphql-client-api 38 | 1.0.2 39 | 40 | 41 | org.slf4j 42 | slf4j-api 43 | 1.7.30 44 | 45 | 46 | org.slf4j 47 | slf4j-simple 48 | 1.7.30 49 | 50 | 51 | 52 | ${project.artifactId} 53 | 54 | 55 | 56 | liberty 57 | 58 | true 59 | 60 | 61 | 62 | 63 | ${basedir}/src/main/resources 64 | 65 | **/*.* 66 | 67 | 68 | 69 | true 70 | ${basedir}/src/main/liberty 71 | 72 | **/*.* 73 | 74 | 75 | 76 | ${project.artifactId} 77 | 78 | 79 | io.openliberty.tools 80 | liberty-maven-plugin 81 | ${openliberty.maven.version} 82 | true 83 | 84 | 85 | 9080 86 | 9443 87 | *=info 88 | ${project.build.finalName} 89 | 90 | mpGraphQLSample 91 | 92 | true 93 | mpGraphQL-1.0 94 | 95 | true 96 | all 97 | 90 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/Conditions.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2019, 2021 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | 12 | package io.openliberty.graphql.sample; 13 | 14 | import java.time.LocalDateTime; 15 | import java.time.ZoneOffset; 16 | 17 | public class Conditions { 18 | 19 | private final String location; 20 | private final LocalDateTime localObservationDateTime = LocalDateTime.now(); 21 | private String weatherText; 22 | private boolean hasPrecipitation; 23 | private PrecipType precipitationType; 24 | private boolean dayTime; 25 | private double temperatureC; 26 | private double temperatureF; 27 | private Wind wind; 28 | 29 | public Conditions(String location) { 30 | this.location = location; 31 | } 32 | 33 | public String getLocation() { 34 | return location; 35 | } 36 | 37 | public LocalDateTime getLocalObservationDateTime() { 38 | return localObservationDateTime; 39 | } 40 | 41 | public long getEpochTime() { 42 | return localObservationDateTime.toEpochSecond(ZoneOffset.UTC); 43 | } 44 | 45 | public String getWeatherText() { 46 | return weatherText; 47 | } 48 | 49 | public void setWeatherText(String weatherText) { 50 | this.weatherText = weatherText; 51 | } 52 | 53 | public boolean isHasPrecipitation() { 54 | return hasPrecipitation; 55 | } 56 | 57 | public void setHasPrecipitation(boolean hasPrecipitation) { 58 | this.hasPrecipitation = hasPrecipitation; 59 | } 60 | 61 | public PrecipType getPrecipitationType() { 62 | return precipitationType; 63 | } 64 | 65 | public void setPrecipitationType(PrecipType precipitationType) { 66 | this.precipitationType = precipitationType; 67 | } 68 | 69 | public boolean isDayTime() { 70 | return dayTime; 71 | } 72 | 73 | public void setDayTime(boolean dayTime) { 74 | this.dayTime = dayTime; 75 | } 76 | 77 | public double getTemperatureC() { 78 | return temperatureC; 79 | } 80 | 81 | public void setTemperatureC(double temperatureC) { 82 | this.temperatureC = temperatureC; 83 | } 84 | 85 | public double getTemperatureF() { 86 | return temperatureF; 87 | } 88 | 89 | public void setTemperatureF(double temperatureF) { 90 | this.temperatureF = temperatureF; 91 | } 92 | 93 | public Wind getWind() { 94 | return wind; 95 | } 96 | 97 | public void setWind(Wind wind) { 98 | this.wind = wind; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/PrecipType.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2019 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | 12 | package io.openliberty.graphql.sample; 13 | 14 | public enum PrecipType { 15 | RAIN, 16 | SNOW, 17 | SLEET; 18 | 19 | static PrecipType fromTempF(double tempF) { 20 | if (tempF > 40) { 21 | return RAIN; 22 | } 23 | if (tempF > 35) { 24 | return SLEET; 25 | } 26 | return SNOW; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/UnknownLocationException.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2020 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | 12 | package io.openliberty.graphql.sample; 13 | 14 | public class UnknownLocationException extends Exception { 15 | private static final long serialVersionUID = 10067834876L; 16 | 17 | public UnknownLocationException(String location) { 18 | super(location); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/WeatherService.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2019, 2021 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | 12 | package io.openliberty.graphql.sample; 13 | 14 | import java.util.HashMap; 15 | import java.util.LinkedList; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | import javax.annotation.security.DenyAll; 20 | import javax.annotation.security.RolesAllowed; 21 | import javax.enterprise.context.ApplicationScoped; 22 | 23 | import org.eclipse.microprofile.graphql.Description; 24 | import org.eclipse.microprofile.graphql.GraphQLApi; 25 | import org.eclipse.microprofile.graphql.GraphQLException; 26 | import org.eclipse.microprofile.graphql.Mutation; 27 | import org.eclipse.microprofile.graphql.Name; 28 | import org.eclipse.microprofile.graphql.Query; 29 | import org.eclipse.microprofile.graphql.Source; 30 | 31 | @GraphQLApi 32 | @ApplicationScoped 33 | public class WeatherService { 34 | 35 | Map currentConditionsMap = new HashMap<>(); 36 | 37 | 38 | @Query 39 | public Conditions currentConditions(@Name("location") String location) throws UnknownLocationException { 40 | if ("nowhere".equalsIgnoreCase(location)) { 41 | throw new UnknownLocationException(location); 42 | } 43 | return currentConditionsMap.computeIfAbsent(location, this::randomWeatherConditions); 44 | } 45 | 46 | @DenyAll 47 | @Query 48 | public List currentConditionsList(@Name("locations") List locations) 49 | throws UnknownLocationException, GraphQLException { 50 | 51 | List allConditions = new LinkedList<>(); 52 | for (String location : locations) { 53 | try { 54 | allConditions.add(currentConditions(location)); 55 | } catch (UnknownLocationException ule) { 56 | throw new GraphQLException(ule, allConditions); 57 | } 58 | } 59 | return allConditions; 60 | } 61 | 62 | @RolesAllowed("Role2") 63 | @Mutation 64 | @Description("Reset the cached conditions so that new queries will return newly randomized weather data." + 65 | "Returns number of entries cleared.") 66 | public int reset() { 67 | int cleared = currentConditionsMap.size(); 68 | currentConditionsMap.clear(); 69 | return cleared; 70 | } 71 | 72 | public double wetBulbTempF(@Source @Name("conditions") Conditions conditions) { 73 | // TODO: pretend like this is a really expensive operation 74 | System.out.println("wetBulbTempF for location " + conditions.getLocation()); 75 | return conditions.getTemperatureF() - 3.0; 76 | } 77 | 78 | private Conditions randomWeatherConditions(String location) { 79 | Conditions c = new Conditions(location); 80 | c.setDayTime(Math.random() > 0.5); 81 | c.setTemperatureF(Math.random() * 100); 82 | c.setTemperatureC( (c.getTemperatureF() - 30) / 2 ); 83 | c.setHasPrecipitation(Math.random() > 0.7); 84 | c.setPrecipitationType(c.isHasPrecipitation() ? PrecipType.fromTempF(c.getTemperatureF()) : null); 85 | c.setWeatherText(c.isHasPrecipitation() ? "Overcast" : "Sunny"); 86 | double windSpeed = Math.random() * 50; 87 | Wind wind = new Wind((int)(Math.random() * 360), windSpeed, windSpeed + (Math.random() * 50)); 88 | c.setWind(wind); 89 | return c; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/Wind.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2021 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | package io.openliberty.graphql.sample; 12 | 13 | public class Wind { 14 | 15 | private int direction; // valid directions are 0-359 16 | private double speed; 17 | private double gustSpeed; 18 | 19 | public Wind() {} 20 | 21 | public Wind(int direction, double speed, double gustSpeed) { 22 | this.direction = direction; 23 | this.speed = speed; 24 | this.gustSpeed = gustSpeed; 25 | } 26 | 27 | public int getDirection() { 28 | return direction; 29 | } 30 | 31 | public void setDirection(int direction) { 32 | this.direction = direction; 33 | } 34 | 35 | public double getSpeed() { 36 | return speed; 37 | } 38 | 39 | public void setSpeed(double speed) { 40 | this.speed = speed; 41 | } 42 | 43 | public double getGustSpeed() { 44 | return gustSpeed; 45 | } 46 | 47 | public void setGustSpeed(double gustSpeed) { 48 | this.gustSpeed = gustSpeed; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/client/JaxrsResource.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2020 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | package io.openliberty.graphql.sample.client; 12 | 13 | import javax.ws.rs.ApplicationPath; 14 | import javax.ws.rs.GET; 15 | import javax.ws.rs.Path; 16 | import javax.ws.rs.PathParam; 17 | import javax.ws.rs.Produces; 18 | import javax.ws.rs.core.Application; 19 | import javax.ws.rs.core.MediaType; 20 | 21 | import io.smallrye.graphql.client.typesafe.api.GraphQlClientBuilder; 22 | 23 | @ApplicationPath("/") 24 | @Path("test/client") 25 | @Produces(MediaType.TEXT_PLAIN) 26 | public class JaxrsResource extends Application{ 27 | 28 | @GET 29 | @Path("/{location}") 30 | public String tempAndPrecipFor(@PathParam("location") String location) { 31 | WeatherApi api = GraphQlClientBuilder.newBuilder().endpoint("http://localhost:9080/mpGraphQLSample/graphql").build(WeatherApi.class); 32 | TempAndPrecip tempAndPrecip = api.currentConditions(location); 33 | return tempAndPrecip.toString(); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/client/TempAndPrecip.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2020 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | package io.openliberty.graphql.sample.client; 12 | 13 | 14 | public class TempAndPrecip { 15 | 16 | private double temperatureF; 17 | private boolean hasPrecipitation; 18 | 19 | public double getTemperatureF() { 20 | return temperatureF; 21 | } 22 | 23 | public void setTemperatureF(double temperatureF) { 24 | this.temperatureF = temperatureF; 25 | } 26 | 27 | public boolean ishasPrecipitation() { 28 | return hasPrecipitation; 29 | } 30 | 31 | public void setHasPrecipitation(boolean hasPrecipitation) { 32 | this.hasPrecipitation = hasPrecipitation; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "Temperature (F): " + temperatureF + " Precipitation: " + hasPrecipitation; 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/io/openliberty/graphql/sample/client/WeatherApi.java: -------------------------------------------------------------------------------- 1 | // ****************************************************************************** 2 | // Copyright (c) 2020 IBM Corporation and others. 3 | // All rights reserved. This program and the accompanying materials 4 | // are made available under the terms of the Eclipse Public License v1.0 5 | // which accompanies this distribution, and is available at 6 | // http://www.eclipse.org/legal/epl-v10.html 7 | // 8 | // Contributors: 9 | // IBM Corporation - initial API and implementation 10 | // ****************************************************************************** 11 | package io.openliberty.graphql.sample.client; 12 | 13 | import org.eclipse.microprofile.graphql.Name; 14 | 15 | public interface WeatherApi { 16 | 17 | TempAndPrecip currentConditions(@Name("location")String location); 18 | } -------------------------------------------------------------------------------- /src/main/liberty/config/server.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | mpGraphQL-1.0 4 | mpMetrics-2.3 5 | jsp-2.3 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | MP GraphQL Sample App using made-up weather 8 | Application provides made-up weather conditions 9 | 10 | 11 | 12 | allResources 13 | Requiring authentication for all resources when appSecurity is enabled 14 | /* 15 | 16 | 17 | NONE 18 | 19 | 20 | Role1 21 | Role2 22 | 23 | 24 | 25 | 26 | GeneralResources 27 | /login.html 28 | GET 29 | 30 | 31 | AllUsers 32 | 33 | 34 | 35 | 36 | FORM 37 | 38 | /login.html 39 | /login.html 40 | 41 | 42 | 43 | 44 | Role1 45 | 46 | 47 | Role2 48 | 49 | 50 | AllUsers 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/main/webapp/graphiql.html: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 11 | 22 | 23 | 30 | 31 | 32 | 33 | 34 | 35 | 40 | 41 | 42 | 43 | 44 | 45 |
Loading...
46 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
Open GraphiQL
5 | 6 |
Log out
7 | 8 | -------------------------------------------------------------------------------- /src/main/webapp/login.html: -------------------------------------------------------------------------------- 1 | 2 | Login Page 3 | 4 |

Form Login

5 |
6 |
Enter user ID and password:
7 |
User ID
8 |
Password
9 |
10 |
11 | 12 | -------------------------------------------------------------------------------- /src/main/webapp/logout.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | <% 4 | request.logout(); 5 | %> 6 | 7 |
You are Sucessfully logged out...
8 | 9 | Go back To index page 10 | 11 | --------------------------------------------------------------------------------