graphql-python / swapi-graphene

GraphQL Starwars API using Graphene and Django

Home Page:http://swapi.graphene-python.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docs: Mutation Example

rattrayalex opened this issue · comments

Hi there,

This looks amazing! Thanks so much for such a simple example of what looks like a terrific library. It would be awesome to see an example with mutations, if for no other reason than to assure readers that they work :-)

Hi @rattrayalex, I didn't implement mutations in the swapi example because it may "overload" the data in the database with non-standard instances (like strange Characters or Spaceships created by people).

There is already an example of using mutations in graphene with Django:
https://github.com/graphql-python/graphene/tree/master/examples/starwars_django
(With tests included)

I could solve this issue by:

  • Create a custom Hero model and let the people create their own heroes through mutations
  • Place a link in the SWAPI README to the example of how to use mutations.

Let me know what you think could be better!

@rattrayalex waiting for your response! 😉

Gotcha, totally makes sense!

I do think it would be pretty cool to see how it works, and both your suggestions sound great to me. I assume you could run a batch job (hourly, maybe?) to delete all heroes so things don't get too crazy. Unless you're getting huge amounts of traffic and even that would be too much?

Cheers!

Hi @rattrayalex,

I've updated the schema adding the createHero mutation.
You can execute the query:

mutation MyMutation {
  createHero(input:{clientMutationId:"a", name:"Peter", homeworldId:"1"}) {
    hero {
      id
      name
      homeworld {
        id
        name
      }
    }
    ok
  }
}

Each time you execute it a new hero will be created

Also, you can query the heroes by:

query {
  allHeroes {
    edges {
      node {
        name
        homeworld {
          name
        }
      }
    }
  }
}

Hope you find it useful! 😉

Wow, amazing - thank you so much! Can't wait to give it a try!

On Fri, Feb 12, 2016, 22:00 Syrus Akbary notifications@github.com wrote:

Hi @rattrayalex https://github.com/rattrayalex,

I've updated the schema adding the createHero mutation.
You can execute the query
http://swapi.graphene-python.org/graphiql?query=mutation%20MyMutation%20%7B%0A%20%20createHero(input%3A%7BclientMutationId%3A%22a%22%2C%20name%3A%22Peter%22%2C%20homeworldId%3A%221%22%7D)%20%7B%0A%20%20%20%20hero%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20homeworld%20%7B%0A%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20ok%0A%20%20%7D%0A%7D
:

mutation MyMutation {
createHero(input:{clientMutationId:"a", name:"Peter", homeworldId:"1"}) {
hero {
id
name
homeworld {
id
name
}
}
ok
}
}

Each time you execute it a new hero will be created

Also, you can query the heroes by:

query {
allHeroes {
edges {
node {
name
planet {
name
}
}
}
}
}

Hope you find it useful! [image: :wink:]


Reply to this email directly or view it on GitHub
#2 (comment)
.