How to update part of my amplify graphql model? - graphql

I am new with Aws Amplify and GraphQL and I am trying to find the best way to handle the following:
I want to be able to edit my FAQ in the frontend. The frontend gets the data from the backend, i edit one field in the frontend and save it, it should be stored in the backend
I have created a simple amplify graphql schema:
type FAQ #model(timestamps: null) {
id: ID!
sectionTitle: String
questionAndAnswer: [QuestionAndAnswer!]!
}
type QuestionAndAnswer {
id: ID!
question: String
answer: String
}
Then I populated it so it contains the following data:
{
"data": {
"listFAQS": {
"items": [
{
"sectionTitle": "Title2",
"id": "22735682-2bab-4695-b93e-1e50642d4654",
"questionAndAnswer": [
{
"answer": "answer4",
"id": "4",
"question": "question4"
},
{
"answer": "answer5",
"id": "5",
"question": "question5"
},
{
"answer": "answer6",
"id": "6",
"question": "question6"
}
]
},
{
"sectionTitle": "Title1",
"id": "21be8234-69f2-47fe-b942-d3e655197c70",
"questionAndAnswer": [
{
"answer": "answer1",
"id": "1",
"question": "question1"
},
{
"answer": "answer2",
"id": "2",
"question": "question2"
},
{
"answer": "answer3",
"id": "3",
"question": "question3"
}
]
}
]
}
}
}
Is there an easy way I can only change for instance the question "question4" without having to send the whole FAQ "row"?
When I was experimenting with it it seemed that the whole array was reseted and only the new data was saved. Should both FAQ and QuestionAndAnswer be a #model each where I connect them with #connection and update them separately as needed?

The following seems to be an easy way, as you can update only "question4" if you need, by using its own unique ID with GraphQL mutation.
type FAQ #model(timestamps: null) {
id: ID!
sectionTitle: String
questionAndAnswer: [QuestionAndAnswer!]!
}
type QuestionAndAnswer #model {
id: ID!
question: Question
answer: Answer
}
type Question #model {
id: ID!
question: String
}
type Answer #model {
id: ID!
answer: String
}
I did not include #connections (they should be in QuestionAndAnswer model), but 'Yes' would be the answer to " Should both FAQ and QuestionAndAnswer be a #model each where I connect them with #connection and update them separately as needed?".

Related

Incorrectly selected data in the query

Only articles that contain the EmailMarketing tag are needed.
I'm probably doing the wrong search on the tag, since it's an array of values, not a single object, but I don't know how to do it right, I'm just learning graphql. Any help would be appreciated
query:
query {
enArticles {
title
previewText
tags(where: {name: "EmailMarketing"}){
name
}
}
}
result:
{
"data": {
"enArticles": [
{
"title": "title1",
"previewText": "previewText1",
"tags": [
{
"name": "EmailMarketing"
},
{
"name": "Personalization"
},
{
"name": "Advertising_campaign"
}
]
},
{
"title": "title2",
"previewText": "previewText2",
"tags": [
{
"name": "Marketing_strategy"
},
{
"name": "Marketing"
},
{
"name": "Marketing_campaign"
}
]
},
{
"title": "article 12",
"previewText": "article12",
"tags": []
}
]
}
}
I believe you first need to have coded an equality operator within your GraphQL schema. There's a good explanation of that here.
Once you add an equality operator - say, for example _eq - you can use it something like this:
query {
enArticles {
title
previewText
tags(where: {name: {_eq: "EmailMarketing"}}){
name
}
}
}
Specifically, you would need to create a filter and resolver.
The example here may help.

Idiomatic way of eager loading - how to populate deep nested association with database/sql and pq

I was just wondering how to deal with eager loading queries to retrieve data with several associations, let me explain my models:
type User struct {
Id uint
Name string
Username string
Image string
}
type Post struct {
Id unit
Content string
Author *User // I use user_id in author column for the posts table
Comments []Comments
}
type Comments struct {
Id unit
PostId uint
Message string
CommentBy *User // I use user_id in comment_by column for the comments table
}
This is an example of retrieved data:
{
"Posts": [
{
"Id": 1,
"Content": "test post",
"Author": {
"Id": 1,
"Name": "author",
"Username": "h3ll0",
"Image": "author.png"
},
"Comments": [
{
"Id": 1,
"PostId": 1,
"Message": "good article",
"CommentBy": {
"Id": 2,
"Name": "second user",
"Username": "r3ader",
"Image": "reader.png"
}
},
{
"Id": 2,
"PostId": 1,
"Message": "bad article",
"CommentBy": {
"Id": 3,
"Name": "third user",
"Username": "thirD",
"Image": "third.png"
}
}
]
}
]
}
I want to retrieve the post data with all nested associations and map them to the Post struct. How would you do it with database/sql and pq (Postgres driver)?
I don't want to use any ORM at all, I only use database/SQL and SQLx.
Keep in mind that the performance is important because The posts and comments tables are big enough.

GraphQL - How to get field types from the retrieved schema?

