Is there any way of integrating an existing databa...
# prisma-whats-new
g
Is there any way of integrating an existing database with GraphQL, or if I have a schema generated (say using PostGraphQL/Postgraphile's introspection to generate a GraphQL .gql or .json schema file from the data structure) to import that?
m
Are you referring to GraphQL or Graphcool? You could use Express to build a GraphQL server in front of your existing database
g
Graphcool, to take an existing database and either have Graphcool introspect it or use another tool to introspect and then port the structure from a file. I have an Express server with Middleware operational but a Graphcool implementation would be better and more manageable
e
Ok, so if your concern is bootstrapping the project from an existing schema, that is doable via the Graphcool CLI:
Copy code
graphcool init --schema <https://graphqlbin.com/instagram.graphql>
Just replace the uri with a location of your schema, then you can run a migration script to move the data over (that is not going to be so simple), but the above command would prepare a graphcool project to receive the data
💯 1
g
Oh wow, I wasn't aware of that. Where can I find an example migration script?
e
Well a migration script is going to depend on your data
Basically, make some node script that you can run on command, and it will hit your existing server to pull the data for each type, and then make a mutation to create each record that was fetched from your DB
If that makes sense.
Copy code
For some type:
    allRecords = getAllRecordsOfSomeType
    for record in allRecords:
        createNewRecordOfThatType
g
Ohhh, you'd need something like Knex then
to connect to DB and pull records
e
What database are you using right now?
g
Postgres
e
Ok, so however you currently would run a query on that data, do so, and then you can use something like graphql-tag to dynamically create the mutations in your script
I tend toward MySQL for a RDB, but the process should be relatively simple. Whatever node package you would need to get the data, you would follow the pseudocode I provided
But yes, Knex is one of many tools you could use for this
g
👍
e
I would say start with one record of one type, just in case you fuck up
a
If you have a way to export the data to CSV or JSON, the import process is more straightforward
e
This^
But depending on the database tools, that may not be a straightforward process.
The migration is as dirty as it would get, but is still certainly manageable
So for Postgres, it seems the export + import route could work well
g
PostgraphQL/Postgraphile already does this, I have a massive export of my table data to GraphQL format
it does either gql or json
I mean table structure/associations
a
What does exporting data to gql mean?
g
Setup with Relay connections
a
Okay, table structure
g
Edges + Nodes
a
You mean it generates mutations?
g
Yes
Full CRUD
My issue with it is it's lack of easy extensibility and authentication. It implements a JWT system where it decodes a token sent in, but the authorization the author suggests is to use Postgres Row-Level Security policies on user roles, contained as a field within the JWT
so like
a
I think you could tweak Postgraphile's Graphile build plugins to output a schema that is compatible with Graphcool
But you still need a way to export/import your data
g
I setup a really shitty middleware system
I've never worked in Node or Express before

https://i.imgur.com/Ihl1x6L.png

a
So, what is your exact issue now? Do you want to go from your Postgres database to Graphcool with its own database, or do you want something else?
g
I was just curious if there was a way to import to Graphcool because it's much more flexible
a
Well, like I said, there is. You can either manually create your schema in Graphcool, or you can tweak Postgraphile to output a schema you can use directly in Graphcool. Then import your data from CSV/JSON like in the forum link I sent.
g
Okay cool, thank you!
a
Just to be clear, you can not connect from Graphcool to your Postgres database, so it's an all or nothing decision to move to Graphcool
g
I'm aware, even Postgraphile is just middleware that uses table introspection to generate a GraphQL server
I think the easiest approach might be a hybrid system using the existing Rails + Postgres code, and then possibly only exposing certain model types to a GraphQL endpoint
a
It depends. If you want to keep the data in the Postgres database, you can't use Graphcool
g
This undertaking was much much more complex than I thought. I've spent 4 days putting in 8-14 hours a day trying to wrap my head around integrating a data layer, authentication, and use/types of GraphQL plus Apollo
Holy mackerel it's not the most straightforward thing. Biggest issue being auth/which fields to expose.
s
postgraphql has an option
--export-schema-graphql
that might be interesting