Use grpc as a data source for graphql apollo server
- Install protoc into
./vendor
folder, checkpackage.json
"compile:proto" script has correct path "vendor/protoc-23.2-osx-x86_64/bin/protoc", - Run
npm run compile:graphql
npm run start:grpc:server
npm run start:graphql:server
- Go to localhost:4000 and run query
query ExampleQuery {
grpcBooks {
title
author
}
}
# use default protoc version
docker build -t local-protoc .
# or specify custom one
docker build --build-arg PROTOC_VERSION=24.0 -t local-protoc .
-
Protobuf Definitions
: The plugin should be able to parse the protobuf definitions (.proto files) which define the gRPC service and the message types it uses. This includes service methods and their request/response types. -
GraphQL Schema Generation
: The plugin should be able to generate a GraphQL schema from the protobuf definitions. This involves mapping protobuf message types to GraphQL types and service methods to GraphQL queries or mutations. For example, in the graphql-over-grpc repository, the plugin generates a GraphQL schema where each gRPC service method becomes a GraphQL field. -
GraphQL to gRPC Mapping
: The plugin should provide a way to map GraphQL requests to gRPC requests and vice versa. This involves converting GraphQL queries/mutations to gRPC method calls and converting the responses back to GraphQL. In the grpc-graphql-gateway repository, this is done using a GraphQL execution engine which resolves GraphQL fields to gRPC method calls.
graph TD
A[Proto Files] --> B[Protoc Plugin]
B --> C[Parse Proto Definitions]
C --> D[Map Proto to GraphQL Types]
D --> E[Generate GraphQL Schema]
E --> F[Generate Resolvers]
F --> G[Output GraphQL Types and Resolvers]
graph TB
A[Consumer] --> B[Gateway]
B --> C[Search Service]
B --> D[Order History Service]
B --> E[Reviews Service]
B --> F[Cart Service]
B --> G[Checkout Service]
This diagram:
- Consumer: This is the user or system that is making requests. It communicates with the Gateway.
- Gateway: This is the entry point for the Consumer. It routes requests from the Consumer to the appropriate services.
- Search Service: This service handles search-related requests.
- Order History Service: This service handles requests related to the user's order history.
- Reviews Service: This service handles requests related to reviews.
- Cart Service: This service handles requests related to the user's shopping cart.
- Checkout Service: This service handles checkout-related requests.
graph TB
A[Consumer] --> B[Gateway]
B --> C[Search Service]
B --> D[Order History Service]
B --> E[Reviews Service]
B --> F[Cart Service]
B --> G[Checkout Service]
C -->|gRPC| H[gRPC Plugin]
D -->|gRPC| H
H --> I[GraphQL Schemas]
Apollo is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Here's a high-level overview of how it works:
-
Apollo Client: This is the core part of Apollo and acts as a comprehensive state management library that integrates with your JavaScript app. It enables you to fetch, cache, and modify application data, all while automatically updating your UI.
-
Fetching Data: Apollo Client uses a GraphQL server to fetch data. You write queries using the GraphQL query language and Apollo Client takes care of sending the query to your server and parsing the response.
-
Caching: Apollo Client caches the results of your queries, which can help to reduce the load on your server and improve performance. When a component is mounted, Apollo Client checks if the data for any queries it's making is already in the cache. If it is, Apollo Client uses the cached data instead of sending a new network request.
-
Updating the UI: Apollo Client integrates with your UI library (like React, Vue, Angular, etc.) and automatically updates your UI when the data changes. This is done through a system of "watched queries". When the results of a query change (either due to a network request or a cache update), any UI components that depend on that data are automatically re-rendered with the new data.
-
Local State Management: Apollo Client also allows you to manage local state alongside remote data. You can define local resolvers to handle local queries and mutations, and you can even mix local and server data in the same query.
-
Apollo Server: While not necessary to use Apollo Client, Apollo Server is a community-driven, open-source GraphQL server that works with any GraphQL schema. It's the perfect companion to Apollo Client, providing features like performance tracing, error tracking, and schema stitching.
In summary, Apollo provides a holistic solution to manage both local and remote data in JavaScript applications, making it easier to build complex, data-driven applications.
TODO: fix diagram
graph TB
A[UI Components] --> B[Apollo Client]
B --> C[GraphQL Operations]
C --> D[Apollo Client Cache]
D --> E[Reactivity]
E --> A
C --> F[Network Layer]
F --> G[GraphQL Server]
G --> H[Data Sources]
H --> G
G --> F
F --> C
This diagram can be interpreted as follows:
-
UI Components: These are the visual elements of your application. They interact with Apollo Client to fetch and display data.
-
Apollo Client: This is the core part of Apollo. It manages both local and remote data and integrates with your UI components.
-
GraphQL Operations: These are the queries, mutations, and subscriptions that your UI components use to interact with your data.
-
Apollo Client Cache: Apollo Client caches the results of your queries to improve performance and enable offline functionality.
-
Reactivity: Apollo Client automatically updates your UI components when the data they depend on changes.
-
Network Layer: This is the part of Apollo Client that communicates with your GraphQL server.
-
GraphQL Server: This is your server that provides a GraphQL API. It fetches data from your data sources in response to queries from Apollo Client.
-
Data Sources: These are the databases, APIs, and other sources of data that your server uses to fetch data.
-
Schema First Design
-
Schema Usage
graph TB
A[GraphQL Client] --> B[GraphQL Queries/Mutations]
B --> C[Apollo Client]
C --> D[Network Layer]
D --> E[GraphQL Server]
E --> F[Resolvers]
F --> G[Data Sources]
F --> H[GraphQL Schema]
H --> I[Type Definitions]
H --> J[Resolvers]
J --> K[Data Sources]
graph TB
A[GraphQL Client] --> B[GraphQL Queries/Mutations]
B --> C[Apollo Client]
C --> D[GraphQL Server]
D --> E[Resolvers]
E --> F[Data Sources]
E --> G[GraphQL Schema]
G --> H[Type Definitions]
G --> I[Resolvers]
I --> J[Data Sources]
TODO:fix diagram with arrow text instead of full edges
graph TB
A[GraphQL Client] -->|"{ books { title, author } }"| B[Apollo Client]
B -->|"{ addBook(title: 'New Book', author: 'Author') { title, author } }"| C[GraphQL Server]
C --> D[Query/Mutation Parser]
D --> E[Resolvers]
E --> F[Data Sources]
D --> G[GraphQL Schema]
G --> H["Type Book { title: String, author: String }"]
H --> I["Type Query { books: [Book] }"]
I --> J["Type Mutation { addBook(title: String, author: String): Book }"]
J --> K["Resolvers { Query: { books() {...} }, Mutation: { addBook(_, { title, author }) {...} } }"]
K --> L[Data Sources]
-
Join the XTechnology discord community to collaborate and feedback on the project
-
rejoiner - Generates a GraphQL schema from gRPC microservices
-
On GraphQL Gateway to serve Go GRPC microservices 2018 by Iheanyi Ekechukwu(@kwuchu),
-
https://www.apollographql.com/tutorials/lift-off-part1/05-apollo-server
-
https://x-tech.io/our-engineering-blog/translating-grpc-services-into-graphql
-
https://medium.com/xanthous/translating-grpc-services-into-graphql-6a8e49556d96
- add tests for working server with dynamic generated schema using node test runner
- make it in docker
- write a general purpose graphql server to work with generated grpc code
- create nestjs tutorial - schema first (nexus!) or code first?
- check golang grpc gateway
- protoc plugin to generate graphql schemas from grpc microservices
POC
- traverse, find all messages and services from input files and generate graphl services
- https://protobufjs.github.io/protobuf.js/Root.html#nested
- https://github.com/protobufjs/protobuf.js/blob/master/examples/traverse-types.js
- alternative traverse
use templates https://github.com/ysugimoto/grpc-graphql-gateway/blob/master/protoc-gen-graphql/template.go
- generate graphql types definitions
- generate resolvers
- traverse, find all messages and services from input files and generate graphl services
- document use cases
- npm publish prepare - restructure repo (mkdir example, mv server* example)
- which grpc protobuf registry to choose? also, is it possible to use graphql apollo registry?
- is there a better transformation library?
- is it neccessary to to support all the protoc available flags?
- would it be easier to manually create graphql schemas?
- Type System - GraphQL gRPC Comparison
- Backlog
- Questions
- Build Naive Implementation
- Use https://github.com/graphql/graphql-js, and specifically types
- Tests
- About Project
- Start
- Backlog
- Questions
- Links