ApolloError: variable x is declared as '[String!]!', but used where '_text' is expected - graphql

ApolloError: variable 'wallet_addresses' is declared as '[String!]!', but used where '_text' is expected
I get that error above. And my query is below.
mutation updateUser(
$id: Int
$email: String
$wallet_addresses: [String!]!
) {
update_dev_user(
where: {email: {_eq: $email}}
_set: {
wallet_addresses: $wallet_addresses
}
){
returning {
id
email
}
}
}```
I believe the mistake is at the `$wallet_addresses: [String!]!` this part. I have no idea how I an define an array type in the parameter.
[![enter image description here](https://i.stack.imgur.com/aZcz9.png)](https://i.stack.imgur.com/aZcz9.png)
I tried
[String!]!
[String!]
[String]
[]
types but got errors in anyway and could find in the docs ( hasura ). I just want to send string array to a table column which has Text[] type.

I solved issue with giving type _text to wallet_addresses parameter and converted string array to array literal.
doc: https://hasura.io/docs/latest/mutations/postgres/insert/#insert-an-object-with-an-array-field
const toArrayLiteral = (arr: string[]) =>
JSON.stringify(arr)?.replace('[', '{')?.replace(']', '}')?.replaceAll('"', '');

Related

GraphQLError: Syntax Error: Unexpected "("

I am new to GraphQL and am trying to query the TfL API for a project with React. I am trying to pass $arrival as a variable into the query, dynamically calling arrivals by station ID. It was working yesterday but this morning it is throwing a syntax error, any ideas where it may be coming from?
Type Defs
const typeDefs = `
type Station {
id: String!
stationName: String
lineId: String
lineName: String
platformName: String
direction: String
timestamp: String
timeToStation: Int
currentLocation: String
towards: String
expectedArrival: String
modeName: String
}
type Query($arrival: String!) {
getArrivals(id: $arrival): [Station]
station: [Station]
}
`;
Query
const GET_ALL_ARRIVALS = gql`
query Arrivals($id: String!) {
getArrivals(id: $id) {
id
stationName
platformName
lineName
towards
timeToStation
}
}
`;
Try changing your schema to
const typeDefs = `
type Station {
id: String!
stationName: String
lineId: String
lineName: String
platformName: String
direction: String
timestamp: String
timeToStation: Int
currentLocation: String
towards: String
expectedArrival: String
modeName: String
}
type Query {
getArrivals(id: String!): [Station]
station: [Station]
}
`;
That doesn't explain the error, but it seems wrong to me in comparison to the documentation
When you declare the top-level query type, you're trying to embed variable definitions as part of the type definition. They don't belong here; they are purely part of the query. $variable references never appear anywhere in the schema.
type Query { # does not take any parameters
getArrivals(id: ID!): [Station]
# ^^^ specify the _type_ of the field argument here
}
query Arrivals($someId: ID!) { # variable definition is part of the query
getArrivals(id: $someId) { # bind variable to field argument
id, ...
}
}

Array of Objects Apollo Server and post from react

So i'm trying to figure out how to pass an array of objects from a POST request to apollo server on AWS lambda.
I've checked this out but it's not the same problem
Array of objects convert into object of objects when I use Apollo
The post request looks like this...
api.post('query', { query : `mutation {saveNewItem(description: "${description}", specials: ${JSON.stringify(specials)}){name}}`})
// comment to get rid of silly scroll bar overlapping code
Schema looks like this...
const { gql } = require('apollo-server-lambda')
const typeDefs = gql`
type ShoppingItem {
description: String
specials: [Specials]
}
input Specials {
description: String
price: String
qty: String
saved: String
}
type Mutation {
saveNewItem(description: String!, specials: [Specials]) : ShoppingItem
}
`
example Specials looks like this...
[{ // Object One
description: "First One"
price: "1.00"
qty: "1"
saved: "false"
},{ // Object two
description: "Second One"
price: "1.00"
qty: "1"
saved: "false"
}]
The error I get currently is...
'Error: The type of ShoppingItem.specials must be Output Type but got: [Specials].',
'at assertValidSchema (/Users/me/Desktop/Projects/app/build/node_modules/graphql/type/validate.js:71:11)',
If I change it to a normal "type" it complains about it not being Input type.
I've also been through the apollo server docs and can't quite see what I'm doing wrong?
Please that as mentioned by Daniel in comments whilst technically the "duplicate" answer given is correct the information offered here is far more high quality and useful to people facing the problem(in my opinion)
You can only use input types for input (GraphQLInputObjectType) and object types for output (GraphQLObjectType). You are using Specials as both: As output type for the field specials in ShoppingItem and as input type in mutation argument specials. To do this you need two types. The reason for this is that output types (can) have resolvers (this is actually abstracted away from apollo server in your case). You will have to create two different types:
type ShoppingItem {
description: String
specials: [Specials]
}
type Specials {
description: String
price: String
qty: String
saved: String
}
input SpecialsDraft {
description: String
price: String
qty: String
saved: String
}
type Mutation {
saveNewItem(description: String!, specials: [SpecialsDraft]) : ShoppingItem
}

Graphql scalar type for a value that can be string or object?

i have an api for sign up which returns error of string or null,
error: 'Email already use' or error: null
how do i construct it in schema? what i have now is this:
const typeDefs = gql`
type Mutation {
signUp(email: String, password: String): String
}
`;
since typeof null is object, how can i make it like this in graphql?
signUp(email: String, password: String): String || Object
Help?
GraphQL has a standard syntax for returning error values and your schema does not directly need to account for this.
In your schema I would “unconditionally” return whatever type you’d normally expect to return:
type UserAccount { ... }
type Query {
me: UserAccount # or null if not signed in
}
type Mutation {
signUp(email: String!, password: String!): UserAccount!
}
If it’s unsuccessful, you will get back a null field value (even though the schema in theory claims it shouldn’t) and an error.
{
"errors": [
{
"message": "It didn’t work",
"locations": [ { "line": 2, "column": 3 } ],
"path": [ "signUp" ]
}
]
}
In GraphQL you can define what field can be null and what can't.
Take a look at the docs:
https://graphql.org/learn/schema/#object-types-and-fields
String is one of the built-in scalar types - these are types that
resolve to a single scalar object, and can't have sub-selections in
the query. We'll go over scalar types more later.
String! means that the field is non-nullable, meaning that the GraphQL service promises
to always give you a value when you query this field. In the type
language, we'll represent those with an exclamation mark.
So in case of your schema String is absolutely fine. It can be null
type Mutation {
signUp(email: String, password: String): String
}

How to change the graphql typedefs name that is coming from json results of external api?

I have encountered an issue with the naming in my typedefs. The error prompting the following.
Syntax Error: Expected Name, found Int "24"
I am using Coinmarketcap Api and accessing it with my apollo graphql server. The api has naming such as 24h_volume_usd, percent_change_1h etc, but as long as integer within the name, it will have this name issue.
I am not really sure how can I fix this issue. Can anyone please help me out? Thank you very much.
Schema.js:
const typeDefs = `
type cryptos {
id: String
name: String
symbol: String
rank: String
price_usd: String
price_btc: String
24h_volume_usd: String
market_cap_usd: String
percent_change_1h: String
available_supply: String
total_supply: String
last_updated: String
}
type Query {
cryptos: [cryptos]
}
`
resolvers.js:
const resolvers = {
Query: {
cryptos: () => {
return axios.get('https://api.coinmarketcap.com/v1/ticker/').
then(result => result.data );
}
}
Process api result using map functions
`var newHashmap = {};
Object.keys(hashmap).forEach(function(key){
var value = hashmap[key];
key = key + "xxx";
console.log("changing:");
console.log(key); newHashmap[key] = value
});`

Resolving query type with promise in bucklescript

I've got this query im trying to test with with the reason graphql_ppx library. code gist
This is a screenshot of the editor type annotations:
Using the #mhallin/graphql_ppx library, i've got the following query set up:
module FilmQuery = [%graphql
{|
{
allFilms {
films {
id
title
releaseDate
}
}
}
|}
];
exception Graphql_error(string);
/* Construct a "packaged" query; FilmQuery takes no arguments: */
let filmQuery = FilmQuery.make();
/* Send this query string to the server */
let query = filmQuery##query // type string
I get an the following error when i send the query to the server it returns the following error.
{ errors: [ { message: 'Must provide query string.' } ] }
But if you Js.log(query) you see its being constructed which works on https://swapi.apis.guru
query films($first: Int) {
allFilms(first: $first) {
films {
id
title
releaseDate
}
}
}
If you Js.log(filmQuery) you get:
{ query: 'query {\nallFilms {\nfilms {\nid \ntitle \nreleaseDate \n}\n}\n}',
variables: null,
parse: [Function: parse] }
If your run the same query in Altair and you check the query that was sent in the devtools network tab you see:
{"query":" query films($first: Int) {\n allFilms(first: $first) {\n films {\n id\n title\n releaseDate\n }\n }\n }\n","variables":{}}
The editor is provided this type error:
"- error
[bucklescript]
This has type:
string
But somewhere wanted:
Js.t({.. query : string, variables : Js.Json.t })
string"
How do I get this promise/unit type resolved? Thank you.
So the new question is: Why isn't the sendQuery() function recognizing the filmQuery##parse key?
Your sendQuery method is expecting a type in the shape of what is returned from FilmQuery.make(), but you are passing it just the query property, which is a string.
You can fix this by passing filmQuery as into sendQuery instead of just the query property from filmQuery that is referenced by the query variable.

Resources