Giter Site home page Giter Site logo

swapi-graphene's Introduction

GraphQL SWAPI using Graphene

This is a integration example of Graphene in Django. View demo

Structure

All the models and fixtures are based in the original swapi project.

The schema (where all the magic happens) is in starwars/schema.py.

Look ma, a GraphQL integration with Django models in less than 150 LOC!

Deploying locally

You can also have your own GraphQL Starwars example running on locally. Just run the following commands and you'll be all set!

git clone [email protected]:graphql-python/swapi-graphene.git
cd swapi-graphene

# Install the requirements
pip install -r requirements_base.txt

# Collect static data
python manage.py collectstatic

# Setup the db and load the fixtures
python manage.py migrate

Once you have everything done, just run:

python manage.py runserver

Open your browser and visit localhost:8080 et voilá!

Deploying on Heroku

To get your own GraphQL Starwars example running on Heroku, click the button below:

Deploy

Fill out the form, and you should be cooking with gas in a few seconds.

swapi-graphene's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swapi-graphene's Issues

Docs: Mutation Example

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 :-)

Mutation example (#2) doesn't work

This useful example now doesn't work, can you update?

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

Demo does not working

I tried demo with this query

{
  allFilms() {
    edges {
      node {
        id
      }
    } 
  }
}

But I got error

{
  "errors": [
    {
      "message": "type object 'Film' has no attribute 'get_object_type'",
      "locations": [
        {
          "line": 16,
          "column": 3
        }
      ]
    }
  ],
  "data": {
    "allFilms": null
  }
}

Get introspective schema on client side

Hello Team,

Im trying to get introspective schema on my android client. for that Im using following command.
apollo-codegen download-schema http://swapi.graphene-python.org/--output schema.json

But it returns me following error:
error: Error while fetching introspection query result: invalid json response body at http://swapi.graphene-python.org/ reason: Unexpected token < in JSON at position 1

I require schema.json at client side to query the server.

Kindly guide.
Thank you.

Support for Github GraphQL

Hey everyone,
that's a really nice project!
Is it possible to query the Github GraphQL API as well?
Github already wrote an example for that in Rails.
Their schema is defined as JSON and they're requiring a personal access token.
Thanks!

Commands from the README don't work, quiver integration broke the master probably

  • Python version: Python 2.7.12
  • Operating System: Ubuntu 16.04

Description

I ran the commands from the README. The migrate failed, look at the backlog below. When I revert commit 5d92929 it works. Quiver integration broke the master probably.

What I Did

user@server$ ./manage.py migrate
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/user/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/.local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/.local/lib/python2.7/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/home/user/.local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/user/.local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "/home/user/.local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/user/.local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/user/.local/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/user/.local/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/user/.local/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/user/.local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/user/.local/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/user/.local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/user/.local/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/user/.ssh/swapi-graphene/swapi_graphene/urls.py", line 27, in <module>
    from graphql.backend.quiver_cloud import GraphQLQuiverCloudBackend
  File "/home/user/.local/lib/python2.7/site-packages/graphql/backend/quiver_cloud.py", line 5, in <module>
    "requests package is required for Quiver Cloud backend.\n"
ImportError: requests package is required for Quiver Cloud backend.
You can install it using: pip install requests

Missing app.json prevent Heroku deployment

Readme includes a button for deploying on Heroku, but clicking it leads to a Heroku error saying:

No app.json located in the repo URL provided.

Make sure that an app.json file exists in the project root directory.

running python manage migrate fails with 'reload' is not defined

Followed the Readme and had an issue

python manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/management/base.py", line 398, in execute
    self.check()
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/ben/.virtualenvs/swapi/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/ben/src/gowork/src/github.com/swapi-graphene/swapi_graphene/urls.py", line 25, in <module>
    from starwars.schema import schema
  File "/Users/ben/src/gowork/src/github.com/swapi-graphene/starwars/schema.py", line 2, in <module>
    reload(sys)
NameError: name 'reload' is not defined

Example is not working

http://swapi.graphene-python.org

query test {
  allFilms {
    edges {
      node {
        id
        episodeId
        title
      }
    }
  }
}

get this:

{
  "data": {
    "allFilms": null
  },
  "errors": [
    {
      "message": "(1862, 'Your password has expired. To log in you must change it using a client that supports expired passwords.')",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ]
    }
  ]
}

login into django admin gives me 500
http://swapi.graphene-python.org/admin/login/?next=/admin/

Another thing is query through connection

{
  allFilms {
    edges {
      node {
        id
        title
        characters {
          edges {
            node {
              id
            }
          }
        }	
        
      }
    }
  }
  __debug {
    sql {
      vendor
      alias
      sql
      duration
      rawSql
      params
      startTime
      stopTime
      isSlow
      isSelect
      transId
      transStatus
      isoLevel
      encoding
    }
  }
}

will give the answer:

