mgagliardo91 / postgraphile-federation-plugin

GraphQL Federation support via Postgraphile Plugin
MIT License
6 stars 2 forks source link

Error: GraphQL schema is invalid: Union type _Entity must define one or more member types #146

Open RuiLoureiro opened 2 years ago

RuiLoureiro commented 2 years ago

I am trying to run postgraphile v4.12.9 with this plugin, using the provided docker-compose.yml, but I'm getting the following error:

Error: GraphQL schema is invalid:
- Union type _Entity must define one or more member types.
    at SchemaBuilder.buildSchema (/usr/local/lib/node_modules/graphile-build/node8plus/SchemaBuilder.js:344:15)
    at Object.createPostGraphileSchema (/usr/local/lib/node_modules/postgraphile-core/node8plus/index.js:227:28)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async createGqlSchema (/usr/local/lib/node_modules/postgraphile/build/postgraphile/postgraphile.js:123:33)

To reproduce:

  1. npm install postgraphile @graphile/federation
  2. docker-compose up
  3. postgraphile -c postgres://user:pass@localhost:5432/db --append-plugins @graphile/federation
RuiLoureiro commented 2 years ago

The error goes away when passing a superuser in the connection string, to postgraphile (instead of user), so I guess this plugin is trying to access something it doesn't have access to.

mgagliardo91 commented 2 years ago

@RuiLoureiro thanks for raising the issue. There are two things at play here:

  1. The default README for this repo needs to change - you're installing the default @graphile/federation package which is not this fork. The lack of update to this README was due to the not knowing whether it would be merged in to the main repo, or left as a separate fork. I will get the README updated to reflect it:
npm install postgraphile postgraphile-federation-plugin
docker-compose up
postgraphile -c postgres://user:pass@localhost:5432/db --append-plugins postgraphile-federation-plugin
  1. The error is being thrown because there are no entities defined in a default database. Basically, without any information, there is nothing to federate. That being said, we should be able to make this not fail the build and instead handle it gracefully - I will get a PR up to reflect that change. In the meantime, if you create a table in the database, the plugin should work.
RuiLoureiro commented 2 years ago

@mgagliardo91 Thank you for your reply. I have updated the package name, but I'm still getting the same error on a non-empty database. With the postgraphile-federation-plugin plugin, I get the error and graphiql shows No Schema Available. If I remove the plugin, everything works.

RuiLoureiro commented 2 years ago

I tested again that if I give postgraphile a super user (in the connection string), the error goes away.

I'm creating the user for postgraphile with

CREATE ROLE postgraphile login;
GRANT usage ON SCHEMA my-schema TO postgraphile;

Does it need any additional permissions?

RuiLoureiro commented 2 years ago

Ok I found that the error is happening because I have a table without a primary key The following migration causes the error:

CREATE SCHEMA myschema;

CREATE TABLE myschema.users (
    user_id          TEXT NOT NULL,
    user_name        TEXT NOT NULL
);
CREATE ROLE client;
CREATE ROLE postgraphile login;

GRANT usage ON SCHEMA myschema TO client;
GRANT usage ON SCHEMA myschema TO postgraphile;
GRANT SELECT ON TABLE myschema.users TO client;

grant client to postgraphile;

If I add a primary key (PRIMARY KEY(user_id)), everything works. Is having a primary key on tables a requirement to using this plugin? In our use case, because we are using timescaledb, some tables do not have primary keys.