On this page

MySQL

Generate GraphQL schema from MySQL database with GraphQL Mesh. Learn how to use the MySQL handler and its powerful 'where' field.

This handler allows you to generate GraphQL schema from an existing MySQL database.

How to use?

To get started, install the handler library:

npm i @omnigraph/mysql

Now, you can use it directly in your Mesh config file:

mesh.config.ts
import { defineConfig } from '@graphql-mesh/compose-cli'
import { loadMySQLSubgraph } from '@omnigraph/mysql'

export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadMySQLSubgraph('Employees', {
        endpoint: 'mysql://root:passwd@localhost:3306/employees'
        // You can use environment variables like
        // endpoint: "mysql://{env.MYSQL_USER}:{env.MYSQL_PASSWORD}@{env.MYSQL_HOST}:{env.MYSQL_PORT}/{env.MYSQL_DATABASE}"
      })
    }
  ]
})

How does where work?

Every CRUD operation has where field in its input, so you can see all the columns of a table. where works like below;

{
  getProduct(
    where: {
      id: 5
      year: ">2010"
      price: "100..200"
      level: "<=3"
      sn: "*str?"
      label: "str"
      code: "(1,2,4,10,11)"
    }
  ) {
    id
    name
  }
}

This GraphQL operation will send the following query to your MySQL database;

SELECT id, name FROM product WHERE id = 5 AND year > '2010' AND (price BETWEEN '100' AND '200') AND level <= '3' AND sn LIKE '%str\_' AND label = 'str' AND code IN (1,2,4,10,11)