All Projects → phillip-kruger → graphql-example

phillip-kruger / graphql-example

Licence: Apache-2.0 license
A MicroProfile GraphQL Example

Programming Languages

java
68154 projects - #9 most used programming language
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to graphql-example

microprofile1.2-samples
Eclipse MicroProfile 1.2 Samples
Stars: ✭ 22 (-50%)
Mutual labels:  wildfly, microprofile
application-modernization-javaee-quarkus
Application Modernization Sample - From Java EE (2010) to Cloud-Native (2021)
Stars: ✭ 99 (+125%)
Mutual labels:  microprofile, quarkus
microprofile-samples
Showcases typical use cases of Quarkus Applications
Stars: ✭ 27 (-38.64%)
Mutual labels:  quarkus, smallrye
optaplanner-quickstarts
OptaPlanner quick starts for AI optimization: many use cases shown in many different technologies.
Stars: ✭ 226 (+413.64%)
Mutual labels:  quarkus
Trotsky
a cloud native ✏️ blog platform 一个正在开发中的云原生笔记平台
Stars: ✭ 27 (-38.64%)
Mutual labels:  quarkus
quarkus-resteasy-problem
Unified error responses for Quarkus REST APIs via Problem Details for HTTP APIs (RFC7807). Supports Quarkus 2.0+ and 1.4+
Stars: ✭ 36 (-18.18%)
Mutual labels:  quarkus
puppet-jboss
Installs JBoss EAP and WildFly application servers and manage their resources and applications in either a domain or a stand-alone mode
Stars: ✭ 15 (-65.91%)
Mutual labels:  wildfly
openshift-quickstart
Developer Workshops related to the Java development on OpenShift
Stars: ✭ 19 (-56.82%)
Mutual labels:  quarkus
heterogeneous-microservices
Implementation of the same simple microservice on different frameworks
Stars: ✭ 43 (-2.27%)
Mutual labels:  quarkus
getting-started-with-microprofile
📙 Everything you need to know about MicroProfile
Stars: ✭ 35 (-20.45%)
Mutual labels:  microprofile
smallrye-fault-tolerance
SmallRye implementation of MicroProfile Fault Tolerance: annotation-based timeouts, circuit breakers, fallbacks and more
Stars: ✭ 47 (+6.82%)
Mutual labels:  microprofile
quarkus-amazon-lambda
A simple demo application showing quarkus usage with Amazon Lambdas
Stars: ✭ 22 (-50%)
Mutual labels:  quarkus
x-ray
Statistics and analytics Java EE 6 software for blogs (tested with roller) and webapps. It is a vanilla Java EE 6 (REST/JAX-RS, CDI, EJB, JPA) app, tested on Glassfish v3.1, built with Maven 3 / hudson and developed with NetBeans 7. X-ray is the sample app of the "Real World Night Hacks" book.
Stars: ✭ 27 (-38.64%)
Mutual labels:  microprofile
quarkus-workshops
Hosts Quarkus related workshops
Stars: ✭ 135 (+206.82%)
Mutual labels:  quarkus
boost
Boost Maven and Gradle plugins for MicroProfile development
Stars: ✭ 27 (-38.64%)
Mutual labels:  microprofile
generator-jvm
Generate JVM (java, kotlin, scala) project with gradle / maven / sbt build systems and docker / docker-compose for rapid development
Stars: ✭ 40 (-9.09%)
Mutual labels:  microprofile
smallrye-opentracing
An MicroProfile-OpenTracing implementation
Stars: ✭ 17 (-61.36%)
Mutual labels:  microprofile
Certified-Rancher-Operator-Thai
มาเรียนรู้ Kuberntes แบบ On-Premise และ Architecture ของ Rancher ที่ใช้ในการจัดการ Kubernetes Cluster เพื่อนำสู่ Certified Kubernetes Administrator และ Certified Rancer Operator
Stars: ✭ 78 (+77.27%)
Mutual labels:  quarkus
native-java-examples
Native Java Apps with Micronaut, Quarkus, and Spring Boot
Stars: ✭ 44 (+0%)
Mutual labels:  quarkus
migrate-Java-EE-app-to-azure
Migrate an existing Java EE workload to Azure
Stars: ✭ 12 (-72.73%)
Mutual labels:  wildfly

MicroProfile GraphQL Example

This is an example of the MicroProfile GraphQL API using the SmallRye Implementation. It's done as part of these blog posts:

and these presentations:

The services are exposed with both REST and GraphQL for comparison.

Person example

This example expose person data as well as scores that the person got for certain activities.

Running in Quarkus

