How can I refer to an object while creating another object in GraphQL? - graphql

I've been trying to build an app and for brevity, assuming that I only have two types of objects with the following fields:
User: user_uid, name, Settings(reference)
Settings: settings_uid, some_settings_json
So far, I've tried creating Settings object first then linking the ready Settings object to the User object getting created.
Creating the settings object:
mutation createSettings {
createSettings(data: {some_settings_json: "{"some_key": "some_value"}" }) {
settings_uid
}
}
Creating the User object:
mutation createUser {
createUser(data: {name: "some_user", settings: <SETTINGS_OBJECT>) {
user_uid
}
}
Although I've checked documentations, tutorials, web; I haven't been able to figure out how to replace <SETTINGS_OBJECT>, so that these two objects would get linked. Note: I don't want to create settings while user is getting created. For that, a tutorial already provides an example.
Any help would be appreciated.

I found it. The link is provided via connect keyword.
mutation createUser {
createUser(data: {
name: "some_name", settings: {
connect: {
settings_uid: "settings_uid"
}
}
}) {
user_uid
}
}

Related

How can I get the project name for a timelog issue entry using the project ID from a Gitlab GraphQL query

I'm using the graphql explorer for gitlab and I'm trying to get timelog entries for a given user (as below).
The response gives me a projectId which I assume I'd have to use in a subsequent query (I don't believe I can get it in the same query?), but the project object doesn't let me find the project by ID, only fullpath.
Am I correct in understanding that I would need to use the projects query and provide the id there? In trying that (query below), I just get the error 12345678 is not a valid GitLab ID. (obviously I'm using a real ID given previously).
So, two points/questions I guess:
The timelog entry provides a projectId which I can't use in either the project or projects query to get the project name?
I can't just return the project name directly in the same query as the original user timelog one?
Any thoughts greatly appreciated.
{
users(usernames: ["user1"]) {
nodes {
id
username
timelogs(startDate: "2022-05-03T09:00:00") {
nodes {
issue {
projectId
}
timeSpent
}
}
}
}
}
{
projects(ids:[12345678]) {
nodes {
name
}
}
}
I don't know if this helps, but I use:
{
group(fullPath: "groupname") {
name
timelogs(startTime: "2022-04-01", endTime: "2022-04-30", username: "roberto") {
pageInfo {
hasNextPage
endCursor
}
nodes {
user {
username
}
issue {
iid
webUrl
state
projectId
title
}
spentAt
timeSpent
summary
note {
body
url
}
}
}
}
}

Hasura: Allow-list configuration

I'm trying to setup the allow list feature in Hasura, but the docs seem pretty sparse. This is one of the queries:
{
hasura_auth(args: {cleartext_password: "xxx", email: "email#mail.com"}) {
jwt_token
}
}
How would I integrate the dynamic parts in an allow list?
I tried this and lot's of variations with no luck:
{
hasura_auth(args: {cleartext_password: $pass, email: $email}) {
jwt_token
}
}
Thanks for your help!
What you have to know is to tell hasura the name of your query with full syntax.
like this...
Operation name is => get_user_by_pk
Operation is
query get_user_by_pk($id: uuid!) {
user_by_pk(id: $id) {
id
username
email
}
}
the main part is you have to use the exact operation in your code having the operation name.
now, in your project, you will send the variable (in this case id[uuid]) to the query handler and send this to your hasura server.
ask me if it is not clear for you.

GraphQL mutation in KeystoneJS: "Cannot use 'in' operator to search for 'id' in undefined"

In a KeystoneJS GraphQL project I'm trying to create a new data object (an "Article") in the 'resolveInput' hook of another, existing, data object (a "Proposal" -- when a Proposal is approved, I create an Article based on that Proposals'data).
This worked fine using the Mongoose adapter, but I've tried to do it using the built in GraphQL API, using keystone.executeQuery and I get the following error:
My Article list has a relationship with one Proposal (I'm leaving out the other fields)
fields: {
proposal: {
isUnique: false,
type: fields_1.Relationship,
ref: 'Proposal',
access: {
create: true,
read: true,
update: false
}
}
}
and I create the new Article thus (I'm omitting some code)
hooks: {
resolvedInput: async params => {
const articleCreateInput = {
title: cleanTitle,
text: cleanText,
visible: HIDDEN,
proposal: {
connect: {
id: proposalId
}
}
};
const articleCreationResult = await keystone.executeQuery(createArticle, { variables: articleCreateInput });
}
}
As far as I can see this is the correct way to do it, using connect, connecting an existing item to one you are creating.
My query is
const createArticle = `mutation createArticle($data:ArticleCreateInput) {
createArticle(data:$data) {
id
title
visible
publishDate
}
} `;
and as far as I can see I'm following the schema correctly
I'm sure I'm making an obvious mistake but at the moment I don't see it -- and I'm not sure whether that mistake is a GraphQL mistake or a KeystoneJS mistake (or both).

How can I insert records in AwsAppSync mutation with proper #connection values?

I have added a resources table to my schema, connecting to a Plants table:
type Resource #model
{
id: ID!
name: String!
Plants: [Plant] #connection(name: "ResourcePlant")
}
Ran amplify push, and all resources were created properly.
Now I wanted to add a Resource, and link it to all Plants properly.
Do you know how is the sintaxe I should use to run the recently created mutation createResource in order to add the items on Plant I want to include to that resource?
I tried to run like this:
mutation CreateResource {
createResource (input: {
name: "Plant",
Plants : {
items :
{ id: "f9a0468e-da74-41d5-8287-1cb6a76b25a5" }
}
}
) {
name,
Plants {
items {
id
}
nextToken
}
}
}
This was the error message:
Validation error of type WrongType: argument 'input' with value
'ObjectValue{objectFields=[ObjectField{name='name',
value=StringValue{value='Plant'}}, ObjectField{name='Plants',
value=ObjectValue{objectFields=[ObjectField{name='items', value=ObjectValue{objectFields=[ObjectField{name='id',
value=StringValue{value='f9a0468e-da74-41d5-8287-1cb6a76b25a5'}}]}}]}}]}'
contains a field not in 'CreateResourceInput': 'Plants' # 'createResource'
How did you define Plant?
And have you checked this example? https://aws-amplify.github.io/docs/cli-toolchain/graphql#connection
Ok, after some headache, I found what was missing in my model. For me so far it has proved to be the best way of doing this relationship...
I have added on my Plant type, on schema definition, a field named plantResourceId (other than the one used for the #connection directive). What I found out was that, by convention, when inserting/updating a record on "Plant" and adding the resource "id" field content of the resource I want to "connect" to that plant, it will automatically be retrieved when "Resources" is queried, for each item - what is better: Out-of-the-box from codegen.
Insert example
mutation CreatePlant {
createPlant(input:{
name: "MyPlant",
plantResourceId: "id-for-connected-resource"
}) {
name,
plantResourceId
}
}
Query example to retrieve items:
query listPlantsOnResource {
listResources(filter: {
name: {
contains: "myfilter"
}
}) {
items {
id
name
Plants
{
items {
id
name
description
}
}
}
}
}
It worked very well!
Thanks all who contributed!

How to create a new entry in a specific collection?

Or in other words: How can I create a new entry that has a relationship with a specific collection and only with it?
The following code creates a new entry of the type Brand. How can I place it inside a specific collection?
I'm new to graphql, so if what I would like to achieve is not possible at all or not the way graphql works please tell me
Additional information:
I'm using mlab as my back end, and strapi (headless cms). On strapi I installed graphql as an external plugin.
Thanks in advance
mutation {
createBrand(input: {
data: {
name: "John",
description: "this is my description"
}
}) {
brand {
name
description
}
}
}
To my very limited understanding of graphql, I think that in order to associate a new entry with a specific collection you need to edit the code above and add:
mutation {
createBrand(input: {
data: {
name: "John",
description: "this is my description"
collectionInPlurals: ['collectionId']
}
}) {
brand {
name
description
}
}
}
collectioninPlurals for example could be items if the type of the collection is 'item' and then you provide the id of the specific item
I hope it could benefit others...

Resources