Graphql insert a child while indicating his parent - graphql

I'm very new into graphql but I'm able to insert a new "notification" that belongs to a "Tienda" which means store, however I'm not able to indicate to which store it belongs to.
This is the schema
type Tienda #model {
id: ID!
name: String!
cliente: [Cliente] #manyToMany(relationName: "clienteDeTienda")
news: [Notificacion] #hasMany
}
type Cliente #model {
id: ID!
name: String!
stores: [Tienda] #manyToMany(relationName: "clienteDeTienda")
}
type Notificacion #model {
id: ID!
store: Tienda #belongsTo
content: String!
}
This is how I insert a new notification:
const response = await API.graphql(graphqlOperation(createNotificacion, {input:data}))
This is what data has inside:
Object {
"content": "Nueva notificacion",
}
And this is the response I get from console.log(response):
Object {
"data": Object {
"createNotificacion": Object {
"content": "Nueva notificacion",
"createdAt": "2022-11-13T18:04:55.000Z",
"id": "9f50d333-fc9e-478f-be6a-c54275797a27",
"store": null,
"tiendaNewsId": null,
"updatedAt": "2022-11-13T18:04:55.000Z",
},
},
}
As you can see "content" was added to the notification but "store" is null but I haven't been successful in inserting the store ID in there.
This is how the mutations is defined:
export const createNotificacion = /* GraphQL */ `
mutation CreateNotificacion(
$input: CreateNotificacionInput!
$condition: ModelNotificacionConditionInput
) {
createNotificacion(input: $input, condition: $condition) {
id
store {
id
name
cliente {
nextToken
}
news {
nextToken
}
createdAt
updatedAt
}
content
createdAt
updatedAt
tiendaNewsId
}
}
`;
I've tried with the following 3 methods which give no error but still doesn't link to it's parent:
API.graphql(graphqlOperation(createNotificacion, {input:data, store:{id:'55988776-11af-42b8-b93b-9de9af35f7dc'}}))
And
API.graphql(graphqlOperation(createNotificacion, {input:data, store:'55988776-11af-42b8-b93b-9de9af35f7dc'}))
And:
API.graphql(graphqlOperation(createNotificacion, {input:data},{store:'55988776-11af-42b8-b93b-9de9af35f7dc'}))
Please somebody point me the right path or documentation about how to do this. I need to know how to perform the graphqlOperation correctly.
Thank you very much

In the end what I had to do is this:
const response = await API.graphql(graphqlOperation(createNotification, {input:{...data, storeNotificationsId:'someValidStoreId'}}));

Related

cannot delete a product from db using graphql

Schema:
type Mutation {
removeProduct(id: String!): Product
}
type Product {
id: ID!
name: String!
slug: String!
description: String!
price: Float!
image: String!
ingredients: [String]
addOns: [String]
}
resolver:
exports.Mutation = {
removeProduct: async (parent, { id }, { Product }) => {
const deletedProduct = await Product.deleteOne({ id });
return deletedProduct;
},
};
query:
mutation{
removeProduct(id:"b55572b9-eb4d-46ea-a82f-f01cb0ba3993")
{
id
name
}
}
I need to remove a product from the db, but while deleting it, the response from graphql shows that "message": "Cannot return null for non-nullable field Product.name.",
It maybe because the product is deleted and nothing is returned because there is no product in db.
How do I write a query to delete a product ?
Note that it deletes the product from the db, but shows error in graphql response.

Cannot read properties of null (reading '_doc')

