I was trying to make a "hello world" for postgraphql but I got some strange errors on an empty database. Just running via app.use(postgraphql(url)); in a node server and browsing to the root, the browser displays:
{"errors":[{"message":"Mutation fields must be an object with field names as keys or a function which returns such an object."}]}
and the terminal running the express server also shows an error:
GET / 500 10.432 ms - -
Error: Connection terminated
at .<anonymous> (/Users/lacker/graphql-hacks/postgraph/node_modules/postgraphql/node_modules/pg/lib/client.js:189:29)
at g (events.js:286:16)
at emitNone (events.js:86:13)
at emit (events.js:185:7)
at Socket.<anonymous> (/Users/lacker/graphql-hacks/postgraph/node_modules/postgraphql/node_modules/pg/lib/connection.js:66:10)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at TCP._handle.close [as _onclose] (net.js:492:12)
Am I doing something wrong here, or is this unsupported, or is this a bug?
This is interesting, yeah in an empty database there wouldn't be any mutations (or queries for that matter!) so GraphQL would throw an error. We should really provide a better error message in this case.
If you want a good example schema to play around with, check out the forum example which also has some simple seed data 馃憤
OK I had this mental model where I could alter the table over time and start with an empty database but now I see that is not how things are supposed to work and running postgraphql on an empty database is just not how it's supposed to go. I've probably been thinking in mongodb too much. Thanks for the pointer to the example schema
Hmm, I've never thought about it that way. You just gave me some good ideas, thanks 馃槉
Opened #129 as a tracking issue for a better development flow with PostGraphQL. What if we allowed you to run SQL queries in GraphiQL in development as you setup your schema?
It's getting off topic here but I have actually been trying to use PostGraphQL without writing any SQL, just writing JavaScript. I dunno if that's in scope for PostGraphQL as a project but IMO that would be appealing to the largest audience. So I was trying to see if you could connect some ORM like sequelize to configure the tables (working for me now) and then have hooks somehow to run application logic in the Node server where PostGraphQL is mounted (I still dunno if that's possible). I really like PostGraphQL's strategy of having the GraphQL API closely coupled to the database schema - that philosophy is very much how Facebook operates internally - but I think you also need to be able to run validation / other logic in a normal application server environment like node.js rather than in postgres.
You might want to read this issue https://github.com/calebmer/postgraphql/issues/87 :blush:
Tight coupling with the database is first and foremost a quality constraint. I wanted to create something actually useful to people first. My goal is to get PostGraphQL to the point where it can support any backend (MySQL, MongoDB, etc.) maybe with a bit of JavaScript code, and any frontend (GraphQL, REST, Falcor, etc.).
Of course the part where we diverge into more database backends and API frontends is far away, I want to focus on making Postgres + GraphQL as powerful as possible. But a great near term side effect of this is that we should be able to extend PostGraphQL with validation / other logic in JS like you mention hopefully pretty soon :wink:
If you want to follow development check out the next branch. I haven鈥檛 started thinking about what a good JS side API would look like (focusing on implementing the Postgres/GraphQL internals first). I鈥檇 love contributions in that area :blush:
Just a note, PostGraphQL is implementing a --watch flag (#166). So now you can just start up a PostGraphQL server from the CLI and add all the tables, columns, schemas, functions you want while PostGraphQL automatically restarts. Thanks for sharing your frustrations :+1:
Most helpful comment
Just a note, PostGraphQL is implementing a
--watchflag (#166). So now you can just start up a PostGraphQL server from the CLI and add all the tables, columns, schemas, functions you want while PostGraphQL automatically restarts. Thanks for sharing your frustrations :+1: