Code Monkey home page Code Monkey logo

graphql_springboot's Introduction

GraphQL with SpringBoot

In this project you can see how to use GraphQL with Spring Boot and create a GraphQL server. This is a use case of an One-To-Many relation mapping. We will not use any database for this project, information is saved and retrieved from static lists. The scenario is a movies / studios relation where each studio has a list of movies.

Types and Resolvers

We are using two types here:

  • Movie
  • Studio

You can see each type along its queries and mutations on its corresponding file:

  • movie.graphqls
  • studio.graphqls

There is also another file: common.graphqls Inside there we have only the Query and Mutation declarations. movie and studio query and mutation type both extends from this common file.

For each type we are using its own resolver in order to correctly map the two types together.

Static data initialization

As we are not using any DB for storing and retrieving data, any CRUD will be holding on static lists. We are initializing the lists inside GraphqlApplication class

@Bean
public List<Studio> getStudios(){
  ...
}

@Bean
public List<Movie> getMovies(){
  ...
}

Build

Run the project using the following commands:

mvn clean package
java -jar graphql-1.0.0.jar

Schema

GraphQL uses a single endpoint only. You see the schema on your browser:

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

Using GraphiQL

You can use your web browser to test the project and play with graphql. GraphiQL is the tool for this job. On your browser:

http://localhost:8080/graphiql

e.g.:

query {
  findAllMovies {
    title
  }
  getMoviesOfStudio(studio: {id:2, name:"Warner Studios"}) {
    id
  }
	findAllStudios {
	  name
    movies {
      title
    }
	}
  
}

produces:

{
  "data": {
    "findAllMovies": [
      {
        "title": "Minios"
      },
      {
        "title": "Aquaman"
      },
      {
        "title": "Fantasia"
      }
    ],
    "getMoviesOfStudio": [],
    "findAllStudios": [
      {
        "name": "Universal Studios",
        "movies": [
          {
            "title": "Minios"
          }
        ]
      },
      {
        "name": "Warner Studios",
        "movies": [
          {
            "title": "Aquaman"
          }
        ]
      },
      {
        "name": "Disney Studios",
        "movies": [
          {
            "title": "Fantasia"
          }
        ]
      }
    ]
  }
}

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.