How to return a string from a GraphQL mutation wrapped on restApi? - rest

My restAPI POST call return a uuid string as result, like so:
246f97e8-cdc8-4b01-9881-19278be7e33d
This API is wrapped with GraphQL. The mutation I wrote for this post method works fine, I can see it gets triggered and return a 200 status.
The only problem is that I cannot return that same string as the mutation result. This is what I have:
mutation myMutation($input: any) {
myMutation(input: $input)
#rest(type: "type", path: "apiPath", method: "POST", endpoint: "myEndpoint")
{
id
}
}
That id return an integer and any other value I add besides id return "null".
Any idea on how I can get that uuid through the mutation?

Related

How to rename axios FormData array when submitting

Is there something I can do to prevent axios from renaming my FormData field name when it is an array.
For example:
I have an array of images I am posting using the field name of images. When I post this, I notice in the payload of my browser the field becomes multiple fields, i.e. it becomes images.0 and images.1.
In other words it looks like axios is renaming the field and making multiple fields with names like images.N.
Is there any way to avoid this renaming? On the server side I am using node with Nestjs and multer.
My Nestjs controller function expects a field name images but it throws an "unexpected field" error because axios renames the fields to images.0 and then images.1.
NestJS controller function that fails (note the field name):
#Post()
#UseGuards(AuthGuard('jwt'))
#UseInterceptors(FilesInterceptor('images', 30, { dest: './uploads' }))
create(
#Body() body: CreateAssetDto,
#User() user: RequestUser,
#UploadedFiles() files: Array<Express.Multer.File>,
) {
console.log(files, body, user.userId);
//return this.assetsService.create(body, user.userId);
}
NestJs controller function that works (note the use of the field name images.0):
#Post()
#UseGuards(AuthGuard('jwt'))
#UseInterceptors(FilesInterceptor('images.0', 30, { dest: './uploads' }))
create(
#Body() body: CreateAssetDto,
#User() user: RequestUser,
#UploadedFiles() files: Array<Express.Multer.File>,
) {
console.log(files, body, user.userId);
//return this.assetsService.create(body, user.userId);
}

A platform returns a POST method to an url and I want to retrieve the data to firestore

I want to retrieve the data from a third party platform that generates a POST call to an url that I can define. But I can only configure the url. I cannot configure the headers or the body of the POST action.
I want to store the JSON into a firestore collection.
The platform calls a POST action to a URL y can define, and that POST action has a JSON with the parameters I want to store.
In another post they directed me to the firestore API
https://stackoverflow.com/a/50061155/18276916
But there it is stated that the body of the POST action must have the following structure:
{
"writes": [
{
object (Write)
}
],
"labels": {
string: string,
...
}
}
The body of the POST action is defined in the third party aplication, and I cant modify it.
Is it posible to do this with Firestore, or is there another method I can use?
What would be the path of the url?:
https://firestore.googleapis.com/v1/projects/[my-project-id]/databases/(default)/documents/[my-collection-name]
Also, can I set the firestore rules to allow everyone to perform create actions so the call doesn't need to be authenticated
I found a solution that works
https://www.jeansnyman.com/posts/google-firestore-rest-api-examples/
With the following structure:
Url: https://firestore.googleapis.com/v1/projects//databases/(default)/documents/
Body:
{
fields: {
title: { stringValue: title },
category: { stringValue: category }
}
}
But here the body has its defined structure. I would need the url to accept:
{
title: title,
category: category
}

must have a selection of subfields. Did you mean \"createEvent { ... }\"?", [graphql] [duplicate]

