Git Product home page Git Product logo

wishlist's Introduction

Wishlist using GraphQL

A simple implementation of a GraphQL Server with graphql-java. Use branch dgs to see the same implementation with the dgs-framework by netflix that is building on top of graphql-java.

Schema

type Query {
    wishById(id: ID): Wish
}

type Mutation {
    createWish(id: String!, name: String!, description: String!, ageRating: Int): Wish
}

type Wish {
    id: ID
    name: String
    description: String
    ageRating: Int
    giftedFrom: Person
    givenTo: Person
}

type Person {
    id: ID
    name: String
    age: Int
}

Examples

Query

{
  wishById(id: "wish-1"){
    name,
    description
    ageRating
    givenTo {
      name
    }
    giftedFrom {
      name
    }
  }
}

Mutation

mutation {
  createWish(
    id: "wish-2",
    name: "Gin",
    description: "Monkey 47 Gin, 50cl",
    ageRating: 18
  ){
    name,
    description
    ageRating
  }
}

Thoughts about GraphQL

Usage

Language Support

All major technologies support the graphql specification by now [1].

Client control over what data is fetched

The client is able to control what data should be requested. In REST, if an endpoint is used by multiple clients there is often more data in the response than needed. With graphql, the client can request only the needed data, resulting in smaller and more suitable queries. This is also helpful when dealing with devices with smaller bandwidth [2].

Performing expensive queries

Because of the design of graphql, the client can decide what data should be requested. This could lead to very expensive queries where a client requests a lot of fields from a lot of resources [2]. This can have a big impact on the server and its resources (sometimes multiple involved microservices), without being able to control it. Similar to SQL, it is very hard to optimize for such expensive / complex queries.

Aggregate data from multiple sources

Graphql can reduce the round trip, when data from multiple data sources (e.g. multiple microservices) is needed. In REST this would require multiple HTTP calls. In graphql this can be solved by defining multiple data types to aggregate the data from multiple sources (see example) [3]. With this, graphql can be used as aggregation gateway or act like a BFF [4].

Writes & Mutations

Graphql supports writes by using the mutation keyword [5]. Similar to REST, any query could theoretically modify data, but using mutation is recommended. Graphql is mostly used for read queries, while writes are often still made using REST [3].

Error Handling

There is no uniform error handling compared to REST, where the HTTP codes provide a clear structure for unsuccessful responses. In graphql errors, there is only a message field required by the graphql spec, other fields are optional [6]. Because the response is always HTTP code 200, errors need to be handled manually. Some libs define their own contract [7].

Source

wishlist's People

Watchers

Lukas Frei 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.