{
  "errors": [
    {
      "message": "Cannot combine queries on two different base models.",
      "locations": [
        {
          "column": 9,
          "line": 7
        }
      ]
    },
    {
      "message": "Cannot combine queries on two different base models.",
      "locations": [
        {
          "column": 9,
          "line": 7
        }
      ]
    },
    {
      "message": "Cannot combine queries on two different base models.",
      "locations": [
        {
          "column": 9,
          "line": 7
        }
      ]
    },
    {
      "message": "Cannot combine queries on two different base models.",
      "locations": [
        {
          "column": 9,
          "line": 7
        }
      ]
    },
    {
      "message": "Cannot combine queries on two different base models.",
      "locations": [
        {
          "column": 9,
          "line": 7
        }
      ]
    },
    {
      "message": "Cannot combine queries on two different base models.",
      "locations": [
        {
          "column": 9,
          "line": 7
        }
      ]
    }
  ],
  "data": {
    "allFilms": {
      "edges": [
        {
          "node": {
            "id": "RmlsbTox",
            "title": "A New Hope",
            "characters": null
          }
        },
        {
          "node": {
            "id": "RmlsbToy",
            "title": "The Empire Strikes Back",
            "characters": null
          }
        },
        {
          "node": {
            "id": "RmlsbToz",
            "title": "Return of the Jedi",
            "characters": null
          }
        },
        {
          "node": {
            "id": "RmlsbTo0",
            "title": "The Phantom Menace",
            "characters": null
          }
        },
        {
          "node": {
            "id": "RmlsbTo1",
            "title": "Attack of the Clones",
            "characters": null
          }
        },
        {
          "node": {
            "id": "RmlsbTo2",
            "title": "Revenge of the Sith",
            "characters": null
          }
        }
      ]
    },
    "__debug": {
      "sql": [
        {
          "vendor": "unknown",
          "alias": "default",
          "sql": "SELECT COUNT(*) AS \"__count\" FROM \"starwars_film\"",
          "duration": 0.0011839866638183594,
          "rawSql": "SELECT COUNT(*) AS \"__count\" FROM \"starwars_film\"",
          "params": "[]",
          "startTime": 1493950734.813272,
          "stopTime": 1493950734.814456,
          "isSlow": false,
          "isSelect": true,
          "transId": null,
          "transStatus": null,
          "isoLevel": null,
          "encoding": null
        },
        {
          "vendor": "unknown",
          "alias": "default",
          "sql": "SELECT \"starwars_film\".\"id\", \"starwars_film\".\"created\", \"starwars_film\".\"edited\", \"starwars_film\".\"title\", \"starwars_film\".\"episode_id\", \"starwars_film\".\"opening_crawl\", \"starwars_film\".\"director\", \"starwars_film\".\"producer\", \"starwars_film\".\"release_date\" FROM \"starwars_film\" LIMIT 6",
          "duration": 0.0003190040588378906,
          "rawSql": "SELECT \"starwars_film\".\"id\", \"starwars_film\".\"created\", \"starwars_film\".\"edited\", \"starwars_film\".\"title\", \"starwars_film\".\"episode_id\", \"starwars_film\".\"opening_crawl\", \"starwars_film\".\"director\", \"starwars_film\".\"producer\", \"starwars_film\".\"release_date\" FROM \"starwars_film\" LIMIT 6",
          "params": "[]",
          "startTime": 1493950734.815223,
          "stopTime": 1493950734.815542,
          "isSlow": false,
          "isSelect": true,
          "transId": null,
          "transStatus": null,
          "isoLevel": null,
          "encoding": null
        }
      ]
    }
  }
}

Querying for allSpecies and homeworld executes 38 SQL queries

This query:

{
  allSpecies {
    edges {
      node {
        name
        homeworld {
          name
          gravity
        }
      }
    }
  }
}

Executes 38 sql queries, looking something like this:

(0.001) SELECT COUNT(*) AS "__count" FROM "starwars_species"; args=()
(0.000) SELECT "starwars_species"."id", "starwars_species"."created", 
"starwars_species"."edited", "starwars_species"."name", 
"starwars_species"."classification", "starwars_species"."designation", 
"starwars_species"."average_height", "starwars_species"."skin_colors", 
"starwars_species"."hair_colors", "starwars_species"."eye_colors", 
"starwars_species"."average_lifespan", "starwars_species"."homeworld_id", 
"starwars_species"."language" FROM "starwars_species" LIMIT 37; args=()

(0.000) SELECT "starwars_planet"."id", "starwars_planet"."created", 
"starwars_planet"."edited", "starwars_planet"."name", 
"starwars_planet"."rotation_period", "starwars_planet"."orbital_period", 
"starwars_planet"."diameter", "starwars_planet"."climate", 
"starwars_planet"."gravity", "starwars_planet"."terrain", 
"starwars_planet"."surface_water", "starwars_planet"."population" FROM 
"starwars_planet" WHERE "starwars_planet"."id" = 9; args=(9,)

(0.000) SELECT "starwars_planet"."id", "starwars_planet"."created", 
"starwars_planet"."edited", "starwars_planet"."name", 
"starwars_planet"."rotation_period", "starwars_planet"."orbital_period", 
"starwars_planet"."diameter", "starwars_planet"."climate", 
"starwars_planet"."gravity", "starwars_planet"."terrain", 
"starwars_planet"."surface_water", "starwars_planet"."population" FROM 
"starwars_planet" WHERE "starwars_planet"."id" = 14; args=(14,)

... repeat another 36 times

Full list here: https://gist.github.com/simonw/762a98de85a2f113d1d7ec6026f73ee4

Is it possible to detect this case and then use select_related() or prefetch_related() here to join against the relevant table and get everything done in just one or two queries?

If so, an example showing how to do that would be a fantastic illustration of a slightly more complex use-case for graphene-django (I tried myself and couldn't figure out how to do it).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.