is it possible to create multiple records in one r...
# prisma-whats-new
s
is it possible to create multiple records in one request without "naming" the records?
p
Copy code
require("dotenv").config();
const Bluebird = require("bluebird");
const _ = require("lodash");
const { Lokka } = require("lokka");
const { Transport } = require("lokka-transport-http");

const headers = {
  Authorization: "Bearer ${GRAPHQL_ACCESS_TOKEN}"
};

const client = new Lokka({
  transport: new Transport(process.env.GRAPHQL_ENDPOINT, { headers })
});

const createAttendees = async () => {
  const mutations = _.chain(_.range(1000))
    .map(
      n =>
        `{
        post: createAttendee(text: "${n}") {
          id
        }
      }`
    )
    .value();

  await Bluebird.map(mutations, m => client.mutate(m), { concurrency: 4 });
};

const queryBatch = () => {
  return client.query(`
    query getAttendees {
      posts: allAttendees(first: 250) {
        id
      }
    }
  `);
};

const deleteBatch = async () => {
  console.log("Fetching new nodes");
  const posts = (await queryBatch()).posts;

  if (posts && posts.length > 0) {
    console.log(`Deleting next batch of ${posts.length} posts...`);
    const mutations = _.chain(posts)
      .map(
        post =>
          `{
          deleteAttendee(id: "${post.id}") {
            id
          }
        }`
      )
      .value();

    await Bluebird.map(mutations, m => client.mutate(m), { concurrency: 4 });
    await deleteBatch();
  }
};

const main = async () => {
  // set to true to create test data
  if (false) {
  } else {
    // query total posts:
    const postsMeta = await client.query(`{
      meta: _allAttendeesMeta {
        count
      }
    }`);

    console.log(`Deleting ${postsMeta.meta.count} posts...`);
    await deleteBatch();
  }

  console.log("Done!");
};

main().catch(e => console.error(e));
can’t find thel ink haha, but here’s something htat might help you
n
@signor_busi you can include multiple different mutation without using GraphQL aliases
but you can't do so with the same mutation
the reason is simply because the result JSON uses the mutation name as a key
s
ok, thanks for clarifying!
a
There's actually a nice undocumented feature with regards to aliases. You can put more than one of the same mutation in one request without alias, as long as you make sure the field names are unique. The nice bonus is that the API will combine the results into one result object.
Copy code
mutation{
  createPost(description: "blah"){
   firstDesc: description
  }
  createPost(description: "more blah"){
   secondDesc: description
  }
}
Will result in:
Copy code
{
  "data": {
    "createPost": {
      "firstDesc": "blah",
      "secondDesc": "more blah"
    }
  }
}
n
interesting
is this per spec?
a
The result field names need to be unique. If not, use aliases. That's per spec. That can be on mutation, or on field, the result is the same: unique identification of result fields.
Don't know if it's explicit in the spec, but it works on graphcool
I use it as a convenience method when generating mutations in code, because I get one result object back, instead of multiple objects with one field
n
so in case of creating many objects, you map over the properties of the one result instead of mapping over many results
a
Yes, it saves one level of nesting
n
definitely a cool trick
a
Could be one for my 'out of the box' blog series that I have absolutely no time for in probably the next year 😃
😎 2
n
put it on the list 😄
a
You can also use this trick to force transactions
n
how so?
a
Given this mutation (the result = 'ok' field is set by a RP hook):
Copy code
mutation{
  createPost(description: "blah", result:""){
   result
  }
  createPost(description: "more blah", result:""){
   result
  }
}
If ALL posts are created succesfully, I get a single result:
Copy code
data {
   createPost {
      result: "ok"
   }
}
If not, I get an error:
Copy code
{
  "data": null,
  "errors": [
    {
      "message": "Field 'createPost' conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.
 <clipped>
And ALL mutations fail.
n
what hook point is the RP function?
a
TRANSFORM_ARGUMENT
, that's the only one where I can change the
result
field I passed in
n
that's very interesting
now I understand what's going on
it's not really a transaction but I see what you mean
a
No it's not, but it works as one. All mutations fail because of this error.
So if 9 are "ok" and only 1 is "nok", no nodes are created.
n
ya indeed that's slick
😎 1
a
Actually it always works like this on batch mutations, but having the results collapsed into a single 'result = "ok"' field is very nice to handle on the client