You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+117-32
Original file line number
Diff line number
Diff line change
@@ -19,82 +19,167 @@ module.exports = {
19
19
password:'db-password',
20
20
database:'world'
21
21
},
22
-
query:'SELECT * FROM city',
23
-
idFieldName:'ID'
22
+
queries: [
23
+
{
24
+
statement:'SELECT * FROM country',
25
+
idFieldName:'Code',
26
+
name:'country'
27
+
}
28
+
]
24
29
}
25
30
}
26
31
// ... other plugins
27
32
]
28
33
};
29
34
```
30
35
36
+
And then you can query via GraphQL with the type `allMysql<Name>` where `<Name>` is the `name` for your query.
37
+
38
+
Below is a sample query, however, it is probably different from yours as it would dependent on your configuration and your SQL query results.
39
+
40
+
Use [GraphiQL](https://www.gatsbyjs.org/docs/introducing-graphiql/) to explore the available fields.
41
+
42
+
```graphql
43
+
query {
44
+
allMysqlCountry {
45
+
edges {
46
+
node {
47
+
Code
48
+
Name
49
+
Population
50
+
}
51
+
}
52
+
}
53
+
}
54
+
```
55
+
31
56
### multiple queries
32
57
33
-
When you have multiple queries, include the plugins multiple times with different `typePrefix`.
58
+
When you have multiple queries, add another item in the `queries` option with different `name`.
34
59
35
60
```javascript
61
+
// In your gatsby-config.js
36
62
module.exports= {
37
63
plugins: [
38
64
{
39
-
resolve:'gatsby-source-mysql',
65
+
resolve:`gatsby-source-mysql`,
40
66
options: {
41
67
connectionDetails: {
42
68
host:'localhost',
43
-
user:'malcolm',
44
-
password:'password',
69
+
user:'db-username',
70
+
password:'db-password',
45
71
database:'world'
46
72
},
47
-
query:'SELECT * FROM city',
48
-
idFieldName:'ID',
49
-
typePrefix:'City'
73
+
queries: [
74
+
{
75
+
statement:'SELECT * FROM country',
76
+
idFieldName:'Code',
77
+
name:'country'
78
+
},
79
+
{
80
+
statement:'SELECT * FROM city',
81
+
idFieldName:'ID',
82
+
name:'city'
83
+
}
84
+
]
50
85
}
51
-
},
86
+
}
87
+
// ... other plugins
88
+
]
89
+
};
90
+
```
91
+
92
+
### joining queries
93
+
94
+
It's possible to join the results of the queries by providing `parentName`, `foreignKey`, and `cardinality` to the query object.
95
+
96
+
> Currently only one-to-one and one-to-many relationship are supported. If you have a use case for many-to-many relationship, [raise an issue][raise-issue], and I'll look into it.
97
+
98
+
```javascript
99
+
// In your gatsby-config.js
100
+
module.exports= {
101
+
plugins: [
52
102
{
53
-
resolve:'gatsby-source-mysql',
103
+
resolve:`gatsby-source-mysql`,
54
104
options: {
55
105
connectionDetails: {
56
106
host:'localhost',
57
-
user:'malcolm',
58
-
password:'password',
107
+
user:'db-username',
108
+
password:'db-password',
59
109
database:'world'
60
110
},
61
-
query:'SELECT * FROM country',
62
-
idFieldName:'Code',
63
-
typePrefix:'Country'
111
+
queries: [
112
+
{
113
+
statement:'SELECT * FROM country',
114
+
idFieldName:'Code',
115
+
name:'country'
116
+
},
117
+
{
118
+
statement:'SELECT * FROM city',
119
+
idFieldName:'ID',
120
+
name:'city',
121
+
parentName:'country',
122
+
foreignKey:'CountryCode',
123
+
cardinality:'OneToMany'
124
+
}
125
+
]
64
126
}
65
127
}
66
128
// ... other plugins
67
129
]
68
130
};
69
131
```
70
132
71
-
## Plugin options
72
-
73
-
As this plugin is a wrapper of the popular [`mysql`](https://www.npmjs.com/package/mysql) library, the options are based on the library.
74
-
75
-
-**connectionDetails** (required): options when establishing the connection. Refer to [`mysql` connection options](https://www.npmjs.com/package/mysql#connection-options)
76
-
-**query** (required): the SQL query statement to be executed.
77
-
-**idFieldName** (required): column that is unique for each record. This column must be included in the `query`.
78
-
-**typePrefix** (optional): the prefix of the data source in the GraphQL schema. Default to `MySql`.
79
-
80
-
## How to query your data using GraphQL
81
-
82
-
The GraphQL type would be follow the format of `all<typePrefix>Results`.
83
-
84
-
Below is a sample query, however, it is probably different from yours as it would dependent on your configuration and your SQL query results.
133
+
In the example above, `country` and `city` is one-to-many relationship (one country to multiple cities), and they are joined with `country.Code = city.CountryCode`.
85
134
86
-
Use [GraphiQL](https://www.gatsbyjs.org/docs/introducing-graphiql/) to explore the available fields.
135
+
With the configuration above, you can query a country joined with all the related cities with
87
136
88
137
```graphql
89
138
query {
90
-
allCountryResults {
139
+
allMysqlCountry {
91
140
edges {
92
141
node {
93
142
Code
94
143
Name
95
144
Population
145
+
cities {
146
+
Name
147
+
}
148
+
}
149
+
}
150
+
}
151
+
}
152
+
```
153
+
154
+
It also works the other way, i.e. you can query the country when getting the city
155
+
156
+
```graphql
157
+
query {
158
+
allMysqlCity {
159
+
edges {
160
+
node {
161
+
Name
162
+
country {
163
+
Name
164
+
}
96
165
}
97
166
}
98
167
}
99
168
}
100
169
```
170
+
171
+
## Plugin options
172
+
173
+
-**connectionDetails** (required): options when establishing the connection. Refer to [`mysql` connection options](https://www.npmjs.com/package/mysql#connection-options)
174
+
-**queries** (required): an array of object for your query. Each object could have the following fields:
|`statement`| Required | the SQL query statement to be executed. |
179
+
|`idFieldName`| Required | column that is unique for each record. This column must be returned by the `statement`. |
180
+
|`name`| Required | name for the query. Will impact the value for the graphql type |
181
+
|`parentName`| Optional | name for the parent entity. In a one-to-many relationship, this field should be specified on the child entity (entity with many records). |
182
+
|`foreignKey`| Optional | foreign key to join the parent entity. |
183
+
|`cardinality`| Optional | the relationship between the parent and this entity. Possible values: `"OneToMany"`, `"OneToOne"`. Default to `"OneToMany"`. (Note: many-to-many relationship is currently not supported.) |
0 commit comments