This project uses Quarkus, the Supersonic Subatomic Java Framework to test GraphQL.
Prerequisites:
- JDK 17 (and configure
JAVA_HOME
environment variable) - Maven 3.9.6
-
To Create the scaffold (initial structure) you can run next command:
mvn io.quarkus.platform:quarkus-maven-plugin:3.9.1:create \ -DprojectGroupId=com.redhat \ -DprojectArtifactId=quarkus-graphql \ -Dextensions='quarkus-smallrye-graphql' \ -DnoCode
cd quarkus-graphql
NOTE: if you already have a quarkus project, and you need add the graphql dependency only. You can run next command:
mvn quarkus:add-extension -Dextensions='quarkus-smallrye-graphql'
-
You can run your application locally using next command:
mvn quarkus:dev
-
Introspect and see the full schema of the GraphQL API running the following command:
curl http://localhost:8080/graphql/schema.graphql
-
Play with GraphQL UI (Experimental - not included in the MicroProfile specification). GraphQL UI is a great tool permitting easy interaction with your GraphQL APIs.
NOTE: The Quarkus
smallrye-graphql
extension ships with GraphiQL and enables it by default in dev and test modes, but it can also be explicitly configured for production mode as well, by setting thequarkus.smallrye-graphql.ui.always-include
configuration property totrue
.Open the http://localhost:8080/q/graphql-ui/ URL in your favorite web browser.
And make a Query to get all Films, for example:
{ allFilms { director, heroes { name surname darkSide } } }
Or make a Query to get specific films, for example:
query getFilms { film0 : film(filmId: 0) { episodeID, title, heroes { name } } film1 : film(filmId: 1) { episodeID, title, releaseDate } }
Or create a new Hero doing a
Mutation
, for example:mutation createNewHero { createHero(hero: { name: "Ahsoka", surname: "Tano", height: 1.70, mass: 54, darkSide: false, lightSaber: WHITE, episodeIds: [1,2] }) { name, surname } }
Or you can delete a Hero, for example:
mutation deleteAHero { deleteHero(id:1) { name, surname } }
-
Make an HTTP call using CURL to explore the required parameters and the returned response:
curl -X POST localhost:8080/graphql -H "Content-Type: application/json" -d '{ "query": "query { allFilms { episodeID } }" }'
The above HTTP call should retrn the following JSON:
{ "data": { "allFilms": [ { "episodeID": 4 }, { "episodeID": 5 }, { "episodeID": 6 } ] } }