Catch the highlights of GraphQLConf 2023! Click for recordings. Or check out our recap blog post.
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:

npm i @graphql-mesh/mysql

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

.meshrc.yaml
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) - The hostname of the database you are connecting to. (Default: localhost)
  • port (type: Int) - The port number to connect to. (Default: 3306)
  • localAddress (type: String) - The source IP address to use for TCP connection
  • user (type: String) - The MySQL user to authenticate as
  • password (type: String) - The password of that MySQL user
  • database (type: String) - Name of the database to use for this connection
  • ssl (type: Object) - SSL Options for your MySQL connection:
    • rejectUnauthorized (type: Boolean) - Default: true
    • ca (type: String) - Path to your CA
  • 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)