HTTP CORS on Graphql server won't insert - graphql

I'm working on this project where I have 3 "tables". 2 of them are independent and one is M:M relationship.
However, my database was working fine until I realized it could not liaise the 2 independent table in the 3rd one, so I decided to change up the ids with "_" and make it work like that. However, it keeps returning me the 400 Error and I cant seem to figure out why.
{
"students": [
{
"id": 3021,
"nume": "Name",
"prenume": "Surname",
"an": 3,
"departament": "1"
}
],
"exams": [
{
"id": 1,
"subiect": "Subject",
"data_examen": "02/06/2022 10:00",
"profesor": {
"nume": "Name Surname"
}
}
],
"listexams": [
{
"id":1,
"exam_id":1,
"student_id":3021
}
]
}
Insertion code:
`function send()
{
dategraph.forEach((graph) => {
deInserat={ examenId:graph.examId,studentId:graph.studentId};
obiectInterogare={query:`mutation adaugare($examenId:Int!,$studentId:Int!)
{createListaexamene(examId:$examenId, studentId:$studentId) {id examId studentId}}`,
variables:JSON.stringify(deInserat)}
textInterogare=JSON.stringify(obiectInterogare)
configurari={url:"http://localhost:3000/",
type:"POST",
data:textInterogare,
contentType:"application/json",
success:procesareRaspunsql,
error: (error) =>
{
console.log('Error', error);
}
}
$.ajax(configurari);
});
}`
It's taking the variables from another place but that is working just fine (because it worked prior to changing the fields) but it won't now. Any ideas what it could be?
The error that gets thrown out is

This might help somebody. Beware of the implicit GraphQL schema. The reason why it kept throwing out 400 Error is because I had the M:M relationship keys defined as "int!" instead of "ID!"
As I stated in the question, I knew it's something in the query/database. Thanks to the expert who downvoted it

Related

GraphQL java: return a partial response and inform a user about it

