when using a create mutation, how can I supply a c...
# prisma-whats-new
m
when using a create mutation, how can I supply a custom ID?
a
@mike.johnson You mean something like:
Copy code
mutation {
   mySpecialMutation: createPost(...) { id }
}
m
no, i mean to actually pass the ID in the createPost
i have SQL tables users, posts, etc. I am using a curl mutation to create each row of each table
a
ID fields are always auto-generated, you cannot supply them, only with update, not with create
m
if I create users with grahpcool ids, and then my posts table references
user_id
it will be set to the old db user id
a
You could create an
oldId
field that you use
m
ok, so I create oldId, then run a mutation to set the REAL id to the old id?
a
Yes, I'm writing the import feature for the cli at the moment, I keep a local list of { typeName, id, oldId }
And I use that to make the connections, so when you create mutations for the Posts, you would do a lookup in your local list, and replace the user_id value before sending the mutation
m
is there a drawback to this approach? For example, I read somewhere that apollo needs the IDs to be unique in order to update properly
a
The actual ID's will still be generated by graphcool, so they will be unique
m
oh, so you mean instead of querying by ID I will just always query by oldID?
or do you mean supply oldID to the connection itself? Can I do that when defining a relation?
a
No. 1. You insert all users, returning the new ID from the mutation, and keep a list of the oldId and new ID 2. When you insert posts, before sending the mutation, you replace to old reference Id user_id by the new id from your local lookup list
So you would get a new user record, with a new ID, and a new post, with a new ID, and connect the post to the new user id
m
Right, but in this case I'm not importing, I'm sending the data TO the client from a backend
a
When querying your API from graphcool, you would always use new id's after you're done with the import
m
er, not client, but graphcool
a
I thought you said you have data in SQL tables, and your creating mutations to put the data into graphcool?
m
yes, FROM inside of the SQL application
so I won't know on that side what IDs graphcool wll assign for users when I go to make the posts
so if I just get all the data into graphcool with
oldID
fields, both for posts and users, how would I then re-map all the posts
user_id
fields to the new usersID?
a
I understand, so in that case you can generate all mutations first from the SQL application, but you need to use temporary fields in graphcool to hold the foreign keys
And then afterwards, you need to run 'some' script to make the actual connections
The method I'm using takes import files, and executes the mutations right away, so I get the new ID's back, and use them to replace the old foreign keys, but if that's not possible for your use case, then you need to do it like this
m
how close are you on this script 🙂
a
You can check the PR on the import feature: https://github.com/graphcool/graphcool-cli/pull/117. The flat import is there, the 'making the connections' part is about two days ahead.
m
so I should just wait a few more days?
a
Only if it fits your use case to execute the mutations 'on the fly' and you have a way to export the data first into a file that the import can read. Even then, it's still a PR, so it will take some time to be 'production-ready' after that.