I want to upload picture while creating a task. Error:: graphql.error.graphql_error.GraphQLError: 'Upload' scalar serialization is not supported.
graphene can't be used because ariadne and graphene need different version of graphql-core.
this is a flask-graphql webapp and i don't want to use any other api.
ariadne==0.17.0
flask-graphql==2.0.1
graphene_file_upload==1.3.0
graphql_core==3.2.0
i am new to graphql can somebody help me to upload a picture.
**routes.py**
type_defs = load_schema_from_path('./api/schema.graphql')
schema = make_executable_schema(type_defs, query, upload_scalar,mutation,snake_case_fallback_resolvers)
**schema.graphql**
scalar Upload
schema {
query: Query
mutation: Mutation
}
type Todo {
id: ID!
title:String
file:Upload
description: String!
}
type TodoResult {
success: Boolean!
errors: [String]
todo: Todo
}
type TodosResult {
success: Boolean!
errors: [String]
todos: [Todo]
}
type deleteTodoResult {
success: Boolean!
errors: [String]
}
type Query {
todo(todoId: ID!): TodoResult!
todos: TodosResult!
}
type Mutation {
createTodo(title: String,file: Upload! ,description: String!): TodoResult!
deleteTodo(todoId: ID!): deleteTodoResult!
markDone(todoId: ID!): TodoResult!
}
**mutations.py**
@convert_kwargs_to_snake_case
def resolve_create_todo(obj, info,title,file, description):
try :
print(description,"dtae")
print("fue")
file = {"file":(file,open("./static","rb"),'application-type')}
print(type(file),"typeppr")
todo = Todo(title=title,file=file,description=description)
print(todo,"tofooo")
db.session.add(todo)
db.session.commit()
payload = {
'success': True,
'todo': todo.to_dict()
}
except ValueError:
payload = {
'success': False,
'errors': [f'Incorrect date format provided. Date should be in the format of dd-mm-yyyy']
}
print("was hhh")
return payload
**app.js**
function createTodo(title,file,description, callback) {
fetch(`/graphql`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `mutation(${file}: Upload) {
createTodo(
,title: "${title}",file: "${file}",description: "${description}") {
success
errors
todo {
id
title
file
description
}
}
}`,
}),
})
.then(res => res.json())
.then(res => callback(res.data))
.catch(console.error)
}
**models.py**
class Todo(db.Model):
__tablename__ = 'Todo'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
file = db.Column(db.String)
description = db.Column(db.String)
print(type(file),"fillle")
#completed = db.Column(db.Boolean, default=False)
def to_dict(self):
return {
'id': self.id,
'title': self.title,
'file': self.file,
'description': self.description,
#'completed': self.completed,
}