Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract data from a list of dicts using graphene's mutation method?

I’m trying to created a list of objects using graphql mutation, but have been unsuccessful. I've identified the error, please see code snippets and comment on where the error is propagating.

Note: I'm using Graphene on Flask with Python 2.7

Here’s an example payload:

mutation UserMutation {
    createUser(
        phones: [
            {
                “number”: “609-777-7777”,
                “label”: “home"   
            },
            {
                “number”: “609-777-7778”,
                “label”: “mobile"   
            }
        ]
    )
}

On the schema, I have the following:

class CreateUser(graphene.Mutation):
    ok = graphene.Boolean()
    ...
    phones = graphene.List(graphene.String())  # this is a list of string but what I need is a list of dicts!
like image 599
Amir Avatar asked Sep 19 '16 17:09

Amir


People also ask

What is a mutation query in GraphQL?

In this chapter, we will learn mutation queries in GraphQL. Mutation queries modify data in the data store and returns a value. It can be used to insert, update, or delete data. Mutations are defined as a part of the schema. The syntax of a mutation query is given below −. mutation{ someEditOperation(dataField:"valueOfField"):returnType }

How to add new student records using a mutation query?

Mutations are defined as a part of the schema. mutation { someEditOperation (dataField:"valueOfField"):returnType } Let us understand how to add new student record into the datastore using a mutation query. Create a project folder by the name mutation-app. Change your directory to mutation-app from the terminal.

How hard is it to pull data from the Microsoft Graph?

Most APIs, including the Microsoft Graph, require authorization before they’ll give you an access token that will allow you to call the API. This is often the most difficult part for someone new to pulling data from APIs (it was for me).

How to extract data from cells with conditions in Excel?

Now, press Ctrl+ Shift + Enter simultaneously to apply this formula. We will get our first extracted data in cells that match the condition Then drag around the fill handle towards the row and column. Thus, the resulting data will be shown which matches the condition.


1 Answers

For having a dict as a Input, you need to use InputObjectType. (InputObjectTypes are like ObjectTypes but just for input data).

This example should work with graphene 1.0.

class PhoneInput(graphene.InputObjectType):
    number = graphene.String()
    label = graphene.String()

class CreateUser(graphene.Mutation):
    class Input:
        phones = graphene.List(PhoneInput)
    ok = graphene.Boolean()

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()
like image 111
Syrus Akbary Nieto Avatar answered Oct 03 '22 08:10

Syrus Akbary Nieto