Hi I am trying to learn GraphQL language. I have below snippet of code.
// Welcome to Launchpad!
// Log in to edit and save pads, run queries in GraphiQL on the right.
// Click "Download" above to get a zip with a standalone Node.js server.
// See docs and examples at https://github.com/apollographql/awesome-launchpad
// graphql-tools combines a schema string with resolvers.
import { makeExecutableSchema } from 'graphql-tools';
// Construct a schema, using GraphQL schema language
const typeDefs = `
type User {
name: String!
age: Int!
}
type Query {
me: User
}
`;
const user = { name: 'Williams', age: 26};
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
me: (root, args, context) => {
return user;
},
},
};
// Required: Export the GraphQL.js schema object as "schema"
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
// Optional: Export a function to get context from the request. It accepts two
// parameters - headers (lowercased http headers) and secrets (secrets defined
// in secrets section). It must return an object (or a promise resolving to it).
export function context(headers, secrets) {
return {
headers,
secrets,
};
};
// Optional: Export a root value to be passed during execution
// export const rootValue = {};
// Optional: Export a root function, that returns root to be passed
// during execution, accepting headers and secrets. It can return a
// promise. rootFunction takes precedence over rootValue.
// export function rootFunction(headers, secrets) {
// return {
// headers,
// secrets,
// };
// };
Request:
{
me
}
Response:
{
"errors": [
{
"message": "Field \"me\" of type \"User\" must have a selection of subfields. Did you mean \"me { ... }\"?",
"locations": [
{
"line": 4,
"column": 3
}
]
}
]
}
Does anyone know what I am doing wrong ? How to fix it ?
From the docs:
A GraphQL object type has a name and fields, but at some point those
fields have to resolve to some concrete data. That's where the scalar
types come in: they represent the leaves of the query.
GraphQL requires that you construct your queries in a way that only returns concrete data. Each field has to ultimately resolve to one or more scalars (or enums). That means you cannot just request a field that resolves to a type without also indicating which fields of that type you want to get back.
That's what the error message you received is telling you -- you requested a User type, but you didn't tell GraphQL at least one field to get back from that type.
To fix it, just change your request to include name like this:
{
me {
name
}
}
... or age. Or both. You cannot, however, request a specific type and expect GraphQL to provide all the fields for it -- you will always have to provide a selection (one or more) of fields for that type.

Usage of GET with request body with GraphQL

I am using REST API GET with payload ( passing input params ) and it returns with JSON response.
I need to convert it into GraphQL, I have the schema and types defined,
but how to merge GET request payload along with GraphQL query JSON and process it to get the desired response back?
Example GET: http://localhost/myproject/getAllCustomers
payload :
{
"name":"Harish"
"passion":"reading"
}
GraphQL query :
{
customer {
name
age
city
}
}
You pass variable to GraphQL query arguments
Query:
query getCustomer($name: String, $passion: String) {
customer(name: $name, passion: $passion) {
name
age
city
}
}
Variable:
{
"name": "Harish",
"passion": "reading"
}

how to create label for facebook broadcast message using facebook's broadcast api?

I want to create a custom label for broadcast message using facebook's broadcast api
I'm able to create label for by doing as instructed in documentation however i get an array of ids in response instead a single id for my custom label , this is how i'm requesting api
function createlabel(token, getlabel) {
var labels;
var options = {
name: "firstbroadcast",
uri: "https://graph.facebook.com/v2.11/me/custom_labels?",
qs: {
access_token: token
},
json: true,
}
requestpromise(options).then(function (label) {
console.log('returned label', label);
labels = label;
getlabel(label);
})
}
the response of above request is something like this
returned label { data: [ { id: '1721744167882031' }, { id:
'1562439310518208' } ],
paging:
{ cursors:
{ before: 'QVFIUldfeDVCVnBIOEo0Q3ZADZA2tIazNtUWxhNlpTUlhOVkQyU0NFZA1IzVGlnbGhJX2lCdWxuQUZAhYUlSaWlnZAFZAZAeU96Q3VrWGtCUHlaTnA1a0p5RmplNEh3',
after:'QVFIUm1WRGVFcXRpSGh1RGtRQzd0eTZAGczZAlNUY5SmdFYUtsMEdSdmpYZAVJhWHVCRGdCQm9mU0lHbHg3X0NqbUc5QkhTNmswMm14WHRNZA3hWeDR0QkdkMTJB' } } }
after getting ids of label i take the id of first index from the returned data and use it to associate PSID to the label like so
function associatelabeltopsid(user, label, token) {
console.log('userid: ' + user + ' label :' + label);
var options = {
user: user,
uri: "https://graph.facebook.com/v2.11/" + label + "/label?
access_token="+token,
}
requestpromise(options).then(function (response) {
console.log('association done', response);
}).catch(function (error, response, body) {
if (error) {
console.log("sending error", error.message);
} else if (response.body.error) {
console.log("response body error senttext");
console.log(response.body.error);
}
})
}
the three arguments of above function "associatelabeltopsid" are user=pagescope id of a recipient,label=[id on first index of the array returned in response from custom_labels endpoint], token={page access token}
the response of above request is something like this which leaves me incapable of associating my label with psid
sending error 400 - "{\"error\":{\"message\":\"(#100) Tried accessing nonexisting field (label) on node type (PageUserMessageThreadLabel)\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"Esk6606yfRg\"}}"
what am i doing wrong here, why the request on endpoint custom_labels returns array of ids for label instead of single id, which one do i use, and how do i associate PSID to the label.
To expand on the comment from #CBroe - you need to set method: 'POST' in your request options. The request-promise module will default to GET if method is not set.