All Projects → batjko → gql2sql

batjko / gql2sql

Licence: MIT license
GraphQL to SQL service example

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to gql2sql

boilerplate-nexus-prisma-apollo-graphql-express
Boilerplate project for a graphql server using nexus-prisma and apollo-server-express
Stars: ✭ 31 (+0%)
Mutual labels:  apollo-server, prisma2
goprisma
A Go wrapper for prisma to turn databases into GraphQL APIs using Go.
Stars: ✭ 54 (+74.19%)
Mutual labels:  prisma-client, prisma2
nestjs-prisma-starter
Starter template for NestJS 😻 includes GraphQL with Prisma Client, Passport-JWT authentication, Swagger Api and Docker
Stars: ✭ 1,107 (+3470.97%)
Mutual labels:  prisma-client, prisma2
Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite & MongoDB (Preview)
Stars: ✭ 18,168 (+58506.45%)
Mutual labels:  sqlite, prisma-client
apollo-directive
tools for easy (and finally readable!) directive implementation in Apollo Server or any server using graphql-tools.makeExecutableSchema
Stars: ✭ 13 (-58.06%)
Mutual labels:  apollo-server
apollobank
A full stack GraphQL banking application using React, Node & TypeScript.
Stars: ✭ 203 (+554.84%)
Mutual labels:  apollo-server
adonis-apollo-server
GraphQL implementation using Apollo Server for Adonis
Stars: ✭ 14 (-54.84%)
Mutual labels:  apollo-server
graphql-blog-cms-api
A Blog CMS API powered by GraphQL and Apollo server
Stars: ✭ 23 (-25.81%)
Mutual labels:  apollo-server
nim-gatabase
Connection-Pooling Compile-Time ORM for Nim
Stars: ✭ 103 (+232.26%)
Mutual labels:  sqlite
LoginToASqlite3DatabaseWithoutCredentialsWithAdminer
✔️ An Adminer plugin to use SQLite databases without credentials (no username and no password)
Stars: ✭ 30 (-3.23%)
Mutual labels:  sqlite
perseus
An Apollo GraphQL Server & React Client in a Yarn Workspace deployed with Zeit 2.0
Stars: ✭ 35 (+12.9%)
Mutual labels:  apollo-server
schemaglue
Naturally breaks down your monolithic graphql schema into bits and pieces and then glue them back together.
Stars: ✭ 117 (+277.42%)
Mutual labels:  apollo-server
graphql-scalars
📐 A library of custom GraphQL scalar types for creating precise type-safe GraphQL schemas, with validation powered by Yup.
Stars: ✭ 68 (+119.35%)
Mutual labels:  apollo-server
Heighliner
A GraphQL Server for NewSpring Web
Stars: ✭ 13 (-58.06%)
Mutual labels:  apollo-server
go-sqlite
Low-level Go interface to SQLite 3
Stars: ✭ 268 (+764.52%)
Mutual labels:  sqlite
egg-apollo-server
No description or website provided.
Stars: ✭ 21 (-32.26%)
Mutual labels:  apollo-server
intellij-prisma
Prisma schema file support for IntelliJ IDEs (WebStorm, PHPStorm, Pycharm, RubyMine, ...)
Stars: ✭ 25 (-19.35%)
Mutual labels:  prisma2
sqlite-jna
Java wrapper and Jdbc driver for SQLite using JNA or Bridj or JNR or JNI or JavaCPP.
Stars: ✭ 20 (-35.48%)
Mutual labels:  sqlite
apollo-express-ts-server-boilerplate
No description or website provided.
Stars: ✭ 29 (-6.45%)
Mutual labels:  apollo-server
database-schema-examples
Database Schema Examples we strive to support in Prisma
Stars: ✭ 94 (+203.23%)
Mutual labels:  prisma2

GQL 2 SQL

Simple example of a GraphQL server that connects to a SQL backend. To demonstrate this, we're using Apollo and Prisma respectively.

It's also accessing data from elsewhere, to demonstrate how GraphQL combines multiple sources.

Technologies involved

Also a few development convenience things, like Nodemon and the prisma cli.

How does it work

The architecture is structured in a way that the different stages of a GraphQL request are clearly separated:

Design

When a request with a GraphQL query comes in, the following happens:

  1. The GraphQL schema is the definition for the API and so the request gets validated against that definition (e.g. if the query is defined, if the requested fields and parameters exist etc).
  2. Then the resolvers, which receive the query details as function parameters, are executed.
  3. The resolvers now make whatever backend calls they need to make to get the data the query was asking for. In our case the resolvers simply call provider functions and return the provider's responses back to the client who sent the query.
  4. We choose to use "providers" as abstractions over our backend resources, so the resolvers don't need to know if the data comes from a database or a third-party API, for example. This allows us to replace those data sources later on, if we need to, without having to mess with the resolvers. It also keeps our resolver logic clear and easy to understand.

Folder structure

You are free to organise your code in whatever way you like, but the structure we chose here makes a lot of sense for what we need.

  • All our actual code is under a single src folder (apart from project config files)
  • Within src we have a schema and a providers folder, clearly separating these responsibilities.
  • Currently our resolvers are part of our schema files to keep all that together. However, if resolvers become more complex, you might want to consider extractiung them into their own folder as well.
  • You can see that, both under the providers and the schema folder, we distinguish between the two different data domains we support: books and poetry. It's nice and clean, and allows you to easily find what you're looking for.
  • When you use the Prisma CLI it automatically creates a prisma folder at the top level, to keep its own config, and since we are using SQLite here, the DB file is also in that folder by default.

There is also an images folder in this repo, which just holds the assets for this README.

GraphQL

For the GraphQL Server, we use the most popular library around: Apollo-Server.

It gives us everything we need to handle GraphQL stuff:

  • The actual web server. We could also use it with an existing API server, e.g. Express, Koa, Fastify etc., but for our simple use case here, it works pretty well on its own.
  • It provides us with ways to assemble schema Type Definitions
  • It also gives us a way to hook in our resolvers that will get the data and send query responses back to the client.
  • As a convenience, it also gives us the gql string template to parse the GraphQL schema notations.
  • Finally, it comes with GraphQL Playground out of the box, which is handy during development.

SQL

It's a pretty traditional need to access a SQL database from our GraphQL server. Lots of so-called ORMs exist to do this, most notable Sequelize and Knex. But recently Prisma has made waves and so we're using it for our example here.

Check the src/providers/booksDB folder for how we handle our SQL database using Prisma:

  • In dbClient.js we initialize our Prisma client, which we will use to execute all our SQL requests.
  • In dbProvider.js, we execute the actual queries against the DB, using the prisma client.
  • We create a few records in our database when the server starts, via the ensureSeedData() function in seed.js, in case the database is empty. This way we have some data to query, no matter what.

Prisma Model autocomplete

Running the server

  1. Clone repo
  2. npm install
  3. npm start launches nodemon for easy development

NOTE: You can reset the SQLite DB anytime by running npm run resetDB.

Run some queries

Apollo Server comes with a querying UI out of the box.

Simply visit http://localhost:3000 and execute a GraphQL query, e.g.:

Query Results

TODO

  • Add a mutation
  • Add a second data source
  • Add a diagram of the architecture
  • Document code more comprehensively
  • Finish ReadMe
  • Maybe add a few tests?
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].