Querying tables by its primary key
For every table that has a primary key(s), GraphQLize generates a corresponding GraphQL query to enable querying the table by its primary keys.
For a table like below,
- Postgres
- MySQL
CREATE TABLE actor (
actor_id SERIAL PRIMARY KEY,
first_name character varying(45) NOT NULL,
last_name character varying(45) NOT NULL
);
GraphQLize generates the following query
type QueryRoot {
actorByActorId(actorId: Int!): Actor
# ...
}
schema {
query: QueryRoot
}
The query name actorByActorId
is created using the following convention.
{camel-case(table-name)}
By{pascal-case(primary-key-column-name)}
If a table has multiple primary keys like below
- Postgres
- MySQL
CREATE TABLE film_actor (
actor_id smallint REFERENCES actor(actor_id),
film_id smallint REFERENCES film(film_id)
CONSTRAINT film_actor_pkey PRIMARY KEY (actor_id, film_id)
);
GraphQLize generates the following query
type QueryRoot {
filmActorByActorIdAndFilmId(actorId: Int!, filmId: Int!): FilmActor
# ...
}
The convention followed here for generating the query name is same as above except that primary key columns are sorted by their names and concatenated with And
.
Sample Queries
# Table Name: employee
# Primary Key Column Name: id
query {
employeeById(id: 1) {
# Column Name: `first_name`
firstName
# Column Name: `last_name`
lastName
# Column Name: `employee_reports_to_id`
employeeReportsToId
}
}
# Table Name: `country`
# Primary Key Column Name: `country_id`
query {
countryByCountryId(countryId: 1) {
# Column Name: `country`
country
}
}
# Query by UUID primary key is also supported.
# Limited to Postgres alone.
# Table Name: `customer`
# Primary Key Column Name: `id` (of type `UUID`)
query {
customerById(id: "847f09a7-39d1-4021-b43d-18ceb7ada8f6") {
# Column Name: `id`
id
# Column Name: `first_name`
firstName
}
}