Giter Club home page Giter Club logo

wundergraph / wundergraph-demo Goto Github PK

View Code? Open in Web Editor NEW
64.0 3.0 9.0 1.06 MB

This Repository demonstrates how to combine 7 APIs (4 Apollo Federation SubGraphs, 1 REST, 1 standalone GraphQL, 1 Mock) into one unified GraphQL API which is then securely exposed as a JSON API to a NextJS Frontend.

Home Page: https://wundergraph.com

TypeScript 67.13% CSS 4.40% Dockerfile 3.02% Go 24.71% JavaScript 0.74%
graphql rest rest-api restful graphql-server graphql-client graphql-api api api-rest api-client api-gateway api-wrapper json-api

wundergraph-demo's Introduction

WunderGraph Demo joining Apollo Federation (with Subscriptions), REST and GraphQL APIs and consuming it from a NextJS application

Open in Gitpod

This repository demonstrates how to combine multiple APIs into one unified API and exposing it as a secure JSON API without losing on developer experience.

We're combining the following services:

  • 4 Apollo GraphQL SubGraphs (Accounts, Inventory, Products, Reviews) combined as a SuperGraph
  • 1 REST API (JSON Placeholder)
  • 1 standalone GraphQL API (Trevorblades Countries GraphQL API)
  • 1 Mock REST API

Architecture Overview

All 7 APIs are combined into one unified GraphQL API and securely exposed using JSON RPC.

This example shows how to use Apollo Federation with Subscriptions, a unique feature to WunderGraph. WunderGraph is the only GraphQL Gateway that supports this feature.

Additionally, this example also shows Live Queries. By using server-side Polling, we're able to turn any API into a realtime stream.

Resources

Read the docs: https://wundergraph.com/docs

If you have Questions, join our Discord: https://wundergraph.com/discord

Getting started

npm install && npm start

Open your browser and go to http://localhost:3000.

How does it work?

Merging the APIs

Have a look at ./wundergraph/wundergraph.config.ts. The following code-snipped introspects the different APIs and merges them all together.

const jsonPlaceholder = introspect.openApi({
    apiNamespace: "jsp",
    source: {
        kind: "file",
        filePath: "jsonplaceholder.v1.yaml",
    },
})

const weather = introspect.graphql({
    apiNamespace: "weather",
    url: "https://graphql-weather-api.herokuapp.com/",
});

const federatedApi = introspect.federation({
    apiNamespace: "federated",
    upstreams: [
        {
            url: "http://localhost:4001/graphql"
        },
        {
            url: "http://localhost:4002/graphql"
        },
        {
            url: "http://localhost:4003/graphql"
        },
        {
            url: "http://localhost:4004/graphql",
        },
    ]
});

const countries = introspect.graphql({
    apiNamespace: "countries",
    url: "https://countries.trevorblades.com/",
})

const myApplication = new Application({
    name: "api",
    apis: [
        federatedApi,
        countries,
        jsonPlaceholder,
        weather,
    ],
});

Once everything is merged, and the configuration is built, the WunderGraph engine is able to delegate all Requests tox the correct upstream(s).

By applying namespaces to each individual API, we're able to avoid naming conflicts when merging multiple APIs.

Request Flow

All Operations from the .wundergraph/operations folder will be automatically turned into Persisted Operations. That is, each Operation will be pre-compiled and mounted on a unique URL Path. E.g. the Operation Countries.graphql will turn into the Endpoint /operations/Countries.

In addition to this Endpoint, wunderctl up will also start a code-generator that generates a TypeScript API Client, React Hooks, etc... Have a look at the folder nextjs-frontend/generated to see all the generated code.

Once a JSON-RPC Request hits the WunderNode (WunderGraph Server), it will call into various middlewares for authentication, caching, etc. and then execute the pre-compiled Operation.

This makes the API very secure and performant. Additionally, our GraphQL Gateway Engine is capable of doing Subscriptions for Apollo Federation as well as Live-Queries to keep the UI automatically updated.

Hacking