I have a SpringBoot application that uses GraphQL to return data to a request.
What I have
One of my queries returns a list of responses based on a list of ids supplied. So my .graphqls file is a follows:
type Query {
texts(ids: [String]): [Response]
}
type Response {
id: String
text: String
}
and the following are request & response:
Request
texts(ids:["id 1","id 2"]){
id
text
}
Response
{
"data": [
{
"id": "id 1",
"text": "Text 1"
},
{
"id": "id 2",
"text": "Text 2"
}
]
}
At the moment, if id(s) is/are not in aws, then exception is thrown and the response is an error block saying that certain id(s) was/were not found. Unfortunately, the response for other ids that were found is not displayed - instead the data block returns a null. If I check wether data is present in the code via ssay if/else statment, then partial response can be returned but I will not know that it is a partial response.
What I want to happen
My application fetches the data from aws and occasionally some of it may not be present, meaning that for one of the supplied ids, there will be no data. Not a problem, I can do checks and simply never process this id. But I would like to inform a user if the response I returned is partial (and some info is missing due to absence of data).
See example of the output I want at the end.
What I tried
While learning about GraphQL, I have encountered an instrumentation - a great tool for logging. Since it goes through all stages of execution, I thought that I can try and change the response midway - the Instrumentation class has a lot of methods, so I tried to find the one that works. I tried to make beginExecution(InstrumentationExecutionParameters parameters) and instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) to work but neither worked for me.
I think the below may work, but as comments suggests there are parts that I failed to figure out
#Override
public GraphQLSchema instrumentSchema(GraphQLSchema schema, InstrumentationExecutionParameters parameters) {
String id = ""; // how to extract an id from the passed query (without needing to disect parameters.getQuery();
log.info("The id is " + id);
if(s3Service.doesExist(id)) {
return super.instrumentSchema(schema, parameters);
}
schema.transform(); // How would I add extra field
return schema;
}
I also found this post that seem to offer more simpler solution. Unfortunately, the link provided by host does not exist and link provided by the person who answered a question is very brief. I wonder if anyone know how to use this annotation and maybe have an example I can look at?
Finally, I know there is DataFetcherResult which can construct partial response. The problem here is that some of my other apps use reactive programming, so while it will be great for Spring mvc apps, it will not be so great for spring flux apps (because as I understand it, DataFetcherResult waits for all the outputs and as such is a blocker). Happy to be corrected on this one.
Desired output
I would like my response to look like so, when some data that was requested is not found.
Either
{
"data": [
{
"id": "id 1",
"text": "Text 1"
},
{
"id": "id 2",
"text": "Text 2"
},
{
"id": "Non existant id",
"msg": "This id was not found"
}
]
}
or
{
"error": [
"errors": [
{
"message": "There was a problem getting data for this id(s): Bad id 1"
}
]
],
"data": [
{
"id": "id 1",
"text": "Text 1"
},
{
"id": "id 2",
"text": "Text 2"
}
]
}
So I figured out one way of achieving this, using instrumentation and extension block (as oppose to error block which is what I wanted to use initially). The big thanks goes to fellow Joe, who answered this question. Combine it with DataFetchingEnviroment (great video here) variable and I got the working solution.
My instrumentation class is as follows
public class CustomInstrum extends SimpleInstrumentation {
#Override
public CompletableFuture<ExecutionResult> instrumentExecutionResult(
ExecutionResult executionResult,
InstrumentationExecutionParameters parameters) {
if(parameters.getGraphQLContext().hasKey("Faulty ids")) {
Map<Object, Object> currentExt = executionResult.getExtensions();
Map<Object, Object> newExtensionMap = new LinkedHashMap<>();
newExtensionMap.putAll(currentExt == null ? Collections.emptyMap() : currentExt);
newExtensionMap.put("Warning:", "No data was found for the following ids: " + parameters.getGraphQLContext().get("Faulty ids").toString());
return CompletableFuture.completedFuture(
new ExecutionResultImpl(
executionResult.getData(),
executionResult.getErrors(),
newExtensionMap));
}
return CompletableFuture.completedFuture(
new ExecutionResultImpl(
executionResult.getData(),
executionResult.getErrors(),
executionResult.getExtensions()));
}
}
and my DataFetchingEnviroment is in my resolver:
public CompletableFuture<List<Article>> articles(List<String> ids, DataFetchingEnvironment env) {
List<CompletableFuture<Article>> res = new ArrayList<>();
// Below's list would contain the bad ids
List<String> faultyIds = new ArrayList<>();
for(String id : ids) {
log.info("Getting article for id {}",id);
if(s3Service.doesExist(id)) {
res.add(filterService.gettingArticle(id));
} else {
faultyIds.add(id);// if data doesn't exist then id will not be processed
}
}
// if we have any bad ids, then we add the list to the context for instrumentations to pick it up, right before returning a response
if(!faultyIds.isEmpty()) {
env.getGraphQlContext().put("Faulty ids", faultyIds);
}
return CompletableFuture.allOf(res.toArray(new CompletableFuture[0])).thenApply(item -> res.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()));
}
You can obviously separate error related ids to different contexts but for my simple case, one will suffice. I however still interested in how can the same results be achieved via error block, so i will leave this question hanging for a bit before accepting this as a final answer.
My response looks as follows now:
{
"extensions": {
"Warning:": "No data was found for the following ids: [234]"
},
"data": { ... }
My only concern with this approach is security and "doing the right thing" - is this correct thing to do, adding something to the context and then using instrumentation to influence the response? Are there any potential security issues? If someone know anything about it and could share, it will help me greatly!
Update
After further testing it appears if exception is thrown it will still not work, so it only works if you know beforehand that something goes wrong and add appropriate exception handling. Cannot be used with try/catch block. So I am a half step back again.

Insert more than one record using GraphQL Mutation

I would like to insert more than one record using GraphQL Mutation but it is giving error. here is the code which I have used to perform this.
input BusinessImageInput {
business_id: Int
image_url: String
}
mutation MyMutation($images: [BusinessImageInput!]) {
insert_business_images(objects: [$images]) {
affected_rows
}
}
And here is variable which i want to pass as paramter.
{"images": [
{
"business_id": 15,
"image_url": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTVzlb1cEw8E0LeLJzk9c0OQV-N387Nt2Kn5w&usqp=CAU"
},
{
"business_id": 15,
"image_url": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTVzlb1cEw8E0LeLJzk9c0OQV-N387Nt2Kn5w&usqp=CAU"
}
]
}
Here is the error
{
"errors": [
{
"extensions": {
"path": "$.query",
"code": "bad-request"
},
"message": "not a valid GraphQL query"
}
]
}
Please help me out.
There is one glaring issue in your code. This line
insert_business_images(objects: [$images]) {
should be
insert_business_images(objects: $images) {
Notice the removed square brackets.
If that does not help, then we'll need more information, such as:
what error do you get?
which implementation of GraphQL are you using both client-side and server-side?
what does the GraphQL code (and possibly resolvers) look like on the server? You have only given us the client-side of the equation.
It's as simple as
mutation MyMutation($images: [BusinessImageInput!]) {
insert_business_images(images: $images) {
or
mutation MyMutation($objects: [BusinessImageInput!]) {
insert_business_images(objects: $objects) {
depends on server insert_business_images mutation definition, the name of argument (images or objects ?) - use explorer ... and [as you can see above] usually input arg and variable are same-named, they only differs with $ prefix.
https://graphql.org/learn/queries/#variables
Also you must follow server input types.

GraphQL Move a child property into parent

I'm looking into GraphQL and got to a question I'm not even sure how to look for in the docs. Probably this isn't even supposed to work, but I'll ask anyway.
I have this query:
query {
organization(login: "facebook") {
repositories(first: 5) {
items: edges {
repo: node {
name
owner {
login
}
}
}
}
}
}
Now, in the response I would like a way to place the login next to name, instead of nested in owner login.
So in the response instead of:
{
...
"name": "react-native",
"owner": {
"login": "facebook"
}
}
I would like to have:
{
...
"name": "react-native",
"ownerName": "facebook"
}
Thank you.
From the specification this is not possible. The response has to be shaped in the way the object types are shaped. There is a project called GraphQL Lodash that gives you a new directive to modify the results. It can be helpful here but it is rather experimental and IMO very undogmatic.

RethinkDB - delete a nested object

I am trying to delete a nested object, but instead of disappearing it is being replaced by an empty object.
Here's the structure of my documents:
[
{
"id": "0" ,
"name": "Employee 1" ,
"schedules": [
{"Weekdays": "yes"},
{"Weekends": "no"}
]
} ,
{
"id": "1" ,
"name": "Employee 2" ,
"schedules": [
{"Weekdays": "no"},
{"Weekends": "yes"}
]
}
]
Let's say I want to delete "Weekends". Here's my code:
r.db('shank').table('teachers').replace(r.row.without({'schedules': 'Weekends'})).run(connection, function(err, result) {
if (err) throw err;
;
//confirmation stuff
});
});
Now if I look at my table, the documents have this:
"schedules": [
{"Weekdays": "yes"},
{}
]
I also tried changing it to follow the syntax described here, by making it:
r.row.without({'schedules': {'Weekends'}})
but I got an error of an unexpected token '}'. Any idea what's up?
This should work:
r.db('test').table('test').get('0').update(function(doc) {
return doc.merge({
schedules: doc("schedules").filter(function(schedule) {
return schedule.hasFields('Weekends').not()
})
})
})
The field schedules is an array, is that expected? Is there a reason why it's not just an object with two fields Weekends and Weekdays? That would make things way easier.
Your last one is close to what worked for me, just need to add a true to the nested JSON object:
r.row.without({'schedules': {'Weekends': true}})

Unable to Properly Consume Data from $resource in Angular

I'm building a prototype app using Parse.com as a temporary backend.
This is how Parse structures its returned data when a request is made.
{
"results": [
{
"playerName": "Jang Min Chul",
"updatedAt": "2011-08-19T02:24:17.787Z",
"cheatMode": false,
"createdAt": "2011-08-19T02:24:17.787Z",
"objectId": "A22v5zRAgd",
"score": 80075
},
{
"playerName": "Sean Plott",
"updatedAt": "2011-08-21T18:02:52.248Z",
"cheatMode": false,
"createdAt": "2011-08-20T02:06:57.931Z",
"objectId": "Ed1nuqPvcm",
"score": 73453
}
]
}
I have a factory that looks like this
app.factory('eventFactory', ['$resource', function($resource){
return $resource('https://api.parse.com/1/classes/Events', {},
{ 'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:false},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
});
}
]);
My controller looks like this:
app.controller('currentTradeshowsController', function($scope, eventFactory){
var testSave = eventFactory.get();
console.log(testSave);
console.log(testSave.results);
});
the first console.log for 'testSave' looks like this :
Where as testSave.results returns undefined.
I would think that I would be able to access all the objects this way no?
What am I missing here?
Any help is very much appreciated.
Even though $resource does a very nice job of abstracting, the fact remains that since it works via HTTP calls it runs asynchronously and the code has to be written to handle that semantically. The documentation expresses this.
var testSave = eventFactory.get().success(function () {
console.log(testSave);
});
Unfortunately the Angular docs do not make it clear, but I think the above will work. Possible alternatives are:
.get(function () {
.get({}, function () {
.get().then(function () {
.get().$promise.then(function () {
Find one that works and works best for you. The idea is that the success callback and anything that relies on a successful request need to be in this function.

Resources