Is it possible to create a document with an id using the graphql API?

I would like to know if it is possible to create a document with a predetermined id using the graphql API.
This would be in the case of writing a database import script that would use graphql instead of FQL.

You will need to create a custom resolver to do this from GraphQL.

The FQL for creating a Document with a specific ID is to provide a Ref, rather than a Collection, to the Create function. Like this:

Create(Ref(Collection("Thing"), "101"), {
  data: {
    value: true 
  }
})

If you had a schema like this:

type Thing {
  value: Boolean!
}

# do not name your custom input the same 
# as the default generated input for your type
input ThingWithIDInput {
  value: Boolean!
}

type Mutation {
  CreateThingWithID(id: ID!, data: ThingWithIDInput!): Thing @resolver
}

You can define the resolver like this:

CreateFunction({
  name: "CreateThingWithID",
  body: Query(Lambda(
    ["id", "data"],
    Create(
      Ref(Collection("Thing"), Var("id")),
      { data: Var("data") }
    )
  ))
})
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.