Modifying Operations

Go to api/.wundergraph/operations, add, remove or modify the operations.

Updating the Frontend

Go to nextjs-frontend/pages/index.tsx and modify the UI, it definitely needs some love for the CSS!

Adding or Removing DataSources

Go to api/.wundergraph/wundergraph.config.ts and modify the introspected DataSources.

wundergraph-demo's People

Contributors

fiam avatar jensneuse avatar mikaelmorvan avatar starptech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wundergraph-demo's Issues

Update SDK for breaking change

Wundergraph Next and SDK dependencies are stale and require updates. API code must be adjusted for a breaking change introduced and the README adjusted to reflect these changes.

Diagram is better than 1000 words

It will be good to add some example diagram to readme so its easy to understand what is happening here.
I needed to dig into CLI source code to understand how this works

Demo app is not working

Hi,
I cloned the repo and then run

npm install && npm start

This is the error that I've got

[generate:api  ] {"level":"fatal","time":1674049427692,"pid":18377,"hostname":"Air-station","component":"@wundergraph/sdk","msg":"Couldn't configure your WunderNode: Error: introspection failed (url: https://graphql-weather-api.herokuapp.com/, namespace: weather), error: Request failed with status code 503"}
[generate:api  ] Error: configuration could not be generated. Process exit with code 1
[generate:api  ] {"level":"error","time":1674049427703,"msg":"Script runner exited with non-zero exit code","hostname":"Air-station","pid":18376,"component":"@wundergraph/wunderctl","runnerName":"config-runner","exit":1,"startTs":1674049419171007000,"stopTs":1674049427703675000,"complete":true}
[generate:api  ] npm ERR! Lifecycle script `generate` failed with error:
[generate:api  ] npm ERR! Error: command failed
[generate:api  ] npm ERR!   in workspace: undefined
[generate:api  ] npm ERR!   at location: /Users/alessioizzo/wundergraph-demo/api

Maybe this is related to the broken link
In fact
Screenshot 2023-01-18 alle 14 45 25

Failed to launch the demo

Hi,

I'm trying to launch the demo and I've got some bugs :

  1. The docker-compose.yml file don't launch if I don't have a version tag on top of it

  2. After installing wunderctl, I'm trying to launch it in the .wundergraph directory and nothing happens :
    I've got the following json message :
    {"level":"info","ts":"2021-10-21T18:13:08.872326848+02:00","msg":"starting WunderNode","version":"0.26.3","commit":"826ec6a9b4aebc8b7ad395ec5e18e8ebcdf7e24f","date":"2021-10-21T08:02:37Z","builtBy":"ci"} {"level":"info","ts":"2021-10-21T18:13:08.915413044+02:00","msg":"api config: file polling","config_file_name":"wundergraph.config.json"} {"level":"debug","ts":"2021-10-21T18:13:08.919160912+02:00","msg":"Analytics config","enable":false} {"level":"debug","ts":"2021-10-21T18:13:08.919239049+02:00","msg":"filePollConfig - config file not found, retrying in 5s","config_path":"wundergraph.config.json"} {"level":"fatal","ts":"2021-10-21T18:13:09.066358235+02:00","msg":"Config Bundler: initial build failed","bundler":"hooks","outFile":"/home/mikaelmorvan/.wundergraph/config/d3VuZGVyZ3JhcGguaG9va3MudHPUHYzZjwCyBOmACZjs-EJ-/hooks.js","errors":[{"PluginName":"","Text":"Could not resolve \"fastify\" (mark it as external to exclude it from the bundle)","Location":{"File":"generated/wundergraph.hooks.configuration.ts","Namespace":"","Line":1,"Column":20,"Length":9,"LineText":"import Fastify from \"fastify\";","Suggestion":""},"Notes":null,"Detail":null}]}

  3. When I launch the nextjs frontend, I've got an error on the webpage
    Unhandled Runtime Error TypeError: Failed to fetch

I'm on Ubuntu 18.04 by the way

Thanks for you help
Mikaël

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.