Knowing the schema (fetched via getIntrospectionQuery), how could I get the type of a particular field?
For example, say I run this query:
query {
User {
name
lastUpdated
friends {
name
}
}
}
and get this result:
{
"data": {
"User": [
{
"name": "alice",
"lastUpdated": "2018-02-03T17:22:49+00:00",
"friends": []
},
{
"name": "bob",
"lastUpdated": "2017-09-01T17:08:49+00:00",
"friends": [
{
"name": "eve"
}
]
}
]
}
}
I'd like to know the types of the fields and construct something like this:
{
"name": "String",
"lastUpdated": "timestamptz",
"friends": "[Friend]"
}
How could I do that without extra requests to the server?
After retrieving the schema, you can build it into a JSON object (if your graphql framework does not do it already for you).
Using a JSON parser, you can retrieve the the types of each field.
I will not enter into the detail, as it would depend on the technology your are using.

denormalise reverse processStrategy

I have an API that gives out data like this with the attributes in a fields property.
{
records: [
{
id: "123",
fields: {
author: {
id: "1",
name: "Paul"
},
title: "My awesome blog post",
comments: [
{
id: "324",
commenter: {
id: "2",
name: "Nicole"
}
}
]
}
}
]
};
When normalizing, I now handle this with a simple processStrategy: (input, parent, key) => input.fields but I would like denormalise this again so that the denormalised entities to contain this fields structure because the API expects it this way.
So far denormalising my normalised data with const denormalizedData = denormalize([123], [article], normalizedData.entities) omits the field:
[
{
"author": {
"id": "1",
"name": "Paul"
},
"title": "My awesome blog post",
"comments": [
{
"id": "324",
"commenter": {
"id": "2",
"name": "Nicole"
}
}
]
}
]
I cannot find anything in the api docs on how to add extra processing on denormalisation, any idea?
Because processStrategy is intended for pre-processing of data during the normalization process, it is not going to be executed during the denormalization. For your use case, I would not use this feature and simply structure your schemas as follows:
const { schema, denormalize, normalize } = normalizr;
const user = new schema.Entity("users");
const comment = new schema.Entity("comments", { commenter: user });
const commentList = [comment];
const post = new schema.Entity("posts", {
fields: { author: user, comments: commentList }
});
const postList = [post];
const mockApiResponse = {
records: [
{
id: "123",
fields: {
author: {
id: "1",
name: "Paul"
},
title: "My awesome blog post",
comments: [
{
id: "324",
commenter: {
id: "2",
name: "Nicole"
}
}
]
}
}
]
};
const normalizedResponse = normalize(mockApiResponse.records, postList);
const denormalizedResponse = denormalize(
normalizedResponse.result,
postList,
normalizedResponse.entities
);
console.log("normalizedResponse", normalizedResponse);
console.log("denormalizedResponse", denormalizedResponse);
This will give you the result you are looking for. If for some reason, you need to stick to your current implementation, I would recommend implementing a transform on your request prior to sending it back to the server. As an example, axios solves this with their transformRequest feature.

graphql - combining results from multiple queries into one array

I am using graphQL to perform a search across multiple mongoDB collections and API's by combining queries. All queries return a result type of
{
_id: string;
name: string;
type: string;
}
Is there any way to flatten the data into a single array?
Combined query example:
query searchAll {
books(input: {text: "e"}) {
_id
name
type
}
magazines(input: {text: "e"}) {
_id
name
type
}
}
Response currently looks like:
{"data": {
"books": [
{
"_id": "5a8ac759c25b7235ffdc6888",
"name": "someBook",
"type": "book"
}
],
"magazines": [
{
"_id": "5a87005bc25b7235ffdc4bdf",
"name": "someMagazine-1",
"type": "magazine"
},
{
"_id": "5a870067c25b7235ffdc4be4",
"name": "someMagazine-2",
"type": "client"
}
]
}
}
Desired response:
{"data": {
"results": [
{
"_id": "5a8ac759c25b7235ffdc6888",
"name": "someBook",
"type": "book"
},
{
"_id": "5a87005bc25b7235ffdc4bdf",
"name": "someMagazine-1",
"type": "magazine"
},
{
"_id": "5a870067c25b7235ffdc4be4",
"name": "someMagazine-2",
"type": "client"
}
]
}
}
You want to look into using interfaces, here's an example of a (slightly richer) schema definition:
interface Searchable {
id: ID!
name: String!
}
type Book implements Searchable {
id: ID!
name: String!
author: Author!
publisher: Publisher!
isbn: String!
}
type Magazine implements Searchable {
id: ID!
name: String!
publisher: Publisher!
issn: String!
}
input SearchInput {
text: String!
}
type Query {
search(input: SearchInput!): [Searchable!]!
}
Here's how you'd query it:
query searchAll {
search(input: {text: "e"}) {
__typename
id
name
... on Book {
author
isbn
}
... on Magazine {
issn
}
}
}
The resolver for search would be responsible for calling all the different collections and aggregating the results into a single array. Going into more depth than this is implementation-specific, but there should be docs for using interfaces (and unions, which are similar) in whichever GraphQL implementation you're using.
The __typename field is what tells you the concrete type of the returned object, thereby letting front-end code do the appropriate display logic.

Resources