Docs
Source Handlers
MySQL

MySQL

image

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

To get started, install the handler library:

yarn add @graphql-mesh/mysql

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

sources:
  - name: Employees
    handler:
      mysql:
        # You can use environment variables like
        # host: "{env.MYSQL_HOST}"
        # port: "{env.MYSQL_PORT}"
        # user: "{env.MYSQL_USER}"
        # password: "{env.MYSQL_PASSWORD}"
        # database: "{env.MYSQL_DATABASE}"
        host: localhost
        port: 3306
        user: root
        password: passwd
        database: employees

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)

Config API Reference

  • host (type: String)
  • port (type: Int)
  • user (type: String)
  • password (type: String)
  • database (type: String)
  • pool (type: Any) - Use existing Pool instance Format: modulePath#exportName
  • tables (type: Array of String, required) - Use specific tables for your schema
  • tableFields (type: Array of Object, required) - Use specific fields of specific tables:
    • table (type: String, required)
    • fields (type: Array of String, required)