I have the following db setup
| event_id | event_name |
|----------|------------|
| 1 | Test |
| 2 | World |
| award_id | event_id | award_name |
|----------|----------|-------------|
| 1 | 1 | greatness |
| 2 | 2 | really good |
and I am trying to query the db as follows:
{
allAwards {
edges {
node {
awardId
eventByEventId(eventId: eventId) {
year
name
country
location
date
dateCreated
}
}
}
}
}
With the following error:
Cannot query field "eventByEventId" on type "Award". Did you mean "eventId"?",
The error you are receiving suggests that you don't have a foreign key constraint between the tables (i.e. references events
), see relations in the PostGraphile docs. Your query is also invalid in that it's adding arguments that do not exist to the eventByEventId
field: the eventId
is implicit as it already exists on the record you're querying. If you use GraphiQL or a similar GraphQL IDE it should show you where your query does not match the GraphQL schema, and even give you hints as to how to fix it.
I've recreated your schema with the following SQL:
create table events (
event_id serial primary key,
event_name text
);
insert into events (event_name) values ('Test'), ('World');
create table awards (
award_id serial primary key,
event_id int references events, -- < THIS IS THE IMPORTANT BIT
award_name text
);
insert into awards (event_id, award_name) values (1, 'greatness'), (2, 'really good');
And then ran the latest postgraphile against it:
$ npx postgraphile@latest -c deleteme
npx: installed 119 in 11.26s
PostGraphile v4.4.1 server listening on port 5000 🚀
‣ GraphQL API: http://localhost:5000/graphql
‣ GraphiQL GUI/IDE: http://localhost:5000/graphiql (enhance with '--enhance-graphiql')
‣ Postgres connection: postgres:///deleteme
‣ Postgres schema(s): public
‣ Documentation: https://graphile.org/postgraphile/introduction/
‣ Join Jimmy McBroom in supporting PostGraphile development: https://graphile.org/sponsor/
* * *
And issued the GraphQL query:
{
allAwards {
edges {
node {
awardId
eventByEventId {
eventName
}
}
}
}
}
This all works as expected:
PS: I'd recommend you use the @graphile-contrib/pg-simplify-inflector plugin to automatically simplify the names of the various relations. With this plugin, the command line would be:
postgraphile -c deleteme --append-plugins @graphile-contrib/pg-simplify-inflector
And the query:
{
awards {
edges {
node {
awardId
event {
eventName
}
}
}
}
}