Issue while Querying GraphQL API as a client - graphql

I am trying to run a query from postman like below and see HTTP 400 bad request as the error. Please help.
query getContract($data : String!){
contract(contractNumber: $data) {
contractNumber
versions(getCurrentContractVersion: true) {
nodes {
customer {
birthDate
legalEntityCode
}
contractAssets {
nodes {
assetDetails {
plate
}
}
}
}
}
}
}
Postman Response - HTTP 400 Bad request
{
"errors": [
{
"message": "Either the parameter query or the parameter id has to be set.",
"extensions": {
"code": "HC0013"
}
}
]
}

Related

Is there a way to respect field selection in AppSync when caching?

I have an AppSync with caching enabled for a lambda data source.
When I'm sending following query:
{
todoItems {
field1
}
}
Then I'm getting response for requested field, and this response is also gets cached by AppSync
{
"todoItems":
[
{
"field1": "some data"
}
]
}
Then immediately (while cached item is still there) I'm sending same query but with extra field:
{
todoItems {
field1
field2
}
}
And getting response where this extraField comes with null value:
{
"todoItems":
[
{
"field1": "some data"
"field2": null
}
]
}
Then I'm waiting while the cache will expire and sending query once again, and only then getting correct result:
{
"todoItems":
[
{
"field1": "some data"
"field2": "other data"
}
]
}
So it looks like AppSync cached first response and responding with cached data, even for requests with different selection of subfields.

Consume graphQL API from Mule 4

I want to consumer graphQl API.
I know we need to use http requester to call graphQl.
I need some info on forming mutation request using dwl.
I was trying to hit this service https://www.predic8.de/fruit-shop-graphql
using below
%dw2.0
outputapplication/json
---
{
"query": "mutation(\$input:addCategoryInput!) { addCategory(input:\$input) { name products { name}} }",
"variables": {
"input": {
"id": 6,
"name": "Green Fruits",
"products": 8
}
}
}
its throwing bad request
But when using below
%dw 2.0
output application/json
---
{
"query": "mutation { addCategory(id: 6, name: \"Green Fruits\", products: 8) { name products { name } }}"
}
its working.
I want to use above format. Are both not valid requests.
Please share me your knowledge or guide me to right blog to refer.
Since GraphQL is not a supported format for DataWeave at this time, you have to write the query yourself as a string. You can however use DataWeave to create the body of a POST request for a query.
Example:
%dw 2.0
output application/json
---
{
"query": "mutation(\$schedule:PipelineScheduleCreateInput!) { pipelineScheduleCreate(input:\$schedule) { pipelineScheduleEdge { node { label nextBuildAt cronline } } } }",
"variables": {
"schedule": {
"pipelineID": "UGlwZWxpbmUtLS02MzliNWJjOC0wMGZmLT",
"cronline": "#midnight",
"label": "Nightly build"
}
}
}

How to fix "Syntax Error: Expected Name, found String \"query\"" in GraphQL

I'm trying to test the GraphQL server I built, by sending GraphQL queries to the server using Postman.
It works when I'm using raw radio button, but when I'm trying to use GraphQL radio button, it returns "message": "Syntax Error: Expected Name, found String \"query\"".
I have tried to change the syntax: mainly add or delete curly braces but nothing happened.
The query I sent in raw mode (working):
{
person(id:"123456789") {
personal_info {
address
}
}
}
The query I sent in GraphQL mode:
QUERY:
query getPerson ($id: String){
person(id: $id){
personal_info {
address
}
}
}
GRAPHQL VARIABLES:
{
"id": "123456789"
}
I expect to get the data I asked for, but I get the error message:
{
"errors": [
{
"message": "Syntax Error: Expected Name, found String \"query\"",
"locations": [
{
"line": 1,
"column": 2
}
]
}
]
}
I had the same problem. During researching I have found the next answer on stackoverflow, thanks #gbenga_ps.
Resolved by adding the correct header to Postman request:
Body of request should be something like next:
{
courses {
title
}
}
If incorrect content-type set, error like next happened:
{
"errors": [
{
"message": "Syntax Error: Expected Name, found String \"query\"",
"locations": [
{
"line": 1,
"column": 2
}
]
}
]
}

GraphQL Github API formatting

I am wondering how to deal with the following problem. I am using GraphQL to query the v4 Github API with the following query:
{
viewer {
repositories(first: 30) {
edges {
node {
name
}
}
}
}
}
This gets me a response that looks like so:
{
"data": {
"viewer": {
"repositories": {
"edges": [
{
"node": {
"name": "test-repo"
}
},
{
"node": {
"name": "another-repo"
}
}
]
}
}
}
}
I am pretty new to GraphQL, I understand that in my query I need to provide the edges and nodes but I would rather get a response back in this kind of way because I am not interested to know about "edges" and "nodes" in my frontend:
{
"data": {
"viewer": {
"repositories": [
{
"name": "test-repo"
},
{
"name": "another-repo"
}
]
}
}
}
}
I am guessing this kind of response is normal for GraphQL but it would be pretty cumbersome to rewrite to response all the time for easier usage in my frontend. Is there some way to emit the "edges" and "nodes" and get the formatting that I would like or is this simply all up to me to deal with?
I have looked at some libraries like Apollo but I have no idea is this is a right fit to deal with things like this. Hopefully someone a bit more experienced with GraphQL could tell me something more.
Sometimes, services provides two endpoints: Relay endpoint (with edges and nodes) and simple endpoint.
Looks like GitHub only have a Relay endpoint. In this case, the only thing you can do is to manually format the response on your frontend.
Actually, such complex response structure is needed because we often need to do a pagination. Take a look at the example:
{
getArticle(id: "some-id") {
id
userId
user {
id
name
}
tags(first: 10, after: "opaqueCursor") {
edges {
node {
id
name
itemsCount
}
}
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
}
}
}
pageInfo is located at the same level as edges.
So if you later will need to do a pagination, it would be better to keep the response format as is.
You can remove the edges query if you know you aren't searching along those relationships. Cursor-based pagination will work by checking the pageInfo value hasNextPage and using endCursor as the after query parameter:
viewer {
repositories(first: 30,after:"<CURSOR_STRING>") {
totalCount
pageInfo{
hasNextPage
endCursor
}
nodes{
name
}
}
}
returns
"viewer": {
"repositories": {
"totalCount": 38,
"pageInfo": {
"hasNextPage": true,
"endCursor": "Y3Vyc29yOnYyOpHOAl/5mw=="
},
"nodes": [
{
"name": "AllStarRoom"
},
{
"name": "shimsham"
},
{
"name": "Monitor-Docs"
}
]
}
}

How to use distinct in GraphQL query?

Here is query where I try to use distinct in graphQl query:
query{
contacts(take: 10, distinct: true) {
firstName
lastName
title
}
}
But I am getting error:
{
"errors": [
{
"message": "Unknown argument \"distinct\" on field \"contacts\" of type \"QuerySchema\".",
"locations": [
{
"line": 2,
"column": 21
}
]
}
]
}
GraphQL has no built-in sorting/filtering. It is up to the server to implement features like that, so if you're relying on a third party API and it doesn't support it then you will have to filter the response yourself.
You need to specify the column that GraphQL will use to check wether a value is distinct. In your case you can do something like:
query MyQuery {
contacts(distinct_on: firstName) {
firstName
lastName
title
}
}
Here is a example of distinct query.
query {
contacts {
distinct(field: title)
}
}
Result will be.
{
"data": {
"contacts": {
"distinct": [
"This is my test post",
"This is my test post1",
"This is my test post2"
]
}
}
}
This query binds all titles and deduplicates.

Resources