Giter VIP home page Giter VIP logo

spring-petclinic-graphql's Introduction

Spring PetClinic Sample Application using spring-graphql

This PetClinic version uses the new spring-graphql project, that has been introduced in july 2021 and is going to be shipped with Spring Boot 2.7.

This version currenty uses Spring Boot 2.7 M3 with GraphQL for Spring 1.0.0-M6.

It implements a GraphQL API for the PetClinic and provides an example Frontend for the API.

Java CI with Maven

Open in Gitpod

Features

As spring-graphql is still experimental, this PetClinic is experimental and in-progress too.

Some features that are built in:

  • Annotated Controllers (see SpecialtyController and VetController)
  • Subscriptions via Websockets (see VisitController#onNewVisit)
  • Own scalar types (See PetClinicRuntimeWiringConfiguration and DateCoercing)
  • GraphQL Interfaces (GraphQL Type Person) and Unions (GraphQL Type AddVetPayload), see class PetClinicRuntimeWiringConfiguration
  • Security: the /graphql http and WebSocket endpoints are secured and can only be accessed using a JWT token. More fine grained security is implemented using @PreAuthorize (see VetService)
    • Example: ´addVetmutation is only allowed for users withROLE_MANAGER`
  • Pagination and Sorting of results: implemented with spring-data, see OwnerQueryWiring
  • Tests: See test folder for typical GraphQL endpoint tests, including tests for security

Running the sample application

You can run the sample application with two ways:

  1. The easiest way: run it pre-configured in cloud IDE GitPod
  2. Run it locally

Run it in GitPod

To run the application (backend, GraphiQL and React frontend) in GitPod, simply click on the "Open in GitPod" button at the top of this README.

  • Note that you need a (free) GitPod account.
  • And please make sure that you allow your browser opening new tabs/windows from gitpod.io!

After clicking on the GitPod button, GitPod creates a new workspace including an Editor for you, builds the application and starts backend and frontend. That might take some time!

When backend and frontend are running, GitPod opens two new browser tabs, one with GraphiQL and one with the PetClinic backend. For login options, see below "Accessing the GraphQL API"

Note that the workspace is your personal workspace, you can make changes, save files, re-open the workspace at any time and you can even create git commits and pull requests from it. For more information see GitPod documentation.

In the GitPod editor you can make changes to the app, and after saving the app will be recompiled and redeployed automatically.

SpringBoot PetClinic in GitPod Workspace

Running locally

The server is implemented in the backend folder and can be started either from your IDE (org.springframework.samples.petclinic.PetClinicApplication) or using maven from the root folder of the repository:

./mvnw spring-boot:run -pl backend

Note: the server runs on port 9977, so make sure, this port is available.

(The server uses an in-memory database, so no external DB is needed)

Running the frontend

While you can access the whole GraphQL API from GraphiQL this demo application also contains a modified version of the classic PetClinic UI. Compared to the original client this client is built as a Single-Page-Application using React and Apollo GraphQL and has slightly different features to make it a more realistic use-case for GraphQL.

You can install and start the frontend by using npm:

cd ./frontend

npm install

npm run build:css

npm run generate

npm start

The running frontend can be accessed on http://localhost:3000.

For valid users to login, see list above.

SpringBoot PetClinic, React Frontend

Accessing the GraphQL API

You can access the GraphQL API via the included customized version of GraphiQL.

The included GraphiQL adds support for login to the original GraphiQL.

You can use the following users for login:

  • joe/joe: Regular user
  • susi/susi: has Manager Role and is allowed to execute the createVet Mutation

After starting the server, GraphiQL runs on http://localhost:9977

Sample Queries

Here you can find some sample queries that you can copy+paste and run in GraphiQL. Feel free to explore and try more 😊.

Query all owners whose lastname starts with "K" and their pets:

query {
  owners(filter: {lastName: "K"}) {
    pageInfo {
      totalCount
    }
    owners {
      id
      firstName
      lastName
      pets {
        id
        name
      }
    }
  }
}

Add a new Visit using a mutation (can be done with user joe and susi) and read id and pet of the new created visit:

mutation {
    addVisit(input:{
        petId:3,
        description:"Check teeth",
        date:"2022/03/30",
        vetId:1
    }) {
        newVisit:visit {
            id
            pet {
                id 
                name 
                birthDate
            }
        }
    }
}

Add a new veterinarian. This is only allowed for users with ROLE_MANAGER and that is susi:

mutation {
  addVet(input: {
      firstName: "Dagmar", 
      lastName: "Smith", 
      specialtyIds: [1, 3]}) {
      
    ... on AddVetSuccessPayload {
      newVet: vet {
        id
        specialties {
          id
          name
        }
      }
    }
      
    ... on AddVetErrorPayload {
      error
    }
  }
}

Listen for new visits using a Subscription

Hint: open Graphiql in two browser tabs in parallel. In 1st window, run the following subscription, in the 2nd tab create than a new Visit (see above for an example). The new Visit should automatically be seen in 2nd tab, after the Mutation in 1st tab completes.

This mutation selects the treating veterinarian of the new created Visit and the pet that will be visiting.

subscription {
    onNewVisit {
        description
        treatingVet {
            id
            firstName
            lastName
        }
        pet {
            id
            name
        }
    }

}

Note: The WebSocket/Subscription support in GraphiQL is far from being robust. Use with care!

SpringBoot PetClinic, GraphiQL

Contributing

If you like to help and contribute you're more than welcome! Please open an issue or a Pull Request

Initial implementation of this GraphQL-based PetClinic example: Nils Hartmann, Twitter

spring-petclinic-graphql's People

Contributors

arey avatar babinslava avatar boly38 avatar bram-atmire avatar cbeams avatar colinbut avatar craigsdennis avatar cyrille-leclerc avatar dependabot[bot] avatar freemansoft avatar gordonad avatar helloworld123122344134 avatar liouxiao avatar lukasz-szewc avatar meltsufin avatar mklose avatar nilshartmann avatar patrickcrocker avatar rstoyanchev avatar srbala avatar srenkens avatar tduchateau avatar tejasm avatar thinkshihang avatar trepel avatar verydapeng avatar vfedoriv avatar vladfau avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.