I'm running a query in GraphQL and getting this error:
{
"errors": [
{
"message": "Cannot read properties of null (reading '_doc')",
"locations": [
{
"line": 38,
"column": 5
}
],
"path": [
"songs",
0,
"creator"
]
}
],
"data": null
}
This is the query.
query {
songs {
song_file_name
song_type
song_size
user_name
creator {
email
}
This is my resolver file, and this is the schema.
const { buildSchema } = require('graphql');
module.exports = buildSchema(`
type Song {
_id: ID!
song_file_name: String!
song_type: String!
song_size: Int!
user_name: String!
creator: User!
}
type User {
_id: ID!
email: String!
password: String
createdSongs: [Song!]
}
input SongInput {
song_file_name: String!
song_type: String!
song_size: Int!
user_name: String!
}
input UserInput {
email: String!
password: String!
}
type RootQuery {
songs: [Song!]!
}
type RootMutation {
createSong(songInput: SongInput): Song
createUser(userInput: UserInput): User
}
schema {
query: RootQuery
mutation: RootMutation
}
`);
Could anyone help me figure out what I'm doing wrong? It looks like creator is returning null, but it is properly defined as far as I can tell. What's causing this?
Also, can anyone suggest a good place to learn GraphQL? I'm following this guide and though I find it useful and informative, it quickly gets to a level where I don't feel I fully understand what I'm doing although I'm following along.
I know the basics of GraphQL, but the relational concepts is hard for me to wrap my head around.

Unable to fetch component data using Graphql in Strapi

I am building Graphql api with Strapi. It's a simple application which user can signup and save some of his/her information. I have two tables for that, User [Which is Strapi provided] and Profile which i use to store user additional information. The two tables has relationship
User [User has and belongs to one Profile]
Profile Profile has and belongs to one User and contact component structure.
I want to be able to fetch the current logged in user and it's information from Profile table, but the Contact field which i save as component type return null.
Here is what i am doing so far
extensions/user-permission/config/schema.graphql.js
module.exports = {
definition: `
extend type UsersPermissionsMe {
profile: ProfileMe
}
type ProfileMe {
id: ID!
website: String!
description: String!
contact: ContactMe
}
type ContactMe {
id: ID!
name: String!
phone: String!
email: String!
}
`
}
extensions/user-permission/services/User.js
'use strict';
module.exports = {
fetchAuthenticatedUser(id) {
return strapi.query('user', 'users-permissions').findOne({ id }, ['role', 'profile']);
},
};
And when run GraphQl Query
query {
me {
username
email
profile {
website
description
contact {
name
phone
email
}
}
}
}
it returned
{
"data": {
"me": {
"username": "company",
"email": "company#test.com",
"profile": {
"website": "http://localhost.com",
"description": "lorem text",
"contact": null
}
}
}
}
I want to also get contact data here. I hope someone on Internet can help me.
please help, thanks you.

Linking schema in Apollor server 2

I have a case that I'm not sure to implement, I have a REST API that returns an array of user IDs. Like this:
{
status: ""
"users": [
{
"userId": ID
},
{
"userId": ID
}
]
}
I want to have the ability to get the user details for each userID, so I want to pass the ID to another API that gets the user details.
The issue is that the second API returns the information as follows
{
status: ""
data:[{
Info:{
userId:
name:
...
}
Permissions{
...
}
...
}]
}
My issue is how can I represent this relation in the schema?
This is the schema I came up with:
Query{
EventUsersSummary(eventId: ID): EventResponse!
}
type EventResponse{
status: String!
users: [EventUser!]!
}
type EventUser {
userId: ID!
}
type User {
info: Info
permissions: Permissions
....
}
type Info{
userId: ID
firstName: String
lastName: String
email: Email
}
....
In the resolver for EventResponse I'm removing status from the result. In the resolver I plan to return the information for each user.

How can I make this as optimisticResponse?

I have a "like" button/mutation. There is still a split second while I get a response from the server. How can I make this as an optimisticResponse?
Here's my mutation:
const LIKE_MUTATION = gql`
mutation LIKE_MUTATION($postId: ID!) {
createLike(postId: $postId) {
id
post {
id
title
text
createdAt
author {
id
}
likes {
id
user {
id
}
}
}
}
}
`
Now what I'm trying to do in my Mutation component is:
optimisticResponse={{
__typename: 'Mutation',
createLike: {
__typename: 'Like',
id: this.props.post.id,
post: this.props.post,
likes: this.props.post.likes.concat({
id: 'vcjsvdcjscd',
user: { name: 'sample', id: 'jsdckbsdjcbdjs' },
}),
},
}}
the like is connected to post and to user in my schema. I'm trying to provide fake user id and name here. It doesn't work. I get this in the console as well:
Missing field id in {
"name": "user",
"__typename": "User"
}

Resources