Coder Social home page Coder Social logo

graphan / react-apollo-typescript-java-example Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 1.0 143 KB

Use static types in the frontend world easily thanks to GraphQL and TypeScript!

HTML 6.00% TypeScript 6.27% JavaScript 1.76% Java 53.09% Groovy 32.56% CSS 0.32%
graphql typescript javascript java react-apollo webpack

react-apollo-typescript-java-example's Introduction

Proof of concept: React Apollo TypeScript Java

Use static typings in the frontend world easily!

Concept

In vast majority of cases, both server-side and client-side use the same data model. The main difference is that data on the server and client side are respectively: statically and dynamically typed. The culprit is that REST APIs do not allow us to send information about data types by default. We just receive data values.

Harnessing such solutions as GraphQL and TypeScript, we can easily move static typings to the frontend world. The communication could be described as follows:

  • Java -> GraphQLSchema -> TypeScript Interfaces -> React

where GraphQL Schema and TypeScript Interfaces are generated automatically.

Hence, the aim of this proof of concept is to demonstrate how to:

  • expose GraphQL API in Java (compatible with React Apollo)
  • generate GraphQL Schema based on Java JPA Model
  • generate TypeScript Interfaces based on GraphQL Schema
  • create the app consuming exposed GraphQL API

Running the app

1. Server

The server side part was taken from the project: graphql-jpa. Only some changes were needed to make it compatible with React Apollo. You just need to run the application located at: server/src/test/groovy/org/crygier/graphql/TestApplication.groovy

GraphQL API will be exposed at: http://localhost:8080/graphql

GraphiQL will be by default available at: http://localhost:8080

2. Client

The client side was created based on typed-graphql-client-example. It was totally ovehauled to make it compatible with GraphQL API exposed by the above server. In order to run:

cd client
yarn install
npm start

If you want to launch the application, remember to run the above server first and then:

How TypeScript Interfaces are generated?

npm start invokes two scripts behind the scenes:

  • npm run update-schema

    It downloads the GraphQL Schema from the server in JSON and save it in client/schema/schema.json.

  • npm run generate-graphql-types

    It generates TypeScript Interfaces based on earlier downloaded GraphQL Schema and graphql files located at: client/src/graphql and save them in: client/src/interfaces.ts

react-apollo-typescript-java-example's People

Contributors

graphan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

donggeo

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.