Graphql GQL cannot parse Ruby symbols or hashrockets - ruby

I have the following method that parses the mutation into a GraphqL-readable format using GQL
def query(attributes:)
<<~GQL
mutation {
createBook(
attributes: #{attributes}
) {
id
title
publicationDate
genre
}
}
GQL
end
I then send a post request to the /graphql endpoint while passing what the query returns as params. As follows
post '/graphql', params: { query: query(attributes: attributes) }
When I hit the endpoint, I get the following error
{"errors"=>[{"message"=>"Parse error on \":\" (COLON) }]}
I later realised that GQL cannot parse Ruby symbols , i.e :first_name. So I tried to convert the attributes hash keys into strings with attributes.stringify, but seems GQL doesn't also recognise Ruby hashrockets, so it threw the error
{"errors"=>[{"message"=>"Parse error on \"first_name\" (STRING) }]}
Is there a way to make GraphQL to parse Ruby symbols and/or hashrockets?

Related

Mock GraphQLUpload dataype in POSTMAN

I am using Postman to send GraphQL queries to my graphQL server. This is what a particular mutation schema looks like:
extend type Mutation {
createMutation(
my_id: ID!
my_data: input_data
): some_output
input input_data {
some_key: int
file : Upload!
}
I am able to perform other mutations and query through graphQL by defining appropriate GraphQL variables in here
I am not sure how to create a json value in GraphQL Variables for "file" of type Upload

GraphQL Relay introspection query fails: Unknown field '__type' on type 'Query'

I have a React application with a GraphQL client (Relay).
I need to make an introspection query to get all the types of a certain enum.
This is my query:
import { graphql } from 'react-relay'
export const corporationTypeEnumQuery = graphql`
query corporationTypeEnumQuery {
__type(name: "CorporationTypeEnum") {
enumValues {
name
}
}
}
`
When I try to compile the app, Relay throws the following error:
Unknown field '__type' on type 'Query'
When I try the exact same query on GraphIQL (the playground that the server offers me), there is no error, and the query is successful.
Please tell me what could be wrong with my GraphQL query. Any suggestions are well received.
The __type must be part of your schema. Graphql spec by default said this should be hidden and not part of schema.. You need to extend your server to expose it or manually put them to schema..
Server can handle that but compiler not since it require explicit definition in .graphql

Postman - Upload file and other argument with GraphQL

I am trying to use Postman to upload a file to GraphQL.
I know how I can upload a file, however, in my mutation I have to pass a String! together with the Upload!.
I am not sure where I can pass this String! in Postman.
My mutation:
mutation AddBookImageOne($bookId: string, $bookImageOne: Upload!){
addBookImageOne(bookId: $bookId, bookImageOne: $bookImageOne)
}
I tried to pass bookId in the variables key but I keep getting the error:
"message": "Variable "$bookId" of required type "String!" was not provided.",
I checked this: Graphql mutation to upload file with other fields
but they use CURL
"Operations" in postman field is:
{"query":"mutation AddBookImageOne($bookId: String!, $bookImageOne: Upload!){\n addBookImageOne(bookId: $bookId, bookImageOne: $bookImageOne)\n}"}
SOLVED thanks to #xadm
I had to pass the variables in the operations field like:
{"query":"mutation AddBookImageOne($bookId: String!, $bookImageOne: Upload!){\n addBookImageOne(bookId: $bookId, bookImageOne: $bookImageOne)\n}", "variables": { "bookImageOne": null, "bookId": "be96934c-d20c-4fad-b4bb-a1165468bad9" } }`

GraphQL error: Variable 'id' has coerced Null value for NonNull type 'ID!'

I am using the AWS AppSync service as my GraphQL server. I am passing a mutation GraphQL tag to create a user but somehow I am getting this error in the console:
GraphQL error: Variable 'id' has coerced Null value for NonNull type 'ID!'
The mutation GraphQL tag is like this:
import gql from 'graphql-tag';
export default gql`
mutation addUser ($id:ID!,$name:String!,$email:String!,$number:String!,$gender:String!,$password:String!,$createdAt:String!,$type:String!){
addUser(
id:$id,
name:$name,
email:$email,
number:$number,
gender:$gender,
password:$password,
createdAt:$createdAt,
type:$type
){
id
name
email
}
}`;
and I am passing this GraphQL tag inside my SignupForm.js to create a user like this:
export default graphql(AddUser,{
props:props=>({
AddUser:user=>props.mutate({
variable:user,
})
})
})(SignUpForm);
When I call this.props.AddUser(user)
where user is signup user details object
I got error mentioned above.
It seems the problem is a typo in the mutation options:
{ variable: user } should be { variables: user } (with an 's')

How to pass GraphQLEnumType in mutation as a string value

I have following GraphQLEnumType
const PackagingUnitType = new GraphQLEnumType({
name: 'PackagingUnit',
description: '',
values: {
Carton: { value: 'Carton' },
Stack: { value: 'Stack' },
},
});
On a mutation query if i pass PackagingUnit value as Carton (without quotes) it works. But If i pass as string 'Carton' it throws following error
In field "packagingUnit": Expected type "PackagingUnit", found "Carton"
Is there a way to pass the enum as a string from client side?
EDIT:
I have a form in my front end, where i collect the PackagingUnit type from user along with other fields. PackagingUnit type is represented as a string in front end (not the graphQL Enum type), Since i am not using Apollo Client or Relay, i had to construct the graphQL query string by myself.
Right now i am collecting the form data as JSON and then do JSON.stringify() and then remove the double Quotes on properties to get the final graphQL compatible query.
eg. my form has two fields packagingUnitType (An GraphQLEnumType) and noOfUnits (An GraphQLFloat)
my json structure is
{
packagingUnitType: "Carton",
noOfUnits: 10
}
convert this to string using JSON.stringify()
'{"packagingUnitType":"Carton","noOfUnits":10}'
And then remove the doubleQuotes on properties
{packagingUnitType:"Carton",noOfUnits:10}
Now this can be passed to the graphQL server like
newStackMutation(input: {packagingUnitType:"Carton", noOfUnits:10}) {
...
}
This works only if the enum value does not have any quotes. like below
newStackMutation(input: {packagingUnitType:Carton, noOfUnits:10}) {
...
}
Thanks
GraphQL queries can accept variables. This will be easier for you, as you will not have to do some tricky string-concatenation.
I suppose you use GraphQLHttp - or similar. To send your variables along the query, send a JSON body with a query key and a variables key:
// JSON body
{
"query": "query MyQuery { ... }",
"variables": {
"variable1": ...,
}
}
The query syntax is:
query MyMutation($input: NewStackMutationInput) {
newStackMutation(input: $input) {
...
}
}
And then, you can pass your variable as:
{
"input": {
"packagingUnitType": "Carton",
"noOfUnits": 10
}
}
GraphQL will understand packagingUnitType is an Enum type and will do the conversion for you.

Resources