(generate from https://code.quarkus.io/)

cd quarkus-example
mvn clean install quarkus:dev

Running in Wildfly

cd wildfly-example
mvn clean install wildfly:run

This will start the application on port 8080.

Testing

Go to http://localhost:8080 to test the application.

  • Click on the 'REST' link to open Swagger UI to test the JAX-RS services.
  • Click on the 'GraphQL' link to open GraphiQL UI to test the MicroProfile GraphQL service.

To stop the application, ctrl-c in the maven session.

screenshot

Examples

See the model in the JavaDoc (target/apidocs/index.html)

Demo 1 : MicroProfile GraphQL vs JAX-RS

REST

Schema: http://localhost:8080/openapi

curl -X GET "http://localhost:8080/rest/profile/1" -H  "accept: application/json"
GraphQL

Schema: http://localhost:8080/graphql/schema.graphql

{
  person(id:1){
    names
    surname
  }
}

Demo 2: Stitching

{
  person(id:1){
    names
    surname
    scores{
      name
      value
    }
  }
}

in the log file:

======= Getting person [1] =======
======= Getting scores [512-46-5065] =======

without score

{
  person(id:1){
    names
    surname
  }
}

in the log file:

======= Getting person [1] =======

Demo 3: Batch

{
  people{
    names
    scores{
      name
    }
  }
}

in the log file:

======= Getting scores [797-95-4822, 373-95-3047, 097-87-6795, 347-01-8880, 733-86-4423, 560-99-2165, 091-07-5401, 539-70-2014, 029-18-5986, 287-58-0690] =======

Demo 4: More than one request

{
  person1:person(id:1){
    surname
    scores{
      name
      value
    }
  }
  person2:person(id:2){
    surname
  }
}

or more than one query:

@Query
public Integer getRandomNumber(long seed){
    Random random = new Random(seed);
    return random.nextInt();
}
{
  person1:person(id:1){
    surname
    scores{
      name
      value
    }
  }
  randomId:randomNumber(seed:11)
}

Demo 5: Collections

{
  people {
    surname
  }
}

Demo 6: JsonB Annotations support

{
  person(id:1){
     surname
     birthDate
  }
}

Demo 7: DefaultValue

@Query
public List<Person> getPersonsWithSurname(
        @DefaultValue("Doyle") String surname) {
    return personService.getPeopleWithSurname(surname);
}

Providing a parameter

{
  personsWithSurname(surname:"Hyatt") {
    names
  }
}

Uing the default

{
  personsWithSurname {
    names
  }
}

Demo 8: Errors and partial responses

Validation Errors
{
  person(id:1){
     surname
     scores{
      thisDoesNotExist
    }
  }
}
Partial results
{
  person(id:1){
     surname
     scores{
      name
      value
    }
  }
}

Demo 9: Metrics and Tracing

Servers

This will be specific to your setup. For me:

Start Prometheus:

cd /opt/Metrics/prometheus-2.19.2.linux-amd64
./prometheus --config.file=prometheus.yml

(prometheus.yml is in the root folder)

Start Grafana:

sudo systemctl start grafana-server

(grafana.json is in the root folder)

Start Jaeger:

docker run -p 5775:5775/udp -p 6831:6831/udp -p 6832:6832/udp -p 5778:5778 -p 16686:16686 -p 14268:14268 jaegertracing/all-in-one:latest
Metrics
@Timed(name = "personTimer", description = "How long does it take to get a Person.", unit = MetricUnits.NANOSECONDS)
@Counted(name = "personCount", description = "How many times did we ask for Person.")

Grafana Dashboard

metrics

Tracing

Jaeger Dashboard

tracing1 tracing2

Demo 10: Security

@RolesAllowed("admin")

Demo 11: Bean validation

@Query
public Integer getRandomNumber(@Min(10) long seed){
    Random random = new Random(seed);
    return random.nextInt();
}
{
  randomNumber(seed:9)
}

Demo 12: Mutations

    @Mutation
    public Person updatePerson(Person person){
        return personService.updateOrCreate(person);
    }
    
    @Mutation
    public Person deletePerson(Long id){
        return personService.delete(id);
    }
Create
mutation CreatePerson{
  updatePerson(person : 
    {
      names: "Phillip"
    }
  ){
    id
    names
    surname
    profilePictures
    website
  }
}
Update

(using the generated id)

mutation UpdatePerson{
  updatePerson(person : 
    {
      id: 11, 
      names:"Phillip",
      surname: "Kruger", 
      profilePictures: [
        "https://pbs.twimg.com/profile_images/1170690050524405762/I8KJ_hF4_400x400.jpg"
      ],
      website: "http://www.phillip-kruger.com"
    }){
    id
    names
    surname
    profilePictures
    website
  }
}
Delete

(using the id)

mutation DeletePerson{
  deletePerson(id :11){
    id
    surname
  }
}

Demo 13: Context (Experimental)

quarkus.hibernate-orm.log.sql=true
@Inject
Context context;

JsonArray selectedFields = context.getSelectedFields();
System.out.println("selectedFields [" + selectedFields +"]");

{
  people{
    surname
  }
}

Demo 14: Client (Future)

See the model in the JavaDoc (target/apidocs/index.html)

//@Inject
PersonGraphQLClient graphQLClient = GraphQlClientBuilder.newBuilder().build(PersonGraphQLClient.class);
Person graphQLPerson = graphQLClient.getPerson(id);
        
System.err.println("================ GRAPHQL ================");
System.err.println(graphQLPerson);

Apendix: Introspection

{
  __schema{
    types {
      name
      kind
    }
  }
}
 

The Introspection query used by GrapiQL

query IntrospectionQuery {
  __schema {
    queryType { name }
    mutationType { name }
    subscriptionType { name }
    types {
      ...FullType
    }
    directives {
      name
      description
      locations
      args {
        ...InputValue
      }
    }
  }
}

fragment FullType on __Type {
  kind
  name
  description
  fields(includeDeprecated: true) {
    name
    description
    args {
      ...InputValue
    }
    type {
      ...TypeRef
    }
    isDeprecated
    deprecationReason
  }
  inputFields {
    ...InputValue
  }
  interfaces {
    ...TypeRef
  }
  enumValues(includeDeprecated: true) {
    name
    description
    isDeprecated
    deprecationReason
  }
  possibleTypes {
    ...TypeRef
  }
}

fragment InputValue on __InputValue {
  name
  description
  type { ...TypeRef }
  defaultValue
}

fragment TypeRef on __Type {
  kind
  name
  ofType {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
              }
            }
          }
        }
      }
    }
  }
}

Schemas

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].