Trying to get a rails application mograted over to...
# prisma-whats-new
m
Trying to get a rails application mograted over to graph.cool. I thought it would be easy to write mutations for each rails model in a rake task, but I'm having trouble with rails thinking the query fields are symbols to be processed... What's the best way to get data from an exisiting app into graph.cool?
c
@mike.johnson I take it you're using a sql db with rails? If so, it should be pretty easy as both are "relational", just create the same table and columns in graphql.cool as you have in your sql db and then just write a bunch of rake tasks using batching to migrate over the data
m
right, but that's my problem, I'm not sure how to write the post request in the rake task
in the mutation for a "createUser", I get this error
NoMethodError: undefined method `split' for :"content-type"🔣
as if it's thinking the query fields are symbols in rails
n
as if it's thinking the query fields are symbols in rails
how are you coming to this conclusion based on the error message?
it sounds like a problem with the header to me.
how do you typically to plain HTTP POST requests in Ruby?
m
slack turned part of the error into an emoji
n
😂
m
typically? I don't 🙂
n
well there is some client I'm sure
m
i found HTTParty gem, but sending the body request with gql is what casues the above error
Copy code
response = <http://HTTParty.post|HTTParty.post>('<https://api.graph.cool/simple/v1/__myid__>',
		headers: {'content-type': 'application/json'},
		body: {"query":"query {allUsers {id name}}"}
	)
when it hits
"query": query
the error is pointing at the second query
just trying to get a query to work before sending a mutation
n
I still don't see how that error message relates to GraphQL at all
clearly something is wrong about how you setup the headers
m
usually in rails if you try to reference an object without using : like
after_create :update_access_token!
if I forget the
:
it gives a very similar error
n
ok
m
maybe i could just use curl/
n
I recommend first getting this HTTP client to run using requestb.in
and sure you can use curl, too
m
ah, backticks are special in rake tasks
that could be tripping it up
n
I don't see a backtick there
m
JSON.stringify({ query: `
n
that's not what you pasted before 😛
m
true nvm.
was looking at the link
n
oh I was refering to the
curl
command there
not the
fetch
one
m
ah, ok. Yeah I think that might be easier
rather than getting the http library to work in rake
and able to parse the queries properly
what does --compressed and --data-binary do?
n
--compressed
is just for optimization,
--data-binary
makes it a POST by adding a body
m
so for a muation it would be something like
'{"mutation": "createUser { id name email }"}'
n
no
m
right, need to pass the varialbes
n
Copy code
curl '<https://api.graph.cool/simple/v1/__PROJECT_ID__>' -H 'content-type: application/json' --data-binary '{"query":"mutation {createUser(name: "Nilan") { id }}"}' --compressed
note that it's still
query
for the JSON property, and the second query is replaced by
mutation
m
ah, i see
n
actually I'll add that to the example, thanks for the question
m
now, what about passwords? 🙂
every user has one, and it's encrypted on the rails end in the db
I'll have to make everyone reset their password after migrating, right?
n
do you know the encryption algorithm and so on?
m
no, I don't, it's wrapped in the Devise gem
n
well, then unfortunately yes
m
Is there a guide for a forgotten password workflow?
enter email, send user email with password token and link to a page that will allow them to update password
n
not yet
m
Are you sure this is right?
{"query":"mutation {createUser(name: "Nilan") { id }}"}
the " before Nilan closes the " for "mutation
and gives `Unexpected character 'N' at input index 40
n
are you using this as curl?
m
yes
n
or somewhere else
ok let me quickly double check, sorry 😄
m
no worries. I have to wrap the curl in backticks as well, otherwise it closes the first set of quotes too
Copy code
`curl '<https://api.graph.cool/simple/v1/__ENDPOINT__>' -H 'content-type: application/json' --data-binary '{"query":"mutation {createUser(name: "Nilan") { id }}"}' --compressed`
n
Copy code
curl '<https://api.graph.cool/simple/v1/__PROJECT_ID__>' -H 'content-type: application/json' --data-binary '{"query":"mutation {createUser(name: \"Nilan\") { id }}"}' --compressed
this should work
m
ah, there we go
let me try that
still,
Copy code
syntax error near unexpected token `('
sh: -c: line 1: `Unexpected character 'N' at input index 38 (line 1, position 39), expected '}':'
n
😄
please just follow this quick video
apparently I'm too incompetent for this haha
m
have to run to a meeting, dang
Yeah, I've tried copying it directly and it wont work in the rails env
it works fine in the console
n
content type needs to be
application/json
and instead of
user
you need to call it
query
m
got a curl working, but it's going to be really tricky getting it to work with all the fields needed
sh 'curl \'https://api.graph.cool/simple/v1/__API__\' -H \'content-type: application/json\' --data-binary \'{"query":"mutation {createUser(name: \"' + "#{user.name}" + '\"){id}}"}\' --compressed'
have to wrap the curl in single-quotes, then escape all the other single-quotes, but keeping the double quotes
😂 1
but it worked, finally
n
this sounds adventurous
m
so, now I can get names in there, but what about relationships?
users have a bunch of purchases, so when using createUser mutation do I pass in the purchases_id as well? then when I create the purchaseMutation I'll have to use it's real id?
n
yea
I think it's best to wait for the import command 🙂
m
i have until next week
🙂
have a demo
n
got it
well you can use nested mutations
m
what is the timeline on the CLI ?importer
alternatively, can I create the users ID at mutation time? I thought that was unique/protected
n
you can't. there's no time line, it's an ongoing thing
m
agartha was mentioning storing the railsID as a new field on the node, then running a second mutation to convert any connections
Trying to get users and purchases in with fields like
railsId
and
railsUserId
on purchases
but then I need to run a query on allPurchases, query for a user for each ones
railsUserId
and then run a mutation to set
purchase(user: $userFromQuery)
does that sound like it would work?