No project description provided
Project description
Django Graphql
gqlgen
gqlgen is a generator tool for GraphQL.
Usage: gqlgen [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
all Generate all schema types
field-resolver Generate field resolver.
type Generate one type
type-resolver Generate all schema types
How to use
# urls.py
from django.contrib import admin
from django.urls import path
from djgql.views import GraphQLView
urlpatterns = [
path('admin/', admin.site.urls),
path('graphql/', GraphQLView.as_view())
]
# utils.py
def context_builder():
return {'version': 1}
# settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'djgql.auth.middleware.BasicAuthMiddleware',
]
GRAPHQL_SCHEMA_FILE = os.path.join(BASE_DIR, 'starwar.gql')
GRAPHQL = {
'SCHEMA': 'starwar.schema.schema',
'ENABLE_PLAYGROUND': True,
'CONTEXT_BUILDER': 'starwar.utils.context_builder
}
import typing
from enum import Enum
from django.conf import settings
from gql import query, gql, type_resolver, enum_type, field_resolver
from gql.build_schema import build_schema_from_file
from djgql.auth import login_required
from pydantic import BaseModel
type_defs = gql("""
type Query {
hello(name: String!): String!
}
""")
@enum_type
class Episode(Enum):
NEWHOPE = 1
EMPIRE = 2
JEDI = 3
class Character(BaseModel):
id: typing.Text
name: typing.Optional[typing.Text]
friends: typing.Optional[typing.List[typing.Optional['Character']]]
appears_in: typing.Optional[typing.List[typing.Optional[Episode]]]
class Human(Character):
id: typing.Text
name: typing.Optional[typing.Text]
friends: typing.Optional[typing.List[typing.Optional[Character]]]
appears_in: typing.Optional[typing.List[typing.Optional[Episode]]]
home_planet: typing.Optional[typing.Text]
class Droid(Character):
id: typing.Text
name: typing.Optional[typing.Text]
friends: typing.Optional[typing.List[typing.Optional[Character]]]
appears_in: typing.Optional[typing.List[typing.Optional[Episode]]]
primary_function: typing.Optional[typing.Text]
@query
@login_required
def hero(parent, info, episode: typing.Optional[Episode]) -> typing.Optional[Character]:
request = info.context['request']
print(request.user)
return Human(id='test')
@field_resolver('Human', 'name')
def human_name(parent, info):
return 'Jack'
@type_resolver('Character')
def resolve_character_type(obj, info, type_):
if isinstance(obj, Human):
return 'Human'
if isinstance(obj, Droid):
return 'Droid'
return None
schema = build_schema_from_file(settings.GRAPHQL_SCHEMA_FILE)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-gql-0.0.1.tar.gz
(8.4 kB
view details)
Built Distribution
File details
Details for the file django-gql-0.0.1.tar.gz
.
File metadata
- Download URL: django-gql-0.0.1.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.8.2 Darwin/19.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd72b1ba62e06f52ef1bfc7c048ea775028660dbc6baf0e8b5f2957c5990914f |
|
MD5 | 2ddd8c755d8b9bd3c7232b3803e37342 |
|
BLAKE2b-256 | dc9d1fb4cc29f47fac42b5b535c50c91ecaf4b5a4bb48f4b6d3b02c25a99f3e3 |
File details
Details for the file django_gql-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_gql-0.0.1-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.8.2 Darwin/19.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cdd2172ee47d73700e5fefbf2522c3f25daef399e54a98d1ed818befb036b2c |
|
MD5 | 12f852ebd975415b4c511b58b36347c8 |
|
BLAKE2b-256 | f5254b8f210564529e65ee6258cdab443561ebc6e240c873ebf4742436160883 |